I have a need for an embedded device to stay in a function for one half a second to allow another device to catch up. I used the 1 millisecond timer(r_cg_cmt.c for Renesas IAR IDE) and had a static volatile int delay_time to increment once every 1 millisecond timed interrupt.
using the code:
//counter.c
//on 1 mS timed interrupt
gen_purpose_1ms_couter();
void gen_purpose_1ms_couter_reset( void ){
gen_c->count = 0;
}
void gen_purpose_1ms_couter( void ){
gen_c->count++;
}
//source_file.h
struct gen_counter {
unsigned int count;
};
extern struct gen_counter *gen_c;
//source_file.c
#include "counter.h"
struct gen_counter *gen_c;
gen_purpose_1ms_couter_reset();
while(1){
if(gen_c->count > 500){
break;
}
}
However this cause the processor (RX231) to stay in the while loop and NOT execute any timed interrupts. Can anyone explain how I can stay in a function for a give amount of time?
You didn't provide a complete example but here are a few possible reasons your code is not working as expected.
gen-c->count
is not re-read each iteration because it is not declared volatile.Does a break point in gen_purpose_1ms_couter
ever hit? If not then the timer or interrupt is not setup correctly.
Does your code start to work if you disable optimizations? If so then genc->count
needs to be declared volatile.
Thank you for your response the 1mS timer hits as it should during normal operation. This delay is within an interrupt which I am just now told is a very bad idea as interrupts should be very fast. I moved it to simply setting a flag and once the interrupt finishes it starts the delay. This was mostly a rookie mistake but thank you for your help. It is appreciated.
@WilliamDussault Not only is it a bad idea to have a delay inside an interrupt: other interrupts are disabled when you execute an ISR, so the timer interrupt cannot execute. You've created a deadlock.
Thanks. I spent a few hours digging through the data sheet trying to cement the fact that I deadlocked the processor.