Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

aTTiny402 Periodic Interrupt Example 2

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 586
Location: Cleveland, OH

usa.gif
PostPosted: Wed Dec 23, 2020 4:33 am    Post subject: aTTiny402 Periodic Interrupt Example 2 Reply with quote

There are not many XTiny examples on the Forum yet, so this is another simple one.

It Configures the Timer/Counter A 0 to perform a periodic interrupt using the Compare0 channel.

One can set the 16-Bit comparison value to anything, so one can set the exact ISR rate.
This is analogous to the CTC Mode in the older AVRs.

This runs on an ATTiny402 with an LED on PortA.6.
It runs the ISR at 1 KHz rate, and flashes the LED at a 1 Hz rate.

Remember to clear the ISR flag from within the ISR!

The other example posted shows using the TCA0 Overflow interrupt.
The TCA0 Compare0 gives fine control over the ISR rate.

JC



Code:

'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
 
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Mon Dec 28, 2020 11:53 am    Post subject: Reply with quote

thanks for the sample JC.
I think migration from xmega to xtiny is simple.
But a sample tells more than a manuals Very Happy

_________________
Mark
Back to top
View user's profile Visit poster's website
andro

Bascom Member



Joined: 12 Mar 2007
Posts: 94
Location: Ljubljana

slovenia.gif
PostPosted: Tue Apr 27, 2021 10:02 pm    Post subject: Reply with quote

Hi,

just had the same issue:
Quote:
Enable TCA0_CMP0 'Doesn't seem to work


Best regards
Andrej
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum