Continue to Site

Welcome to

Welcome to our site! 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.

PIC mikroC send long type variable data via uart

Not open for further replies.


Member level 4
Nov 21, 2006
Reaction score
Trophy points
Activity points
Hi Everyone,

I am trying send data via uart with mikroC compiler. I can send string or char data succesfully. But I couldnt send long type data.

Could you help?

Long data is 32 bits wide (4 bytes). You have to send the 4 bytes using the Lo, Hi, Higher and Highest functions of mikroC PRO.

Example is


assuming microC has sprintf() you can convert the long to a string and send that, e.g.
char text[50];
long int i=567;
text[] should contain the string "567"

Both the methods mentioned will work.
The generic way to do it is to consider the long as four bytes, end to end. Call them AABBCCDD. You can only send one byte at a time because the UART is only 8 bits wide so you need to extract each of the four 8-bit parts individually.

Using AABBCCDD as the example:
The least significant 8 bits can be isolated by ANDing the long with 0xFF, AABBCCDD & 0xFF = 0xDD.
The next most significant bits can be isolated with AABBCCDD & 0x0000FF00 then shifted so CC is in the lowest bits with >> 8.
The next most significant bits can be isolated with AABBCCDD & 0x00FF0000 then shifted so BB is in the lowest bits with >> 16.
The most sgnificant bits can be isolated with AABBCCDD & 0xFF00000000 then shifted so AA is in the lowest bits with >> 24.

If the data has to be sent most significant bits first, simply reverse the order of the four instructions.

You can also do it with a union where you declare one long and four bytes. Writing to the long will make each byte hold bits 31:24, 23:16, 15:8, 7:0 which you can then pass to the UART as 8-bit values.


Long data is 32 bits wide (4 bytes)

I presume that OP did not mean long as a numeric type, but as a string type. Assuming it is the case, once the number has a variable size should control array indexing by reaching the null terminator character.

If you want to send the long type as a string then you can do like this

char str[17];
unsigned long val = 0;

LongToStr(val, str);
strcat(str, "\r\n");

Dear Sir,

How about to receive a long type variable from PC and display on LCD?

if (UART1_Data_Ready() == 1)
              j = UART1_Read()-48;
These are codes just receiving a byte.

Just reverse the processes listed above to send the value as 4 binary 'bytes'.
However you need to be aware that the simple approach you outline above is subject to synchronisation issues: if it sees 'noise' and interprets that as a valid value then all of the subsequent values will be put into the wrong 'byte' of the resulting value; similarly, if you miss a value then you will probably be waiting for a while to receive the 4th 'byte'.

Hi, synchronize you
* either need a defined time of no transmission between sending a complete variable. At the receiver side you need a timeout to reset (synchronize) your receiving routine.

* or you need to transfer a known sync pattern. Best is to use a combination that is not valid ( or at least : not likely) with the variable to transfer. With long variables all combinations are valid (in opposite to float variables).


Not open for further replies.

Part and Inventory Search

Welcome to