Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ATTiny402 Periodic ISR Interrupt Example

 
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: Sat Dec 19, 2020 9:23 pm    Post subject: ATTiny402 Periodic ISR Interrupt Example Reply with quote

The new "XTinies" are rapidly replacing the classic AVRs.

This is a simple example of setting up the Tiny402's Timer/Counter A # 0
to generate an interrupt at 1 KHz, (Once per each 1 mSec).

JC


Code:

'File:  ATTiny402 ISR Demo V1.bas
'Bascom 2.0.8.3
'Carter   Dec. 19,2020

'This is a simple LED flasher for the XTiny402.
'It uses the Timer/Counter (A) to generate an
'interrupt at 1 KHz, (Once / 1 mSec).

'Once one knows how to do this it is easy!
'This demonstrates one approach to doing this.

'This uses the Timer/Counter (A) Overflow Interrupt
'to generate an interrupt at a 1 KHz rate.

'In theory one could use the Compare Ch0 interrupt
'for this, but I've not yet had success with that.

'When new, before one changes any Fuses,
'the XTiny402 runs on the Internal RC Osc at 20 MHz.
'There is also a default clock divider of Divide by 6.
'The default start up clock is therefore:
'20.0 MHz / 6 --> 3.33 MHz

'Hardware:
'XTiny402 microcontroller
'Internal clock at 3.332 MHz
'One LED (and series resistor) on PortA.6  (Pin 2).

'This example directly sets the micro's
'registers to configure the T/C.

'...............................................................................
   $regfile = "atxtiny402.dat"                             'Specify the uC"
   $crystal = 3333333                                      '3.33 MHz

   $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       'LED 5

   LED5 Alias PortA.6

Vars:
    Dim HBCnt as Word      'Heart Beat Counter for ISR

PreMain:
   Waitms 100          'Hardware Startup Delay:
   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, as there is only one of these
   'modules present in this micro.
   'It is still identified as #0, however.

   'Use Normal Mode, OverFlow Intr, for once / 1 mSec ISR rate.

   'Default Tiny402 Internal RC Clock = 20 MHz
   'Default Clock Divisor = Div by 6
   'Default Clock = 20.0 / 6.0 = 3333333 Hz  (3.33 MHz)

   'TCA0 Pre-Scaler set to Div by 1  (i.e. Not Div by anything)
   'TCA0 Pre-Scaler Output = 3333333 Hz
   'Set TOP to 3332 (+1 for roll over)
   '3333333 Hz / 3333  -> 1000.01 Interrupts / Sec

   'Set up the TCA0 Overflow ISR Manually:

   'Set TOP Value for the Overflow ISR
   'This is Div by 3333, Roll over counts as 1 tic.
   TCA0_PER = 3332

   TCA0_CTRLA = 1    'Pre-Scaler Div by 1, Enable module
   TCA0_CTRLB = 0    'Normal Mode

   'Define the ISR Handler for the ISR:
   On TCA0_OVF  HeartBeat        'Run ISR


   'Use the following in the remainder of the program:

   'Enable the Overflow Interrupt:
   'TCA0_INTCTRL = 1      'Enable Overflow Interrupt

   'Use this to Clear the ISR Flag within the ISR:
   'Just calling the ISR does NOT automatically clear
   'the interrupt flag
   'TCA0_INTFLAGS = 1       'Clear the OVF ISR Flag

Main:
   'The Main Loop does nothing.
   'All of the work flashing the LED is done
   'within the ISR.

   'Enable the specific TCA0 Overflow Interrupt:
   'Enable TCA0_OVF      '
   TCA0_INTCTRL = 1      'Enable Overflow ISR

   'Now Enable overall, global, interrupts:
   Enable Interrupts   'Overall Interrupts Enabled

   Do
      'Do nothing...
      'The ISR is doing the LED flashing
      NOP
      NOP
      NOP
   Loop

HeartBeat:
   'Heartbeat ISR
   'Flash LED 1 / Sec
   'ISR fires 1 / every 1 mSec   (1 KHz rate)
   'Uses TCA0 Overflow ISR
   'Rollover counts as 1 tic

   'First manually clear the ISR Flag:
   TCA0_INTFLAGS = 1         'OVF 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 < 25 then
      Set LED5       'On
   Else
      Reset LED5     'Off
   End If

   Return



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