Вопрос

My program contains few global variables , whose values are set during the interrupt service routine (USCI_A0_ISR()) execution.

Once the execution of USCI_A0_ISR() is done , will the global variables hold the value assigned or will be set back to void/0.????

//Global variables
int ptr = 0;
char rxBuffer[16]; 
int flag = -1;
int check[2];

void __set_flag(void)
{

    if (strcmp(rxBuffer,"OK") == 0) flag = 0; 
    else if (strcmp(rxBuffer,"CONNECT") == 0) flag = 1;
    else if (strcmp(rxBuffer,"NO CARRIER") == 0) flag = 3;
    else if (strcmp(rxBuffer,"ERROR") == 0) flag = 4;

}

void __GSM_client(void)
{
    while (flag == -1);
    if (flag == 0) check[0] = buflen(rxBuffer);
}

void main(void)
{
    __Buffer_init();
    __low_level_init();         //WDT 
    __UART0_init();                 //UART 

    __bis_SR_register(GIE);       //interrupts enabled
    __delay_cycles(1000);           // wait till UART intial

    __GSM_client();

    __no_operation();             // For debugger
}

#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
    char byte;
    while (!(UCA0IFG&UCTXIFG));             
    byte=  UCA0RXBUF;
    UCA0TXBUF = byte;
    if (byte == '\r') { 
        //push_char(byte);
        ptr = 0;
        __set_flag();
        //__Buffer_init();              
    }
    else{                       
        push_char(byte);
    }
}

Here is the code snippet of what I am doing. I am setting the "flag" based on the response obtained . When I see the register view in Code Composer Studio , the "flag" value is set correctly , but if try using the value of "flag" elsewhere the value of "flag " is not reflected.

Any pointers over concepts of the interrupt service routine or when loopholes in my coding method appreciated Thanks in Advance AK

Это было полезно?

Решение

Within the interrupt, you are directly or indirectly changing several global variables, e.g. ptr, flag, and I'm assuming rxBuffer[?]. They are not declared "volatile" so their value may or may not change when you return from the interrupt. This is a bug because the behavior can change based on where in the execution of the code the interrupt occurs and what the level of optimization is. As a rule of thumb, any variable modified by an interrupt routine should always be declared volatile.

Другие советы

If you're sure that making the shared variables volatile isn't working then I'd suspect you have redefined a global variable as a local variable somewhere. Check the address of the flag variable when you are debugging and make sure it is the same in __set_flag() and outwith the interrupt, where you think it has not been updated.

I also think that the polling loop in your ISR is poor code and you should find a better way to wait for the transmitter to be ready for the next character.

Thanks to all the feedback i got from the members. Well the idea of declaring all the "variables volatile" did the trick . strcmp() uses const var* so i couldn't use it . I had to write my own custom string compare function. All this minor things solved my problems.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top