Question

I have written a program to transmit char value serially for AT89S51. Its is working perfectly.

Program is given below:-

 #include<reg51.h>
 void main()
 {
 TMOD=0x20;
 TH1=0xFD;
 SCON=0x50;
 TR1=1;
 while(1)
 {
 SBUF='A';
 while(TI==0);
 TI=0;
 }
 }

In above code char 'A' is transmitted.

Now I want to transmit an integer value and I have written a program for it.

Program is given below:-

 #include<reg51.h>
 void main()
 {
 int i=61;
 TMOD=0x20;
 TH1=0xFD;
 SCON=0x50;
 TR1=1;
 while(1)
 {
 SBUF=i;
 while(TI==0);
 TI=0;
 }
 }

Above program is transmitting ' = ' (i.e decimal 61 corresponds to ' = ' character in ASCII).

I want to know how I can transmit an integer value.

Please guide me in this regard.

Was it helpful?

Solution

SBUF contains a single byte (i.e. char) to be transmitted. If you put 'A' there, that's what will be transmitted (in fact 0x41 will be transmitted, which corresponds to ASCII value of 'A'). When assigning a value of i into SBUF, it will be interpreted as byte regardless of type of i. This byte can be interpreted in any way the receiving party desires - it can treat it as integer or as ASCII value, it's the same as far as transmission goes; the difference is in the way the data is treated.

OTHER TIPS

Icepack is right if you only wanted to transmit a char or unsigned char, but if you really wanted to transmit more than 8 bits you will have to do it byte by byte. What you are trying to do requires putting bytes into the array, and sending them over line one byte at a time (SBUF in 8051 can only hold single TX and single RX value at a time). Than you have another issue, do you transmit most significant byte first, or last? Then you should ask yourself if you wanted to transmit binary data (just spit the bytes over the wire) assuming the other party knew your data format? Or do you want to work with strings, so that number '34567' will take for example five bytes (five ASCII codes) or more if you wanted some kind of terminator, line feed, or other non printable characters, while binary it would really be an integer taking two bytes on the 8051. As you can see, your question opens to many other questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top