# PIC mikroC send long type variable data via uart

Status
Not open for further replies.

#### cllunlu

##### Member level 4
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?

#### milan.rajik

##### Banned
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

Code:
UART1_Write(Lo(dat));
UART1_Write(Hi(dat));
UART1_Write(Higher(dat));
UART1_Write(Highest(dat));

#### horace1

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

#### betwixt

##### Super Moderator
Staff member
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.

Brian.

#### andre_teprom

##### Super Moderator
Staff member
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.

#### milan.rajik

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

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

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

#### BiNa2605

##### Full Member level 3
Dear Sir,

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

Code:
if (UART1_Data_Ready() == 1)
{
BytetoStr(j,txt);
Lcd_out(1,5,txt);
}
These are codes just receiving a byte.

#### Aussie Susan

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'.
Susan

#### KlausST

##### Super Moderator
Staff member
Hi,

...to 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).

Klaus

Status
Not open for further replies.

Replies
3
Views
1K
Replies
4
Views
3K
Replies
15
Views
14K
Replies
0
Views
2K
Replies
0
Views
2K