Setting vars in ISRs and the main loop

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

I have written a simple BIOS for my ATMEGA8 that uses

ISR(TIMER2_COMP_vect)
ISR(USART_RXC_vect)

The controlling Solaris application can communiate with the AVR over a serial line. A specific command starts some magic with timer 2 in CTC mode. At the end of this magic a global variable _state is reset to 0 thus signaling the main loop that the experiment is over and C is to be sent via the serial interface to signal end of the experiment to the controlling Solaris software. My main loop looks as follows:

while (1)                       /* loop forever */
  {
          if (_notifyEndOfExperiment == 1 && _state == 0)
            {
            _notifyEndOfExperiment = 0;
            TxByte('C');
            TxByte(0x0D);
            }
  }

I know that the main loop runs since inserting code like

TxByte('V');
delay_ms(1000);

gives me a V on the serial line every second. I can also check the state of the variables

static uint8_t _notifyEndOfExperiment;
static uint8_t _state;

by sending another command to the BIOS that executes

TxByte('c');
putInt_in_serial_trans_buffer((int)_state);
TxByte(',');
putInt_in_serial_trans_buffer((int)_notifyEndOfExperiment);
TxByte(0x0D);

This gives me c0,1 so I know the state of these variables is as it should be. Nevertheless the body of

while (1)                       /* loop forever */
  {
          if (_notifyEndOfExperiment == 1 && _state == 0)
            {
            _notifyEndOfExperiment = 0;
            TxByte('C');
            TxByte(0x0D);
            }
  }

is never executed. I never see the C on the serial line. I am clueless. What am I missing? Any idea?

Thanks a lot!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I presme these variables get altered by the ISR()'s. So declare volatile:

volatile static uint8_t _notifyEndOfExperiment;
volatile static uint8_t _state; 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
I presme these variables get altered by the ISR()'s. So declare volatile:

volatile static uint8_t _notifyEndOfExperiment;
volatile static uint8_t _state; 

David.

Yes, they are. And yes, replacing static with volatile did the trick.

Thanks so much!!!