SOLVED:unusual behaviour of if statement

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello! i noticed an unusual behaviour in my code earlier today i am receiving data and sending it back via bluetooth 

 

ISR(USART1_RX_vect)
{
	uint8_t temp_var =0;
	temp_var= UDR1;

 	if(temp_var > 10)
 		PORTA ^= 0b01000000;  //Toggle LED
	while(((UCSR1A&(1<<UDRE1))==0));
	UDR1 =temp_var;
}

 there is no problem in my communication because what ever i am sending i am getting back without any corruption but The led only toggles when i send in an Even number of string 

for example if i Send AA it toggles but when i send AAA it doesnt can anyone help me with this please? 

This topic has a solution.
Last Edited: Fri. Apr 19, 2019 - 09:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Check if the DOR1 (Data Overrun Flag) in UCSR1A is set.
You might be losing characters because your ISR has a a potential time-delay while(((UCSR1A&(1<<UDRE1))==0));

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it was not set though i found out what the problem was and Now i feel so STUPID :S

when i was sending odd number of character i.e. AAA they were actually AAA/n so because of even numbers of characters and no delay between toggling it was impossible to detect toggle. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your 'even number of string' might actualy be an 'odd number of characters with value > 10'.

eg. when you send AA you might actually be sending

A(0x41)  A (0x41)  CR(0x0d)  LF(0x0a)

 

Try one of

/* ignore any control character */
if (temp_var > 31)

/* ignore CR or LF */
if (temp_var != 13 && temp_var != 10)

 

EDIT - OK you solved it already