[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Global variables and ISRs ???

From: Vincent Trouilliez
Subject: [avr-gcc-list] Global variables and ISRs ???
Date: Tue, 06 Sep 2005 21:23:41 +0200

Hi gents,

After the highly technical thread that just ended today, I would like to
pick your brains on something much lower flying so to speak, sorry for
that ! ;-)

Problem : on my ATmega32, after several experiments (only just started
using it, learning...), I am suffering a big problem. It seems that when
I declare a variable as global, then modify it within an interrupt
routine, the 'main' function can't see that the variable has been
modified, as if it weren't a global variable... hmmm.

The concrete case at hand, was that I tried to implement a buffer for
incoming data on the UART. The ISR would fill the buffer and update the
buffer pointer accordingly. The program in 'main' was checking to see if
the pointer was non-zero and if yes, would send characters back to the
serial port for me to see/check.
I proved that all was working fine, interrupts etc, but somehow, 'main'
would always see the buffer pointer as zero (hence would never react),
as if the ISR didn't update it. 

I simplified the program as much as I could, and the problem is still
there. Could anyone have a look at the code below ?? 
It receives characters on the serial port, toggle sa LED to prove that
the ISR did get serviced, and sets flag to try and communicate with the
'main' function.... which fails. 'main' never sees the flag as
set.... :-/ Any help welcome, because I started scratching my head an
hour ago, and now I don't have many hair left to pull !! ;-)


Vince, extremely annoyed...

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

unsigned char flag, bin;  //global variables

void ioinit (void){

        DDRD = 0x20;    //PD5 as output for the LED
        PORTD = 0x00;   //turn LED off at boot.
        //set up USART
        UBRRH = 0x00;
        UBRRL = 12;             //Baud Rate = 9600
        UCSRA = 0x02;   //mode asynchrone, fast clock
        UCSRC = 0X86;   //asynchronous, no parity, one stop bit, 8 data bits
        UCSRB = 0x98;   //transmitter and receiver enabled, interrupts on
        flag = 0;       


        bin = UDR; //read incoming character to clear interrupt
        PORTD = ~PORTD; //toggle LED to prove ISR was serviced                  
        flag = 1; //tell 'main' about the event

int main (void){

        ioinit ();
                if (flag){
                        UDR = 'A';  //print a character to prove that the flag 
was set.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]