|
程序如下:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char flag,a,i,b,b1;
static unsigned char data CN[4];
static unsigned char data CT;
uchar code seg7[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
uchar code table[]="OK ";
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
sbit buzzer=P0^6;
void delay1(uchar z)
{
while(z--);
}
uchar ASCII2HEX(uchar ASCIIData) //接收到的校验和作为Ascii码,需转换成16进制
{
uchar HexData;
if (ASCIIData==0x30)
{
HexData=0x00;
}
else if (ASCIIData==0x31)
{
HexData=0x01;
}
else if (ASCIIData==0x32)
{
HexData=0x02;
}
else if (ASCIIData==0x33)
{
HexData=0x03;
}
else if (ASCIIData==0x34)
{
HexData=0x04;
}
else if (ASCIIData==0x35)
{
HexData=0x05;
}
else if (ASCIIData==0x36)
{
HexData=0x06;
}
else if (ASCIIData==0x37)
{
HexData=0x07;
}
else if (ASCIIData==0x38)
{
HexData=0x08;
}
else if (ASCIIData==0x39)
{
HexData=0x09;
}
return HexData;
}
void display(uint mm)
{
uchar m1,m2,m3;
m1=mm/100;
m2=mm%100/10;//个、十位分离
m3=mm%100%10;
P0=seg7[m1];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x02;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
P0=seg7[m2];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x04;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
P0=seg7[m3];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x08;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
}
void serinit() //初始化
{
TMOD=0x20;
TH1=0xf3;
TL1=0xf3;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
}
uint serial()
{
if(flag)
{
ES=0;
for(i=0;i<2;i++)
{
SBUF=table[i];
while(!TI);
TI=0;
}
b=ASCII2HEX(CN[0]);
b1=ASCII2HEX(CN[1]);
b=b*10+b1;
CT=0;
ES=1;
flag=0;
}
return b,b1;
}
void main()
{
buzzer = 0; //高有效
P25=1;
P26=0;
P27=1;
P25=0;
P26=0;
P27=0;
serinit();
while(1)
{
serial();
display(b);
}
}
void ser() interrupt 4 //接收成功后自动进入串口中断程序
{
if(RI)
{
CN[CT] = SBUF; //将寄存器数据传递给临时寄存器。
CT++;
RI=0; //串口中断为手动置0恢复
}
flag=1; //标志位置一
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|