Warm tip: This article is reproduced from stackoverflow.com, please click
c embedded iar

How do I continue to receive timed interrupts in an embedded C while loop?

发布于 2020-04-13 10:39:58

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?

Questioner
William Dussault
Viewed
55
kkrambo 2020-02-04 01:50

You didn't provide a complete example but here are a few possible reasons your code is not working as expected.

  1. The timer isn't configured/enabled correctly.
  2. The timer interrupt isn't enabled correctly.
  3. The interrupt handler isn't registered correctly.
  4. The while-loop has been optimized and 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.