I write a code (for Atmega168P) for receive data from USART interrupt as below:
ISR (USART_RX_vect)
{
unsigned char count=0;
unsigned char coder[13];
int over_uart=0;
for (count=0;count<13;count++)
{
while (!recive_485)
{
over_uart++;
wdt;
if (over_uart>=20000) {
coder[0]=0;
count=14;
over_uart=0;
break; }
}
coder[count]=UDR0;
}
}
Each time that i receive 13 byte of data and put in coder
array.
I use a variable over_uart
for calculate time between each of received byte. if over_uart>=20000
, means that:
The receive progress is start but we have problem in lines that do not get next byte in 20000ms And terminate the last command and look for new 13byte that will receive from line.
Any body know a optimized way that find delay between receive data from USART (without help of over_uart
variable)?
EDIT:
I correct the problem in my code and remove put delay in ISI as blow:
ISR (USART_RX_vect)
{
coder[count]=UDR0;
if (coder[count] == 20)
UartFlag=True;
else
count++;
}
And in Main
function:
while (1)
{
if (UartFlag)
{
DoSomthing();
count=0;
UartFlag=Fulse;
}
}
coder
, then exit the ISR. Wait for the next interrupt, repeat until you've put 'enough' bytes intocoder
or whatever other condition you need (like a timer expiring). Don't sit waiting inside the ISR for more stuff to happen. \$\endgroup\$