Thanks again for the help. That fixed the problem and now the interrupt is working properly. I have hit another wall that I simply can't get past. I know that an interrupt service routine should perform few operations and then return to the main function to perform the bulk of the operations. With that in mind I am trying to increment a variable each time the interrupt occurs. The problem is the variable's value doesnt seem to change inside the main function, but when looking at the value of the variable after compiling and running the program I can see that it's value changes as each interrupt occurs. I have pasted the code below. Since each interrupt is 64 microseconds I should be able to use the while loop in the main function to wait 3 seconds then turn on the leds. For some reason the program never gets past the while loop in the main function. I have stored the 64*counter into a variable and looked at its value and it always reads 0...meaning counter must always be reading zero. But when I look at the value of counter it has incremented to a large number. Not sure what has happened here but clearly I am missing something. I can get it to work by placing the code directly into the interrupt, but would rather keep the isr small. I am sure that it is something easy so please excuse my inability to solve this problem. Thanks again for all the help. -Erik-
#include <hidef.h> /* common defines and macros */
#include <mc9s12dt256.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dt256b"
global long counter; //variable used to increment each interrupt
int timer_period;
#define T (*(volatile long*) counter)
#pragma CODE_SEG __SHORT_SEG NON_BANKED
#pragma TRAP_PROC
#ifdef __cplusplus
extern "C"
#endif
void Timer_Interrupt(void){
//CRGINT = 0; //disable interrupt
counter=counter + 1;
//CRGINT=128; //enable interrupt
/
}
#pragma CODE_SEG DEFAULT
//this function will initialize the rti timer.
//the function expects a timer period value in microseconds.
//ONLY the listed periods can be achieved.
void Timer_Initialize(int period_in_us){
/* Possible interrupt periods
64 us
128 us
192 us
256 us
320 us
384 us
448 us
*/
if (period_in_us == 64){ RTICTL = 16;}
else if (period_in_us == 128){ RTICTL = 17;}
else if (period_in_us == 192){ RTICTL = 18;}
else if (period_in_us == 256){ RTICTL = 19;}
else if (period_in_us == 320){ RTICTL = 20;}
else if (period_in_us == 384){ RTICTL = 21;}
else if (period_in_us == 448){ RTICTL = 22;}
timer_period = period_in_us;
}
void Start_Timer(void){
//turn on timer
Timer_Initialize(timer_period);//this ensures that the timer is reset
//at the begining of the timing period. For example the timer could
//be at 32 us and then when we set the counter=0 will get a value of
//counter = 1 but only 32us will have elapsed.
CRGINT = 128;//set RTIE flag.
counter = 0;
}
void Stop_Timer(void){
//turn off timer
RTICTL=0;
CRGINT=0;
}
long Read_Timer(void){
//returns the current timer value
long buffer;
buffer = timer_period * counter;
return buffer;
}
void Delay_us(long delay_time){
Start_Timer();
long Time_2_Delay;
//Time_2_Delay = delay_time*counter;
while (Time_2_Delay < delay_time){
Time_2_Delay= delay_time*counter;
}
Stop_Timer();
}
volatile static long temp;
void main(void) {
/* put your own code here */
counter = 0;
i=0;
EnableInterrupts;
Timer_Initialize(64);
//delay
Start_Timer();
//should wait for 3 seconds then turn on the leds with ddrb and portb below.
while ((counter*64) < 3000000){
}
DDRB=0xF0;
PORTB=0x10;
for(;
{} /* wait forever */
/* please make sure that you never leave this function */
}