Search code examples
assemblytimerinterruptresetavr

AVR interrupt - reset vectors handling in assembly


I am using AVR ATmega16 and I am trying to implement a counter that counts seconds. I am using timer0 in order to produce an interrupt every 1/16 sec on a loop that repeats 16 times. Given the frequency of the microprocessor (f=4Mhz) and the prescaler value (=1024) I calculate that I need to give the timer an initial value of 12.

Here is the start of a sample code that I found on a textbook of mine:

.include "m16def.inc"
.equ start = 12
.equ loops = 16
jmp reset
reti (x16)    ;there will be needed a total of 16 reti's in order for the next
              ;instruction to be at 0x12 = 0d18 where the jmp instruction is placed at the 
              ;overflow interrupt handling routine of timer0
jmp TIM0_OVF
reti          ;other Handlers

Could please some explain what those two reti commands are actually doing? How do they work? What does the comment on the code mean?

*Let me inform you that on ATmega16 the TIMER0_OVF vector is on the program memory address = 0x12


Solution

  • This is the interrupt vector table of the ATMega16:

    1 $000(1) RESET External Pin, Power-on Reset, Brown-out
    Reset, Watchdog Reset, and JTAG AVR
    Reset
    2 $002 INT0 External Interrupt Request 0
    3 $004 INT1 External Interrupt Request 1
    4 $006 TIMER2 COMP Timer/Counter2 Compare Match
    5 $008 TIMER2 OVF Timer/Counter2 Overflow
    6 $00A TIMER1 CAPT Timer/Counter1 Capture Event
    7 $00C TIMER1 COMPA Timer/Counter1 Compare Match A
    8 $00E TIMER1 COMPB Timer/Counter1 Compare Match B
    9 $010 TIMER1 OVF Timer/Counter1 Overflow
    10 $012 TIMER0 OVF Timer/Counter0 Overflow
    11 $014 SPI, STC Serial Transfer Complete
    12 $016 USART, RXC USART, Rx Complete
    13 $018 USART, UDRE USART Data Register Empty
    14 $01A USART, TXC USART, Tx Complete
    15 $01C ADC ADC Conversion Complete
    16 $01E EE_RDY EEPROM Ready
    17 $020 ANA_COMP Analog Comparator
    18 $022 TWI Two-wire Serial Interface
    19 $024 INT2 External Interrupt Request 2
    20 $026 TIMER0 COMP Timer/Counter0 Compare Match
    21 $028 SPM_RDY Store Program Memory Ready
    

    In the code you've shown, the first slot (RESET) is filled by the jmp reset instruction. The next slot you're interested in filling is TIMER0_OVF, but inbetween RESET and TIMER0_OVF there are 8 slots that you should fill with something. Filling them with reti (RETurn from Interrupt) instructions is probably sufficient if you don't care about handling those interrupts.

    If we assume that reti (x16) is interpreted by this assembler as "output 16 reti instructions", then this will take care of filling those 8 slots. In case you're wondering "why 16 instructions for 8 slots?" - that's because each of those slots take up 2 words (enough to fit a jmp instruction), and the size of a single reti instruction is 1 word. So you fill each slot with two reti instructions, the second of which serves no real purpose.