[AVR] Receiving Data with using a State Machine

Status
Not open for further replies.

vitruvius

Newbie level 5
Joined
Apr 13, 2012
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Germany
Visit site
Activity points
1,342
Hello,

I am sending some data from one Atmega644P to another Atmega644P consecutively. The first three bytes are SYN(0x16), DLE(0x10), and STX(0x02). On the receiving part, I made a state machine to control if I receive those bytes correctly.

When it is in DLE_1_s state, it goes directly to the "else" statement, even though the data is 0x10. Why is this happening?

I am using Peter Fleury's uart library.

This is the flow chart diagram I am trying to implement:


This is the code:
Code:
int main(void)
{
	DDRD = 0b11111010;	//	PORTD input/output.
	DDRC = 0xFF;
	
	uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
	
	sei();

	unsigned int rec_char;
	char buffer[7];
	
    while(1)
    {
        rec_char=uart_getc();
        
        switch(state)
        {
	        case SYN_s:
				{					
					if ((unsigned char) rec_char == 0x16) // SYN
					{
						state=DLE_1_s;
					}
					else
					{
						state=SYN_s;
					}
				}
			break;
				
			case DLE_1_s:
				{
					if ((unsigned char) rec_char == 0x10) // DLE
					{
						state=STX_s;
					}
					else if ((unsigned char) rec_char == 0x16) // SYN
					{
						state=DLE_1_s;
					}
					else
					{
						state=SYN_s;
					}
				}
			break;
			
			case STX_s:
				{
					if ((unsigned char) rec_char == 0x02) // STX
					{
						state=TARGET_NO_1_s;
					}
					else if ((unsigned char) rec_char == 0x16) // SYN
					{
						state=DLE_1_s;
					}
					else
					{
						state=SYN_s;
					}
				}
			break;

Thank you.
 

If you take a look at the assembly code generated, you may see what is happening.

You could also add a second level of case statment under each state.
 

Unfortunately I do not know how to read the assembly code.

What is the second level of case statement for?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…