Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

USART data comparision in atmega8 problem

Status
Not open for further replies.

hardik.patel

Member level 5
Joined
Aug 15, 2012
Messages
94
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,008
Hi friends,

i am trying to design rf based device control application using atmega8 n CC2500 Usart RF module. I completely aware to deal with hyperterminal of pc and microcontroller interfacing.
Compiler i used is Winavr n Proteous for simulation.

So in my case i had already finished tx and rx code. On which i successfully sent a string and successfully received that string on rx side.

Now i feeling problem on receiving side regarding to compare that received string with my predefined string. If both are match than i need to go further.

Initially i only want to do is...to compare tx string with rx string---Thats it!! Rest task will be handeled.
Inshort, i dont know how to handle string on rx side.

So for your reference here i attached my work.
 

Attachments

  • Modified serial rx tx both1234.rar
    124.4 KB · Views: 49

Hi,

Are you using yourown protocol with your own commands?
Then try to form your commands with equal length and with a delimiter. This makes parsing easier.
Maybe 4 capital letters folowed by a "*". Like "SEND*".

Then you could use a single byte comparison to wait for a "*", and a 4 byte buffer for the command parsing.
This can safe processing time and SRAM memory.
Don't delete the parsing buffer when command parsing doesn't match, but shift in the new received byte and loose the oldest byte like a ring buffer.

Uart input buffer should act like a FIFO and should be large enough to store all incoming data like command, delimiter abd parameters. Try to avoid command queueing to keep SRAM usage low.

Klaus
 

Hi klausst,

To define protocol is a later task but before that let me compare simple string on rx side...

Your suggestion is in my mind n will think about it later definately.

Regards
 

You need buffers,


choose a size
Code:
#define SIZE 64 //according your available memory
and make a buffer:
Code:
char rx_buffer[SIZE];
and a pointer:
Code:
int rx_ptr=0;

and every time you recieve a data fill the buffer
Code:
//this will be helpful later
#define USARTRxReady() (UCSRA & (1<<RXC))

//This function is used to read the available data
//from USART. This function will wait untill data is
//available.
char USARTReadChar()
{
   char c;
   //Wait untill a data is available

   while(!USARTRxReady())
   {
      //Do nothing
   }

   //Now USART has got data from host
   //and is available is buffer
  c=UDR; //read byte
  //push to buffer
   rx_buffer[rx_ptr]=c;
  if(++rx_ptr>SIZE)rx_ptr=0; //cheap circular buffer
   return c;
}

now you have a buffer where the string is stored, and use a comparison function to check if it's equal to your desired matching string

Code:
if(strstr(rx_buffer,"COMMAND")){
  //match found! do something and then clear buffer by setting rx_ptr to 0
  
  rx_buffer[0]=0; //clear buffer
  rx_ptr=0;
}else ...

if strstr returns 0 it didn't find the string, if it returns a value (it's the starting position where it found the matching string)
(i suggest you to read about the strstr_P variation, to optimize some memory bytes)

I used a circular buffer just in case, you need to make a timer or something to clear the buffer after a while if it didn't receive a correct command, (at least set rx_ptr to 0 again)

now, I suggest for your main program loop to:
Code:
   while(1)
   {
      //Read data
      if(USARTRxReady())
      {
          data=USARTReadChar();
         //test with the new data the commands
         if(strstr(rx_buffer,"COMMAND1"))
         {
             //match found! do something and then clear buffer
             rx_buffer[rx_ptr=0]=0;
         }
         else if(strstr(rx_buffer,"COMMAND12))
         {
             //match found! do something and then clear buffer
             rx_buffer[rx_ptr=0]=0;
         }//if no command found what to do?
	  
          //eco
         //USARTWriteChar('{');
         USARTWriteChar(data);
         //USARTWriteChar('}');
      }//endif
      //do the rest of stuff here, like the timer to clear the rx_ptr if no data has been received for a while...
}

if your compiler mess with the cast, try strstr( (char *)rx_buffer,"COMMAND"), but I doubt you'll have problem there...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top