0
votes

I am using ATMEGA128 microcontroller and AVR studio for my project. I am using a Receive interrupt ISR_USART0 for receiving 8 bytes of data as a data packet and this interrupt is called after it finishes receiving data the data is used to stimulate some actuators. Here is the Interrupt routine. Now i want to add a timeout of 10 ms in this routine such that as soon as the first byte is received it starts counting the timeout and this lasts for 10 ms and then the software skips waiting for the bytes and returns to the main loop. Being a newbie i found very less data on interrupt on implementing such a timeout inside the interrupt routine...can anyone suggest the best way to implement it.?

ISR(USART0_RX_vect)
{   
    uint8_t u8temp;
    u8temp=UDR0;
    data_count++;    
    //UDR0=u8temp;
    //check if period char or end of buffer
    if    (BufferWrite(&buf, u8temp)==1){
           Buffer_OverFlow_ERROR=1; TRANSMISSION_ERROR [6]=Buffer_OverFlow_ERROR;
           MT_ERROR_FLAGS_INIT();BufferInit(&buf);data_count=0;REC_ERROR_FLAG=0;}  

    if (u8temp==0x58){
         BufferSort(&buf);BufferInit(&buf);
         REC_ERROR_FLAG=0;data_count=0;//PORTA|=(1<<PORTA3);
        }

        /*else if{
        REC_ERROR_FLAG=0;BufferInit(&buf);*/


}
1
Unless the USART provides a hardware timeout, you need an additional timer (software or hardware). However, that is too broad for stack overflow. Do some research on your own.too honest for this site

1 Answers

-1
votes

I would use such a function to implement a receive of 8 bytes from the UART with a timeout of n ms.

char buf[8], c = 0;
unsigned int t = 65535, th = 65535;

do {
    if (UCSRA & (1 << RXC))
        buf[c++] = UDR;
    if (!th--) t--;
} while ((c < 8) && t);

I use two variables t and th here, because decrementing a single integer probably isn't enough to get a delay of 10ms.
Now you just need to adjust t to a value such that the loop completes in about 10ms (this depends mainly on your clock). You could probably develop some formula utilizing the macro F_CPU, i.e. t2 = F_CPU/1000; or similar.