View previous topic :: View next topic |
Author |
Message |
jeremywilson
Joined: 11 Jul 2011 Posts: 79 Location: United States
|
Posted: Wed Jul 21, 2021 3:50 pm Post subject: ATTINY1604 TCA0 & TCB0 |
|
|
I'm working on my first ATTINY project and can't get the timers to function properly.
I'm trying to toggle an LED every one second (Code A & B), and put a 38KHz 50% wave on another output (Code C).
problem 1:
Code A & B below are alternate ways to blink the LED.
When executed, the LED is high for 1 second then 52.3Khz 50% wave forever after - same result for code A & B.
problem 2:
Code C does nothing (pin stays low). I'm pretty sure this is because I haven't steered the wave output to PA5 (see image below).
The datasheet is unclear how to steer WO0 to PA5 for TCB0. I've looked at the DAT file, Help file and datasheet and can't figure it out.
Any help would be greatly appreciated!
Code: | $regfile = "atxtiny1604.dat" ' ATTINY1604
Config Sysclock = 16_20mhz , Prescale = 2 : $crystal = 10000000 ' 10 Mhz
Config Submode = New
$hwstack = 40 ' hardware stack size
$swstack = 40 ' software stack size
$framesize = 100 ' frame size
'Port A
Config Porta = &B00111010
Mod_38khz Alias Porta.5
'Port B
Config Portb = &B11111000
Led Alias Portb.3 : Set Led
Sub Timer_isr()
Toggle Led
End Sub
' Code A - toggle LED every 1 second
On Tca0_ovf Timer_isr ' timer ISR
' Enable Tca0_ovf ' enable timer interrupt
Tca0_per = 9766 ' 1 second (10Mhz, 1024 prescale)
Config Tca0 = Normal , Prescale = 1024 , Run = On , Ovf_int = Enabled
' Code B - toggle LED every 1 second
'Config Tca0 = Freq , Prescale = 1024 , Run = On
'Tca0_cmp0 = 9766
'On Tca0_cmp0 Timer_isr
'Tca0_intctrl = 16 ' Enable COMP0 ISR
' Code C - IR LED modulation 38Khz/50% duty on PA5
Config Tcb0 = Pwm , Prescale = 2 , Run = On , Ccmp_output = Enabled
Tcb0_ccmph = 66 ' no. of high cycles
Tcb0_ccmpl = 131 ' no. period cycles - 1
Enable Interrupts ' enable all interrupts
Main_loop:
Goto Main_loop |
[/img][/code]
(BASCOM-AVR version : 2084 , Latest : 2.0.8.4 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Jul 21, 2021 6:48 pm Post subject: |
|
|
1:
you need to reset the timer flag interrupt by writing a 1.
otherwise the timer will not appear again.
see also the help for xtiny
it is similar as in Xmega
3: you need to set the output pin to output mode.
here are some samples, i am not sure if they were included in 2084 release :
Code: | '--------------------------------------------------------------------------------
'name : TCA0-PWM.bas
'copyright : (c) 1995-2021, MCS Electronics
'purpose : demonstrates TCA0
'micro : xtiny816
'suited for demo : no
'commercial addon needed : yes
'--------------------------------------------------------------------------------
$regfile = "atXtiny816.dat"
$crystal = 20000000
$hwstack = 16
$swstack = 16
$framesize = 24
'set the system clock and prescaler
Config Sysclock = 16_20mhz , Prescale = 1 , Clockout = Enabled
'configure the USART
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1
Waitms 2000
Print "Test TCA0"
Config Portb.0 = Output 'WO0 output
Config Tca0 = Pwm_bot , Prescale = 1 , Resolution = Normal , Compare0 = Enabled , Run = On
Tca0_per = 8000 'PWM frequency (period)
Tca0_cmp0 = 2000 '25% duty cycle on pin PB0 (WO0)
Do
nop
Loop |
Code: | '--------------------------------------------------------------------------------
'name : TCB0-PWM.bas
'copyright : (c) 1995-2021, MCS Electronics
'purpose : demonstrates TCB0 using TCA0 as input
'micro : xtiny816
'suited for demo : no
'commercial addon needed : yes
'--------------------------------------------------------------------------------
$regfile = "atXtiny816.dat"
$crystal = 20000000
$hwstack = 16
$swstack = 16
$framesize = 24
'set the system clock and prescaler
Config Sysclock = 16_20mhz , Prescale = 1 , Clockout = Enabled
'configure the USART
Config Com1 = 115200 , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1
Waitms 2000
Print "Test TCB0-TCA0"
' notice the variable W whih is overlayed with L and H
Dim B As Byte , L As Byte , H As Byte , W As Word At L Overlay
Config Tca0 = Normal , Prescale = 1024 , Run = On
Config Porta.5 = Output 'make TCB0 wave output an output
Config Tcb0 = Pwm , Prescale = Tca0 , Run = On , Ccmp_output = Enabled
'we use tca0 for the prescaler
'With this settings the lowest frequency is 76Hz max is 193
L = 255 : 'this is the low value of tcb0_ccmp from1 to 255 duty Cycle
H = 32 : 'this is the high value of tcb0_ccmp from 1 to 100 frequency
Do
'get serial input and change the frequency and pwm
B = Inkey()
If B = "u" Then
Incr L : Gosub Update ' Tcb0_ccmpl = L
Elseif B = "d" Then
Decr L : Gosub Update ' Tcb0_ccmpl = L
Elseif B = "<" Then
Incr H : Gosub Update ' Tcb0_ccmph = H
Elseif B = ">" Then
Decr H : Gosub Update ' Tcb0_ccmph = H
End If
Loop
Update:
Print W 'show the new value
Tcb0_ccmp = W 'assign it to tcb compare register
Return
|
_________________ Mark |
|
Back to top |
|
|
jeremywilson
Joined: 11 Jul 2011 Posts: 79 Location: United States
|
Posted: Wed Jul 21, 2021 7:37 pm Post subject: |
|
|
Thanks for the response:
Quote: | 1:
you need to reset the timer flag interrupt by writing a 1.
otherwise the timer will not appear again. |
Working now.
Quote: | 3: you need to set the output pin to output mode. |
Code: | Config Porta = &B00111010 |
The bit for PA5 is a 1. That should be an output, no?
I tried using Config PortA.5=output to no avail. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Jul 21, 2021 7:50 pm Post subject: |
|
|
yes that should be an output but check the second example where both low and high registers are written in one go using overlay. _________________ Mark |
|
Back to top |
|
|
jeremywilson
Joined: 11 Jul 2011 Posts: 79 Location: United States
|
Posted: Wed Jul 21, 2021 8:43 pm Post subject: |
|
|
All good now ! Many thanks !
I never would have figured out the overlay thing. I use interrupts frequently on ATMega2560/1 and often write low and high register bytes separately and never had an issue. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Jul 22, 2021 2:41 pm Post subject: |
|
|
yes that is odd in these processors. i dont see the point of having separate registers if you can not update them separately.
but i might have missed something in the data sheets. During the last year(s) i have read many of them and some have changed a few times.
the registers are derived from the microchip atdf files. _________________ Mark |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Fri Jul 23, 2021 9:44 am Post subject: |
|
|
Hi Mark
Yes, it doesn't make sense. It's a bug in the processor architecture.
CCMP and CNT Registers Operate as 16-Bit Registers in 8-Bit PWM Mode
When the TCB operates in 8-bit PWM mode (CNTMODE in TCBn.CTRLB is ‘0x7’), the low and high bytes for
the CNT and CCMP registers operate as 16-bit registers for read and write. They cannot be read or written
independently.
Another thing is that if you want to use the TCA clock you have to set the sync bit of the TCB. In your case you need to write 21dec to CTRLA and not 5dec. And restart TCA, but there is another HW bug to watch out for.
The TCA Restart Command Does Not Force a Restart of TCB
The TCA restart command does not force restarting the TCB when TCB is running in SYNCUPD mode. TCB is
restarted only after a TCA OVF
These bugs are in the kernel of both AVR0 and AVR1. With AVR2 they have already fixed the sync bug, but the 8bit register access bug remains.
RS |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Jul 23, 2021 9:58 am Post subject: |
|
|
Quote: | In your case you need to write 21dec to CTRLA and not 5dec. |
The option in config "SYNCUPDATE" only sets this bit 4 in CTRLA. it need to be used with other settings.
It is not clear to me if you mean there is a wrong setting?
anyway, there are always some bugs in the silicon. as long we know about it, there is no problem. _________________ Mark |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Fri Jul 23, 2021 11:00 am Post subject: |
|
|
Hi
you don't make the sync setting when you select the clock.
It doesn't matter anyway. A bug in the HW won't allow synchronization anyway and you have to bypass it, see the procedure I sent.
You write 0x05 and you would have 0x15. Ultimately you set the clock to 0x00 as a baseline and wait for the reboot to be performed.
RS |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Jul 23, 2021 1:24 pm Post subject: |
|
|
i dont get what you mean. please show the CONFIG statement and where it writes the wrong setting.
maybe you refer to the second sample? please note that i did not specify sync option there. if you do, you will see that bit 4 will be set. _________________ Mark |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Fri Jul 23, 2021 1:54 pm Post subject: |
|
|
I'm sending you an email |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Jul 23, 2021 8:44 pm Post subject: |
|
|
ok, i got it. there are 2 things.
the config can use this optional sync : Code: | Config Tcb0 = Pwm , Prescale = Tca0 , Run = On , Ccmp_output = Enabled , Syncupdate = Tca0 |
But a bug in silicon prevents it to work.
then the second issue is that avr studio shows different values for the registers. it could be a bug in timer simulation/studio.
bascom does not simulate these timers anyway but it does show the proper values in the IO registers. _________________ Mark |
|
Back to top |
|
|
|