Discussion in "Project Help" started by    Pater    Apr 20, 2018.
Fri Apr 20 2018, 08:10 pm
#1
Actually I want to transmit 3 bits serially using AT89C51 micro-controller.

I have written the code in ".c" file, using c programming trying to transmit the bits serially using SBUF register.

As the SBUF register is the single register available in serial programming of 89C51, it is not updating its value. The bit which is first transmitted is only repeated in the next 2 bits, it is not being updated and then transmitted.

So I want guidance regarding sending 3 bits(number / integer) using serial communication( with SBUF register).

Following is the code I have written,

#include<reg51.h>

void decimal();
void MSB();
void MIDDLE();
void LSB();

void main()
{
while(1)
{
decimal();

MSB();

MIDDLE();

LSB();


}

}

void decimal()
{
unsigned char x,bin,d1,d2,d3;

bin = Data_Bus;
x = bin /10;
d1 = bin %10;
d2 = x %10;
d3 = x/10;

}

void MSB()
{
unsigned char d1,a;
TMOD = 0X20;
TH1 = 0XFD;
SCON = 0X50;
TR1 = 1;
a = d1 +0x30;
SBUF = a;
while(TI==0);
TI = 0;

}

void MIDDLE()
{
unsigned char d2,b;
TMOD = 0X20;
TH1 = 0XFD;
SCON = 0X50;
TR1 = 1;
SBUF = 0x00;
b= d2 + 0x30;
SBUF = b;
while(TI==0);
TI = 0;
}


void LSB()
{
unsigned char d3,c;
TMOD = 0X20;
TH1 = 0XFD;
SCON = 0X50;
TR1 = 1;
SBUF = 0x00;
c= d3 + 0x30;
SBUF = c;
while(TI==0);
TI = 0;
}
Sun Apr 22 2018, 01:15 am
#2
I have attached sample code to do what you want.

There are a couple of points to note.

When you declare a variable in a function
it only exists in that function.
Variables d1,d2,d3 declared in void decimal() can't be
seen anywhere else.
When you declare d1 in MSB() it is a new variable (value 0 or unknown).

Declare d1,d2,d3 as global.

The serial port only needs to be set up once,
not every time it is used.

When functions do very much the same thing
as MSB(), void MIDDLE() and void LSB() do
find a way to use one call for all three.
Such as passing the variable to print.

sendChar(char c) does that.

Attachment


[ Edited Sun Apr 22 2018, 01:17 am ]
 Pater like this.
Mon Apr 23 2018, 08:43 am
#3
Thank You, ExperimenterUK for your valuable response.

You made it very clear for me to understand the errors and your sample code helped me to solve the issue.

Once again a big THANKS to you !!!
Tue Apr 24 2018, 02:21 am
#4
Thanks
glad it helped

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Michailqfh
Fri Mar 29 2024, 01:53 am
Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm