Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

USART 16bit data

Status
Not open for further replies.

fusian

New Member
USART can only receive 8bit data, if my application is required user to enter a 8bit command byte, then follow by 10bit or 16bit data byte, how can it be done?? any suggestion??

the simple coding i written below can only store 8bit command byte in Parser_Command and 8bit data byte in Parser_Data....:eek:

Code:
WaitForData_Flag = 0;

 while(1)                    						
   {				 
	if(RCIF)
   	{
   	ReceiveByte = RCREG;
	      {
			if(WaitForData_Flag)

				WaitForData_Flag = 0;
				Parser_Data = ReceiveByte;
				DoProcess();
			else
				Parser_Command = ReceiveByte;
				WaitForData_Flag = 1;
		}		 
	}
 
AtomSoft said:
Like first 8
then next 8
then next 2 or next 8 and if its 2 fill the rest with 0

if the user is sending a, say 33279decimal(10000001 11111111) via USART, how will the USART send it??

is it 11111111, then follow by 10000001?
 
It's entirely up to you how you want to send it, personally if you're transferring decimal values back to a PC I prefer to use ASCII strings, followed by CR LF, so you have a clear indication of the start and finish of each number. But if speed is of a concern, then you can send it as binary.
 
Why not just combine the bytes,
Code:
int GetWord(){
int Temp;
    if(RCIF)
        Temp=RCREG;
    Temp=Temp<<8;
    if(RCIF)
        Temp|=RCREG;
    return(Temp);
}

Mike.
 
brilliant!! i get what you mean now...i just call GetWord function to receive a 2bytes data and the data can be accessed in var Temp. Am i correct?
 
Code:
unsigned int Parser_Data;
char ReceiveByte,Parser_Command;

WaitForData_Flag = 0;

 while(1)                    						
   {				 
	if(RCIF)
   	{
	      {
			if(WaitForData_Flag)

				WaitForData_Flag = 0;
				Parser_Data = GetData();

			else
				ReceiveByte = RCREG;
				Parser_Command = ReceiveByte;
				WaitForData_Flag = 1;
		}		 
	}		
}	

//-------------------------------------------------------------------
// GetData()
// Return 16bits data in Temp
//-------------------------------------------------------------------
unsigned int GetData()
{
unsigned int Temp;
    if(RCIF)
        Temp=RCREG;
    Temp=Temp<<8;
    if(RCIF)
        Temp|=RCREG;
    return(Temp);
}
from the coding above, would i manage to get 1byte command in Parser_Command and 2byte data in Parser_Data
 
Yes, the sending computer would have to send Command folloed by High byte of data and then the low byte.

You comment "// Return 16bits data in Temp" is still wrong, Temp is not valid outside of GetData. It simply returns an int.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top