Welcome to EDAboard.com

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

Status
Not open for further replies.

cllunlu

Member level 4
Joined
Nov 21, 2006
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,900
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
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,063
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
Joined
Jul 4, 2009
Messages
15,514
Helped
5,026
Reputation
10,076
Reaction score
4,861
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
131,500
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_luis

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,377
Helped
1,171
Reputation
2,361
Reaction score
1,164
Trophy points
1,403
Location
Brazil
Activity points
54,594
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
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Activity points
0
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
Joined
Sep 1, 2015
Messages
179
Helped
6
Reputation
12
Reaction score
4
Trophy points
18
Location
VietNam
Activity points
1,357
Dear Sir,

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

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

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,409
Helped
393
Reputation
786
Reaction score
413
Trophy points
1,363
Activity points
16,509
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
Joined
Apr 17, 2014
Messages
21,980
Helped
4,591
Reputation
9,196
Reaction score
4,808
Trophy points
1,393
Activity points
145,107
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.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top