Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
unnamed
PostPosted: Aug 04, 2011 - 04:04 PM
Rookie


Joined: May 10, 2011
Posts: 27


Hi, i have a little problem with my usart communication, please look at my code:

Code:

int main (void)
{
   Config32MHzClock();
   usartInit();
   rxn = -1;
   timersInit();
   unsigned char PWM_value=0;
   stdout = &mystdout;
   servo[1] = 42;
   servo[2] = 41;   
   servo[3] = 42;   

   for(;;){
      TC_SetCompareA( &TCD0, servo[1] );
      TC_SetCompareC( &TCD0, servo[2] );
      TC_SetCompareA( &TCD1, servo[3] );
      break;
   }
   
   while(1) {
      
      if(flag == 1){
         printf("Buffer i = s \n", rxn, buffer);
         rxn++;
         flag = 0;
         //servo control
         
         if(buffer[5] != '\0'){

            se1[0] = buffer[0];
            se1[1] = buffer[1];
            se2[0] = buffer[2];
            se2[1] = buffer[3];
            se3[0] = buffer[4];
            se3[1] = buffer[5];

            printf("Se1 = s \n", se1);
            printf("Se2 = s \n", se2);
            printf("Se3 = s \n", se3);   
            
            servo[1] = atoi(se1);   
            servo[2] = atoi(se2);   
            servo[3] = atoi(se3);   
            
            for(i = 0; i < 7; i++){
               buffer[i] = '\0';
            }   
            rxn = 0;
         }            
      }
      
      TC_SetCompareA( &TCD0, servo[1] );
      TC_SetCompareC( &TCD0, servo[2] );
      TC_SetCompareA( &TCD1, servo[3] );
      _delay_ms(500);
      
   }
}


ISR(USARTC0_RXC_vect)
{
   USART_RXComplete(&USART_data);
   if (USART_RXBufferData_Available(&USART_data)) {
         receivedData = USART_RXBuffer_GetByte(&USART_data);     
         if(rxn == 7){   
            rxn = 0;
         }
         buffer[rxn] = receivedData;
         flag = 1;
   }   
}

ISR(USARTC0_DRE_vect)
{
   USART_DataRegEmpty(&USART_data);
}


so, i'am sending numbers to my xmega and when buffer is full i'am trying to split them into 3, 2 digit numbers and use atoi(), but i'am getting wierd things in my se1 (char se1[2]), terminal shows this:

Code:

Buffer 0 = 1
Buffer 1 = 12
Buffer 2 = 123
Buffer 3 = 1234
Buffer 4 = 12345
Buffer 5 = 123456
Se1 = 1234
Se2 = 34
Se3 = 56


why se1 shows 1234 ? it should be 12 only... i have tried everything and i'am feeling so stupid right now, how from these lines

Code:

            se1[0] = buffer[0];
            se1[1] = buffer[1];
            se2[0] = buffer[2];
            se2[1] = buffer[3];
            se3[0] = buffer[4];
            se3[1] = buffer[5];


se1 can be 1234 ?

thanks for any help
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Aug 04, 2011 - 04:17 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England

Try
Code:
            se1[0] = buffer[0];
            se1[1] = buffer[1];
            se1[2] = 0;
            se2[0] = buffer[2];
            se2[1] = buffer[3];
            se2[2] = 0;
            se3[0] = buffer[4];
            se3[1] = buffer[5];
            se3[2] = 0;
Remember that C strings need to be 0 terminated! Wink

(BTW obviously seN[] need to be dimensioned to hold THREE not two bytes!)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
unnamed
PostPosted: Aug 04, 2011 - 05:26 PM
Rookie


Joined: May 10, 2011
Posts: 27


thanks, it's working right now Smile but that wasn't only error, for some wierd reason winavr wasn't updating my hex file, had to change it in build configuration to different name and now its working like a charm Smile

thank you Clawson Smile
 
 View user's profile Send private message  
Reply with quote Back to top
N34000
PostPosted: Aug 27, 2011 - 09:43 AM
Newbie


Joined: Jan 10, 2010
Posts: 1


hello friend's
i need a sample code for interupt usart
can help me ?
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Aug 27, 2011 - 09:47 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18599
Location: Lund, Sweden

Quote:
i need a sample code for interupt usart
can help me ?

Don't be clueless. There is an example in the Tutorials forum. Go over there and search it out.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits