用单片机通过串口发送数据,数据格式为包头0x3A和3位有效数据(数据以十六进制格式),并且通过数码管6,7,8(或者1,2,3位)位显示串口发送的十进制有效数据的代码。
时间: 2023-04-05 13:01:02 浏览: 101
您好,以下是回答:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DIO=P2^0;
sbit CLK=P2^1;
sbit STB=P2^2;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_74hc595(uchar dat)
{
uchar a;
STB=0;
for(a=0;a<8;a++)
{
CLK=0;
DIO=dat&0x80;
dat<<=1;
CLK=1;
}
STB=1;
}
void send_data(uchar dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void main()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
while(1)
{
write_74hc595(table[0]);
delay(1000);
write_74hc595(table[1]);
delay(1000);
write_74hc595(table[2]);
delay(1000);
}
}
void ser() interrupt 4
{
uchar receive_data;
if(RI==1)
{
RI=0;
receive_data=SBUF;
write_74hc595(receive_data);
send_data(receive_data);
}
}
阅读全文