Question: Send a 0-255 in 16 of any number, when the microcontroller received after the dynamic display on the digital tube, the baud rate is self-determined.
#include <reg52.h> #define UCHAR unsigned char #define UINT unsigned int sbit Duan =p2^6;
Sbit Wei =p2^7;
Uchar Code table[]={0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};
UCHAR Flag;
Uchar Bai,shi,ge;
UINT Num;
void Init ();
void display (Uchar Bai,char shi,uchar ge);
void delay (UINT XMS) {UINT I,j;
for (i=0;i<xms;i++) for (j=0;j<10;j++);
} void Main () {init ();
while (1) {if (flag==1) {es=0;
flag=0;
bai=num/100;
SHI=NUM%100/10;
ge=num%10;
/*sbuf=bai; while (!
TI);
ti=0;
Sbuf=shi; while (!
TI);
ti=0;
Sbuf=ge; while (!
TI);
ti=0;*/Es=1;
} display (bai,shi,ge);//If the function is written in the previous loop, it will go wrong, only one digit is displayed ....
}} void Display (Uchar Bai,uchar shi,uchar ge) {wei=1;
P0=0xfe;
wei=0;
P0=0xff;
Duan=1;
P0=table[bai];
duan=0;
P0=0xff;
Delay (2);
Wei=1;
P0=0XFD;
wei=0;
P0=0xff;
Duan=1;
P0=table[shi];
duan=0; P0=0xff;
Delay (2);
Wei=1;
P0=0XFB;
wei=0;
P0=0xff;
Duan=1;
P0=table[ge];
duan=0;
P0=0xff;
Delay (2);
} void Init () {tmod=0x20;
TH1=0XFD;
TL1=0XFD;
sm0=0;
Sm1=1;
Tr1=1;
Ren=1;
Ea=1;
Es=1;
} void Ser () interrupt 4 {num=sbuf;
flag=1;
ri=0; }
Question: Send any byte data from the computer in 9600bps, when the microcontroller receives the data, add a sequence number before this data and send it together with this data to the computer, when the serial number exceeds 255 zero.
#include <reg52.h>
#define UCHAR unsigned char
#define UINT unsigned int
uchar AA;
Uchar Num;
UCHAR Flag;
void Init ();
void Main ()
{
init ();
while (1)
{
if (flag==1)
{
flag=0;
es=0;
if (num==255)
num=0;
Sbuf=num;
while (! TI);
ti=0;
SBUF=AA;
while (! TI);
ti=0;
Es=1
;
}}} void init ()
{
tmod=0x20;
TH1=0XFD;
TL1=0XFD;
sm0=0;
sm1=1;
tr1=1;
ea=1;
Es=1;
Ren=1;
num=0;
}
void Ser () Interrupt 4
{
flag=1;
ri=0;
Aa=sbuf;
num++;
}