Timer Overflow Interrupt


C and Assembly programs to increment Port A when the timer overflows (using interrupts)


Assembly Program:


; Program to increment Port A every 32 ms
; using the timer overflow interrupt

prog:    equ     $0800
data:    equ     $0900
int_vec: equ     $0B10

TSCR:    equ     $0086
TMSK2:   equ     $008D
TFLG2:   equ     $008F

DDRA:    equ     $0002
PORTA:   equ     $0000

CODE:    section .text    ;The stuff which follows is program code
         org     prog     ;set program counter to 0x0800
         lds     #$0A00
         ldaa    #$FF
         staa    DDRA     ; Port A output
         clr     PORTA    ; 0 -> Port A
         ldaa    #$80
         staa    TSCR     ; Turn on timer
         ldaa    #$82
         staa    TMSK2    ; Enable timer overflow interrupt; 32 ms overflow 
         ldaa    #$80
         staa    TFLG2    ; Clear TOI flag
         cli              ; Enable interrupts
loop:    bra     loop     ; Repeat forever

toi_isr: inc     PORTA    ; 
         ldaa    #$80
         staa    TFLG2    ; Clear TOI flag
         rti

VEC:     section .text     ; Constant vectors
         org     int_vec
         dc.w    0         ; BDLC
         dc.w    0         ; ATD             
         dc.w    0         ; reserved
         dc.w    0         ; SCI0          
         dc.w    0         ; SPI             
         dc.w    0         ; Pulse acc input 
         dc.w    0         ; Pulse acc overflow 
         dc.w    toi_isr   ; Timer overflow  
         dc.w    0         ; Timer channel 7 
         dc.w    0         ; Timer channel 6 
         dc.w    0         ; Timer channel 5 
         dc.w    0         ; Timer channel 4 
         dc.w    0         ; Timer channel 3 
         dc.w    0         ; Timer channel 2 
         dc.w    0         ; Timer channel 1 
         dc.w    0         ; Timer channel 0 
         dc.w    0         ; Real time       
         dc.w    0         ; IRQ             
         dc.w    0         ; XIRQ            
         dc.w    0         ; SWI             
         dc.w    0         ; illegal         
         dc.w    0         ; cop fail        
         dc.w    0         ; cop clock fail  
         dc.w    $ff80     ; RESET           


C Program:


Main Program toi.c


#include "hc12.h"

main()
{
    DDRA = 0xff;        /* Make Port A output */
    TSCR = 0x80;        /* Turn on timer */
    TMSK2 = 0x84;       /* Enable timer overflow interrupt, set prescaler */
    TFLG2 = 0x80;       /* Clear timer interrupt flag */
    enable();           /* Enable interrupts (clear I bit) */
    while (1)
    {
                        /* Do nothing - go into low power mode */
    }
}

@interrupt void toi_isr(void)
{
    PORTA = PORTA + 1;  /* Increment Port A */
    TFLG2 = 0x80;       /* Clear timer interrupt flag */
}


Vector locations vector.c

/*      INTERRUPT VECTORS TABLE 68HC12
 */
void toi_isr();         /* character receive handler */

/* Vectors at 0xFFD0 on standard HC12; remapped to 0x0B10 with D-Bug 12 */
void (* const _vectab[])() = {
        0,              /* BDLC               */
        0,              /* ATD                */
        0,              /* reserved           */
        0,              /* SCI0               */
        0,              /* SPI                */
        0,              /* Pulse acc input    */
        0,              /* Pulse acc overflow */
        toi_isr,        /* Timer overflow     */
        0,              /* Timer channel 7    */
        0,              /* Timer channel 6    */
        0,              /* Timer channel 5    */
        0,              /* Timer channel 4    */
        0,              /* Timer channel 3    */
        0,              /* Timer channel 2    */
        0,              /* Timer channel 1    */
        0,              /* Timer channel 0    */
        0,              /* Real time          */
        0,              /* IRQ                */
        0,              /* XIRQ               */
        0,              /* SWI                */
        0,              /* illegal            */
        0,              /* cop fail           */
        0,              /* cop clock fail     */
        (void *)0xff80, /* RESET              */
        };