'File: Tiny402 ISR TCA Comp0 Test V2.bas
'Bascom 2.0.8.3
'Carter Dec. 21,2020
'This is a simple LED flasher for the XTiny402.
'It uses the Timer/Counter (A) to generate an
'interrupt at 1 KHz using the Compare0 interrupt.
'The Comp0 16-bit Register can be set to anything,
'so one can precisely control the interrupt rate.
'This is analogous to the CTC interrupt in the
'older AVR micros.
'XTiny402 powers up at 16 or 20 MHz on Int RC Osc.
'20 MHz is the default unless Fueses are changed.
'The clock has a Div by 6 as default, so the actual
'clock is 3.33 MHz on power up until changed by SW.
'Hardware:
'XTiny402 microcontroller
'Internal clock config'd for 10.0 MHz, on 3.0 V Batts
'LED on PortA.6, (With a series resistor)
'Method:
'On power up set the Int RC Osc to 10.0 MHz.
'Config Timer/Counter A 0 for FREQ Mode.
'Set TCA Pre-Scaler to Divide by 2.
'Clock into TCA is then 5.0 MHz.
'Set Compare0 ("TOP") to 5000.
'5.0 MHz / 5000 -> 1000 interrupts / Sec.
'Set Comp0 to 4999 as roll over counts as 1 tic.
'In the HeartBeat ISR Flash at LED at 1 Hz.
'The Main Loop is doing nothing.
'...............................................................................
$regfile = "atxtiny402.dat" 'Specify the uC"
$crystal = 10000000 '10.0 MHz, Internal RC Osc
$hwstack = 48 'Hardware Stack
$swstack = 48 'Software Stack
$framesize = 48 'Frame Space
'Now Configure the Port's Pins for Input or Output mode.
Config PortA.6 = Output 'LED1
LED1 Alias PortA.6
Vars:
Dim HBCnt as Word 'Heart Beat Counter for ISR
Dim LpCnt1 as Byte
PreMain:
Waitms 50 'Hardware Startup Delay:
'Config Internal RC 20 MHz Osc for 10.0 MHz System Clock
'Set Clock to 10.0 MHz:
Config Sysclock =20MHZ, Prescale = 2
Waitms 50
'Init variables:
HBCnt = 0 'Init/Clear HeartBeat Counter
ConfigTCA:
'Config MUST be in Main, not in a Subroutine!
'Configure the Tiny402 Timer/Counter A, #0 (16-Bit)
'There is only TCA#0 (Zero), as there is only one
'of these modules present in this micro.
'It is still identified as #0, however.
'Configure in "FREQ" Mode, so can set the exact ISR Rate.
'This is analogous to the older micro's CTC Mode.
'NOTE WELL: Remember to clear the ISR Flag inside the ISR.
'It is not automatically cleared by executing the ISR.
'Power Up: Internal RC OSC = 20 MHz, (Unless Fuses changed)
'Clock was set to 10.0 MHz, above, (3.0 V power supply)
'10.0 MHz Clock, Prescaler of Div by 2 -> 5 MHz into TCA.
'Set TCA_Compare0 to divide by 5000, for a resultant
'ISR rate of 1000 Interrupts / Sec.
'Set "TOP", i.e. CMP0, to 4999 as roll over = 1 tic.
Config TCA0 = Freq, Prescale = 2 , run = on
TCA0_CMP0 = 4999 'TOP value for counter
On TCA0_CMP0 HeartBeat 'Define ISR to execute
'Note: Enable the Intr manually.
' Enable TCA0_CMP0 'Doesn't seem to work
TCA0_INTCTRL = 16 'Enable COMP0 ISR
Main:
'The Main Loop does nothing.
'All of the work flashing the LED is done
'within the ISR.
Gosub FlashLED 'Startup flash
'Now Enable overall, global, interrupts:
Enable Interrupts 'Overall Interrupts Enabled
Do
NOP 'Do nothing
Loop
HeartBeat:
'Heartbeat ISR
'ISR fires @ 1 KHz, (q 1 mSec)
'Flash LED 1 / Sec
'First manually clear the ISR Flag:
TCA0_INTFLAGS = 16 'COMP0 Intr Flag
'Next Increment a counter
'1000 interrupts = 1 Second
HBCnt = HBCnt + 1
If HBCnt >= 999 then
'Rollover to 0
HBCnt = 0
End If
'Flash the LED at the start of each second
If HBCnt < 100 then
Set LED1 'On
Else
Reset LED1 'Off
End If
Return
FlashLED:
'Startup flash
For LpCnt1 = 1 to 5
Set LED1
Waitms 100
Reset LED1
Waitms 100
Next LpCnt1
Return
|