Hi All,
I have spent the last 3 days trying to make Timer0 and Timer2 to work in CTC mode. I am running BascomAVR 2.0.7.2 in Windows7 Pro
64bit.
I wrote several variants of the code; configuring the Timers with "Config TimerX" statements, manipulating directly the Timer
registers, activating/deactivating the Timers interrups, etc, but not way, I can never get the Timers to reset on Compare Match in the
Simulator.
It seems I am doing something wrong but I cannot find the source of the error.
I am lost now and I need your help.
This is the latest code I am working with:
[code:1:b1a24882f0]
$regfile = "M328PDEF.DAT" ' used micro
$crystal = 16000000 ' used xtal --> Cycle Time: 62.5nS
$baud = 57600
$hwstack = 64
$swstack = 50
$framesize = 64
''-------------------------------------------------------------------------------
Declare Sub Config_t0_manually
Declare Sub Config_t2_manually
Compare0a = 25 'Same as OCR0A=25
Compare2a = 50 'Same as OCR2A=25
On Compare0a Timer0compare
On Compare2a Timer2compare
On Timer0 T0ovf
On Timer2 T2ovf
'The following 4 commented lines also doesn't work.
'Config Timer0 = Ctc , Prescale = 1024 , Compare A = Disconnect , Compare B = Disconnect , Clear Timer = 1
'Enable Timer0
'Config Timer2 = Ctc , Prescale = 1024 , Compare A = Disconnect , Compare B = Disconnect , Clear Timer = 1
'Enable Timer2
Config_t0_manually
Config_t2_manually
Enable Interrupts
Do
Print "T0:" ; Timer0 ; " T2:" ; Timer2
Print "T0IF:" ; Tifr0 ; " T2IF:" ; Tifr2
Loop
End
Timer0compare:
Print "T0 Compare Match. Timer=" ; Timer0
Return
Timer2compare:
Print "T2 Compare Match. Timer=" ; Timer2
Return
T0ovf:
Print "OVF0, Timer=" ; Timer0
Return
T2ovf:
Print "OVF2, Timer=" ; Timer2
Return
Sub Config_t0_manually
'Do not use Start Timer0 statement as it writes 0 to TCCR0 register
'because there is no previous Config Timer statement.
Timsk0 = &B0000_0011
'b2: OCIE0B
'b1: OCIE0A
'b0: TOIE0
Tccr0a = &B0000_0010
'b7: COM0A1
'b6: COM0A0
'b5: COM0B1
'b4: COM0B0
'b1: WGM01
'b0: WGM00
Tccr0b = &B0000_0101 'Prescaler=1024
'b7: FOC0A
'b6: FOC0B
'b3: WGM02
'b2: CS2
'b1: CS1
'b0: CS0
'Tifr0 = &B0000_0001
'b2: OCF0B
'b1: OCF0A
'b0: TOV0
End Sub
Sub Config_t2_manually
'Do not use Start Timer2 statement as it writes 0 to TCCR2 register
'because there is no previous Config Timer statementet.
Timsk2 = &B0000_0011
'b2: OCIE2B
'b1: OCIE2A
'b0: TOIE2
Tccr2a = &B0000_0010
'b7: COM2A1
'b6: COM2A0
'b5: COM2B1
'b4: COM2B0
'b1: WGM21
'b0: WGM20
Tccr2b = &B0000_0111
'b7: FOC2A
'b6: FOC2B
'b3: WGM22
'b2: CS2
'b1: CS1
'b0: CS0
'Tifr2 = &B0000_0000
'b2: OCF2B
'b1: OCF2A
'b0: T0V2
End Sub
[/code:1:b1a24882f0]
Looking directly at the Timer count values in the simulator I see that the Timers only reset to 0 after reaching 255 and not after
reaching the values configured in COMPAREA and COMPAREB registers.
The OCF0A and OCF2A are never set, indicating that I never have a match, instead of that I get TOV0 and TOV2 flags set when the counters overflows 255.
Maybe I didn't understand the datasheet correctly, please let me know if this is the case.
Acording to the datasheet I configured WGM02=0, WGM01=1 and WGM00=0 and WGM22=0, WGM21=1 and WGM20=0, that is CTC mode for both Timers.
It also seems that both timers run at different speeds in the simulator even when both of them are using the same prescaler, Has someone experienced this issue?
Could someone of you test this piece of code in the simulator to check if any of the counters sets the compare flag or trigger the compare interrupt?
It shoudn't be so difficult to configure CTC mode, so I am pretty sure I made a mistake I overlooked but I cannot find where it is.
Any help will be much appreciated.
Thanks....
Fran
[b:b1a24882f0][color=red:b1a24882f0](BASCOM-AVR version : 2.0.7.2 , Latest : 2.0.7.6 )[/b:b1a24882f0][/color:b1a24882f0]
↧