Electronic – Send integer values through serial communication in PIC to PIC

microchipmicrocontrollerpicserial

I need to pass integer values from one PIC to another through serial port

for char I can use the following code

char data1='a';
TXREG=data1;                                     
while(PIR1.TXIF==0); 

but when I try to pass int type variable it returns an ASCII equivalent..

Is there anyway to receive the data as integer instead of char type?

Best Answer

You already are sending integer values over the UART with the code you show. Your problem is apparently sending integers that are more than 8 bits wide.

The UART inherently only sends 8 bit bytes (in the most common configuration) at a time. If you want to send a wider integer, you have to send more than one byte to represent that integer. For example, you might decide that all multi-byte integers are to be sent in least to most significant byte order. If you have a 16 bit integer to send, then you send the low 8 bits first, then the high 8 bits. The receiving PIC does the reverse. It receives the low 8 bits first, then the high 8 bits, and writes those consecutively in memory so that the rest of the system can access the value as a 16 bit integer.

24 bit integers, for example, are sent the same way except that 3 bytes are needed instead of 2. 32 bit integers require sending 4 bytes.

Note that none of this has anything to do with sending numeric values to show up on a terminal emulator. Terminals display characters. Each possible character has a pre-determined binary code. You send the byte containing the code for a character, and that character shows up on the terminal. For example, the code for the letter "A" is 65. If you send the byte 65, then a terminal will show "A". Likewise, sending 48 causes the terminal to show "0". Look up something called ASCII code. That will tell you what the byte values are for each of the characters the terminal can show.

Here is a example to illustrate all of the above. Suppose you wanted to send the 16 bit integer value 9525. Let's convert that to HEX so we can see the individual bytes easily: 2535h. You have previously decided to send multi-byte values in low to high byte order. The two bytes you will send are therefore 35h and 25h in that order. These have the decimal values 53 and 37. You send these bytes, and the other PIC receives 53 and 37. Note that (37 * 256) + 53 = 9525, which is the value you are transmitting.

If these bytes were to be intercepted by a terminal, then you'd get whatever characters map to 53 and 37, which happen to be "5" and "%". So the terminal will display "5%" when you send 9525. If you wanted the terminal to display "9525" you'd have to send the byte values for the characters "9", "5", "2", and "5". Those happen to be 57, 53, 50, and 53. That is a lot more complicated since the PIC has to figure out the decimal digits of the binary integer it has, convert them to the character codes for those digits, then send those.

Generally it's a lot easier to do any kind of user interface conversion on the PC and let the PIC send and receive native binary. Converting the binary integer 9525 to the characters "9525" is trivial for the PC, but can require substantial code space and cycles on a small PIC.