Working timer1, but wrong timings

This commit is contained in:
Daniele Verducci su MatissePenguin 2021-01-16 19:54:32 +01:00
parent 7c07014b75
commit cf3956356e

View File

@ -39,10 +39,11 @@
; start vector ; start vector
.org 0x0000 .org 0x0000
rjmp main ; jump to main label rjmp main ; reset vector: jump to main label
.org 0x0012 .org 0x001E
rjmp on_int1 ; interrupt for timer 1 overflow rjmp on_tim1_ovf ; interrupt for timer 1 overflow
.org 0x40
; main program ; main program
main: main:
; pins setup ; pins setup
@ -52,7 +53,7 @@ main:
out DDRA, r16 ; set port as output (contains video pin) out DDRA, r16 ; set port as output (contains video pin)
;*** Load data into ram *** ; *** Load data into ram ***
; Set X to 0x0100 ; Set X to 0x0100
ldi r27, high(FRAMEBUFFER<<1) ldi r27, high(FRAMEBUFFER<<1)
ldi r26, low(FRAMEBUFFER<<1) ldi r26, low(FRAMEBUFFER<<1)
@ -70,27 +71,20 @@ main:
cpi r26, 0b11000000 cpi r26, 0b11000000
brne load_mem_loop ; if not 0, repeat h_picture_loop brne load_mem_loop ; if not 0, repeat h_picture_loop
; timer setup (use 16-bit counter TC1) ; *** timer setup (use 16-bit counter TC1) ***
; The Power Reduction TC1 and TC3 bits in the Power Reduction Registers (PRR0.PRTIM1 and ; The Power Reduction TC1 and TC3 bits in the Power Reduction Registers (PRR0.PRTIM1 and
; PRR1.PRTIM3) must be written to zero to enable the TC1 and TC3 module. ; PRR1.PRTIM3) must be written to zero to enable the TC1 and TC3 module.
ldi r16, 0b00001000 ldi r16, 0b00000000
sts PRR0, r16 sts PRR0, r16
ldi r16, 0b00000001 ; Set timer prescaler to 1:1
sts PRR1, r16 LDI r16,0b00000001
; Set TCNT1 (timer counter) to 0xFF00 (the timer will trigger soon) sts TCCR1B,r16
ser r27 ; Enambe timer1 overflow interrupt
sts TCNT1H,r27 LDI r16,0b00000001
clr r26 STS TIMSK1,r16
sts TCNT1L,r26 ; Enable interrupts globally
; Set prescaler to 1:1 (TCCR1B is XXXXX001) SEI
ldi r16, 0b00000001 ; Timer setup completed.
sts TCCR1B, r16
; Enable timer1 overflow interrupt(TOIE1): the interrupt 1 will be fired when timer resets
ldi r16, 0b00000100
sts TIMSK1, r16
; The Global Interrupt Enable bit must be set for the interrupts to be enabled.
ldi r16, 0b10000000
sts SREG, r16
; loop forever ; loop forever
forever: forever:
@ -98,9 +92,9 @@ main:
; ********* FUNCTIONS CALLED BY INTERRUPT *********** ; ********* FUNCTIONS CALLED BY INTERRUPT ***********
on_int1: on_tim1_ovf:
; called by timer 1 two times per line (every 32 uS) during hsync. Disabled while drawing picture. ; called by timer 1 two times per line (every 32 uS) during hsync, unless drawing picture.
; if r25 >= 32 then r25=0 ; if r25 >= 32 then r25=0
cpi r25, 32 cpi r25, 32
brlt switch_status brlt switch_status
@ -112,6 +106,7 @@ on_int1:
cpi r25, 10 ; 5-9: short sync cpi r25, 10 ; 5-9: short sync
breq draw_picture ; 10: draw picture breq draw_picture ; 10: draw picture
jmp short_sync ; 11-16: short_sync jmp short_sync ; 11-16: short_sync
; reti is at end of all previous jumps
draw_picture: draw_picture:
; increment status ; increment status
@ -135,7 +130,7 @@ draw_picture:
; video pin goes low before sync ; video pin goes low before sync
clr r19 ; 1 cycle clr r19 ; 1 cycle
out PORTA, r19 ; 1 cycle out PORTA, r19 ; 1 cycle
cbi PORTC, SYNC_PIN ; sync goes low (0v) ; 2 cycle cbi PORTC, SYNC_PIN ; sync goes low (0v) ; 2 cycle
ldi r18, 31 ; 1 cycle ldi r18, 31 ; 1 cycle
l_sync_pulse_loop: ; requires 3 cpu cycles l_sync_pulse_loop: ; requires 3 cpu cycles
@ -198,7 +193,7 @@ draw_picture:
; video pin goes low before sync ; video pin goes low before sync
clr r19 ; 1 cycle clr r19 ; 1 cycle
out PORTA, r19 ; 1 cycle out PORTA, r19 ; 1 cycle
; debug ; debug
; sbi PORTC, DEBUG_PIN ; high ; sbi PORTC, DEBUG_PIN ; high
; cbi PORTC, DEBUG_PIN ; low ; cbi PORTC, DEBUG_PIN ; low
@ -230,7 +225,7 @@ long_sync:
sts TCNT1H,r27 sts TCNT1H,r27
sts TCNT1L,r26 sts TCNT1L,r26
reti reti
long_sync_end: long_sync_end:
; sync pin is low (sync is occuring) ; sync pin is low (sync is occuring)
sbi PORTC, SYNC_PIN ; sync goes high (0.3v) sbi PORTC, SYNC_PIN ; sync goes high (0.3v)
@ -256,7 +251,7 @@ short_sync:
sts TCNT1H,r27 sts TCNT1H,r27
sts TCNT1L,r26 sts TCNT1L,r26
reti reti
short_sync_end: short_sync_end:
; sync pin is low (sync is occuring) ; sync pin is low (sync is occuring)
sbi PORTC, SYNC_PIN ; sync goes high (0.3v) sbi PORTC, SYNC_PIN ; sync goes high (0.3v)