interrupt using 16-bit timer

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

interrupt using 16-bit timer

Jump to solution
1,248 Views
MO_84
Contributor III

Hello all,

 

I have purchased a dragon12 board and I'm programming in C using CW. I'm having problems with the interrupts. I'm trying to initiate and interrupt when the 16 bit timer overflows. Stepping through the code, it seems that no interrupts are initiated and program runs in the infinite loop in the main function.

 

I have attached my code in a text file. It’s only a few lines.

 

Please help.

 

 

 

#include <hidef.h>           /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */

volatile unsigned int n;
volatile unsigned int x;
void Timer_Init(void);

void main(void)
{
     
    DDRB = 0xFF;    //PORTB as output since LEDs are connected to it
    DDRJ = 0xFF;    //PTJ as output to control Dragon12+ LEDs
    PTJ=0x0;         //Allow the LEDs to display data on PORTB pins
    Timer_Init();      
    EnableInterrupts;
    while(1)
    {
 
      for(;:smileywink:;
    }
}

void Timer_Init(void)
{
    TSCR2=0x87; /*TCNT prescaler + enable interrupt */
    TSCR1=0x80;  //enable system timer
    TFLG2 = 0x80; /*clear Timer Interrupt Flag bit by writing a 1 to bit 7*/
}   

#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module.
Placement will be in NON_BANKED area. */

interrupt void TimerOverflow_ISR(void)
 {
      for(x=0;x<50000;x++)
      PORTB=0xFF;
      TFLG2=0x80; /* clear Timer Interrupt Flag bit by a write*/
      PORTB=0x00;
}




Labels (1)
0 Kudos
1 Solution
408 Views
MO_84
Contributor III

Thanks for your response.

 

No I hadn't done that originally. Once added "VECTOR ADDRESS 0xFFDE TimerOverflow_ISR" to the linker file it is working.

 

I also took your advice on not having loops in the ISR. I'm using flags instead.

 

interrupt void TimerOverflow_ISR(void)
{
    n++;
    if(n<500)
    PORTB=0xFF;
    TFLG2=0x80; /* clear Timer Interrupt Flag bit by a write*/
    if (n >= 500)
    PORTB=0x00;
    if (n == 1000)
    n = 0;
    TFLG2=0x80; /* clear Timer Interrupt Flag bit by a write*/
}

 

Thanks again for your help.

 

View solution in original post

0 Kudos
2 Replies
408 Views
kef
Specialist I

Did you setup timer overflow interrupt vector so that it points to TimerOverflow_ISR?

 

(Loops in ISR's is bad idea. ISR's must execute as fast as possible, keeping interrupt latency low (or allowing other interrupts to be serviced with as small delays as posiible))

0 Kudos
409 Views
MO_84
Contributor III

Thanks for your response.

 

No I hadn't done that originally. Once added "VECTOR ADDRESS 0xFFDE TimerOverflow_ISR" to the linker file it is working.

 

I also took your advice on not having loops in the ISR. I'm using flags instead.

 

interrupt void TimerOverflow_ISR(void)
{
    n++;
    if(n<500)
    PORTB=0xFF;
    TFLG2=0x80; /* clear Timer Interrupt Flag bit by a write*/
    if (n >= 500)
    PORTB=0x00;
    if (n == 1000)
    n = 0;
    TFLG2=0x80; /* clear Timer Interrupt Flag bit by a write*/
}

 

Thanks again for your help.

 

0 Kudos