Electronic – how to clear an UART interrupt flag manually

atmegaatmelcmicrocontroller

I'm using an ATmega32. I have ISR(USART_RXC_vect) as follows

ISR(USART_RXC_vect)
{
    char ReceivedChar ;     
    ReceivedChar = UDR; // Fetch the received byte value into the variable "ReceivedChar"

    if(ReceivedChar == '\n'){       
        RxBuffer[RxPos] = '\0' ;                    
        RxReady=0;
        USART_Cmd_Eval();            
    }
    else{
        RxBuffer[RxPos] = ReceivedChar;
        RxPos++; 
    }
}

After triggering the ISR, it calls the following function to define which command is received from the PC.

void USART_Cmd_Eval(void)
{
            strcpy(RxCommand,RxBuffer);

            if(strcmp(RxCommand, "c") == 0){
                    RxReady = 1;                     
                    ADC_measure();
            }   
}

For each command, the relative function will be called. Like following:

void ADC_measure(void)
{
        while(RxReady ==1)
        {
                _delay_ms(50);
                // Measrung ADC values and send them to USART
        }
}

The problem is: I can't send another command because it is stuck in the ADC_measure function. Basically, it doesn't want to receive another command though ISR, I think it will continue open. Therefore, I think I should clear the interrupt flag before calling ADC_measure. Right? How can do this?

Best Answer

When any interrupt routine is executed then I bit of SREG is cleared to avoid next interrupt execution. The bit is reset by RETI. I.e. you can do it also manually SBI SREG,I (or via uint8_t sreg = SREG; sreg |= _BV(I); SREG = sreg;).

But also USART_RXC_vect may be called again! I don't like any _delay() in interrupt routines as it must be as short as possible. You should recode it.