单片机串口自发自收

单片机串口自发自收

#include
unsigned char code led_code[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,
0x11,0xc1,0x63,0x85,0x61,0x71,0xff};
unsigned char dis_buf[4];
sbit led_clk= P1^6 ;
sbit led_data = P1^7 ;
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
sbit key4=P2^3;
unsigned char read_flag ;
unsigned char flag=0;
unsigned char TT[4],num;
unsigned char i,j;
void led_display ( )
{
unsignedchar t,i;
unsignedchar input_code;
for(i=0;i<4;i++)
{
input_code=dis_buf[ i];
for(t=0;t<8;t++)
{
if(input_code & 0x01 )
{
led_data=1;
}
else
{
led_data=0;
}
led_clk=0;
input_code>>=1;
led_clk=1;
}
}
}
void Send(unsigned char dat)
{
SBUF=dat;
while(TI0);
TI=0;
}
void recive(void) interrupt 4
{
if(RI)
{
RI=0;
TT[num]=SBUF;
num++;
if(num>=4)
{
num=0;
read_flag=1;
}
}
}
void delay_ms(unsigned int x)
{
unsigned int i;
unsigned char j;
for(i=x;i>0; i–)
for(j=110;j>0;j–);
}
void main(void)
{
TMOD=0x20;
SCON=0x50;
PCON=0x00;
TH1=0xfd;
TL1=0xfd;
EA=1;
ES=1;
TR1=1;
dis_buf[0]=led_code[16];
dis_buf[1]=led_code[16];
dis_buf[2]=led_code[16];
dis_buf[3]=led_code[16];
led_display();
while(1)
{
if(key1
0)
{
delay_ms(10);
if(key10)
{
i=0;flag=1;
}
}
else if(key2
0)
{
delay_ms(10);
if(key20)
{
i=4;flag=1;
}
}
else if(key3
0)
{
delay_ms(10);
if(key30)
{
i=8;flag=1;
}
}
else if(key4
0)
{
delay_ms(10);
if(key40)
{
i=12;flag=1;
}
}
if(flag)
{
for(j=i;i {
Send(led_code);
}
flag=0;
}
if(read_flag
1)
{
read_flag=0;
dis_buf[0]=TT[3];
dis_buf[1]=TT[2];
dis_buf[2]=TT[1];
dis_buf[3]=TT[0];
led_display();
}
}
}

你可能感兴趣的:(串口,自发自收,单片机,串口通信,缓存)