Send integer to xmega via usart from flash

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

Hi, i'am working on a little project and i'am currently stuck on (i think) simple task.

How can i send 2 digit number (e.g. 67 or 23) to my xmega and how should i read it ?

for now i can succesfully send one char like 'a' or 'b' and get it back on the screen.
I'am sending it from flash as3 and i trace values like:

...
Send: 20
Buffer: 88888888888819999999999999999999 | 
Send: 22
Buffer: 999999999920000000000000000000000 | 
Send: 23
Buffer: 00022222222222222222222222222223 | 
Send: 24
Buffer: 33333333333333333333333333324444 | 
Send: 25
Buffer: 44444444444444444444425555555555 | 
Send: 30
Send: 31
Buffer: 55555555555555553000000000000000 | 
Send: 33
00000000003111111111111111111111 | 
Send: 34
Buffer: 11111111333333333333333333333333 |
... 

my xmega code looks like this (only usart part)

int main (void)
{
while(1) {

		if (USART_RXBufferData_Available(&USART_data)) {
			receivedData = USART_RXBuffer_GetByte(&USART_data);
		}	
		
		USART_TXBuffer_PutByte(&USART_data, receivedData);
		              

		
	}
}


ISR(USARTC0_RXC_vect)
{
	USART_RXComplete(&USART_data);
}

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

what is the good aproach to this task ?

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

Read characters to a buffer then use atoi() on it.

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

hi, so i'am fighting with atoi for a whole day now and i cant figure it out, whats wrong with my code ?

while(1) {

		if (USART_RXBufferData_Available(&USART_data)) {
			receivedData = USART_RXBuffer_GetByte(&USART_data);  
		}	
		
		if(receivedData != ""){	
			if(receivedData == ','){
				servo[1] = atoi(buffer);	
				buffer[i] = '\0'; 
				i = 0;
			}else{				
				if (i<32){
					buffer[i]=receivedData;
					USART_TXBuffer_PutByte(&USART_data, buffer[i]);
					i++;
				}else{
					i = 0;
				}
			}			
		}	
		
		receivedData = "";	
		TC_SetCompareA( &TCD0, servo[1] );
		
		_delay_ms(100);
		
	}

when i change atoi to for example 72 its working ok, so problem should be in this line, what am i missing ?

thanks for help and sorry for my noobish question :)

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

What does "buffer" contain when atoi() is called? - in other words just printf("%s", buffer) - what comes out?