|
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6;
bit RXAAOK; //接收到0XAA开头标志
bit RXFRMOK; //接收一帧完整数据标
//接收中断中如果是完整一条数据置位,主函数中
//处理完毕清零
uchar RXBUF[32],TXBUF[32]; //接收缓存
uchar RX_P; //数据个数
uchar code leddata[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
void delay(uint z)
{
uint x,y;
for(x = z;x > 0;x--)
for(y = 114;y > 0;y--);
}
void display_PM2_5(uchar t)
{
uchar bai,shi,ge;
bai = t / 100;
shi = t / 10 % 10;
ge = t % 10;
we = 1;
P0 = 0xdf;
we = 0;
du = 1;
P0 = leddata[bai];
du = 0;
delay(5);
we = 1;
P0 = 0xbf;
we = 0;
du = 1;
P0 = leddata[shi];
du = 0;
delay(5);
we = 1;
P0 = 0x7f;
we = 0;
du = 1;
P0 = leddata[ge];
du = 0;
delay(5);
}
void display_PM10(uchar t)
{
uchar bai,shi,ge;
bai = t / 100;
shi = t / 10 % 10;
ge = t % 10;
we = 1;
P0 = 0xfe;
we = 0;
du = 1;
P0 = leddata[bai];
du = 0;
delay(5);
we = 1;
P0 = 0xfd;
we = 0;
du = 1;
P0 = leddata[shi];
du = 0;
delay(5);
we = 1;
P0 = 0xfb;
we = 0;
du = 1;
P0 = leddata[ge];
du = 0;
delay(5);
}
void UART_init()
{
TMOD = 0x20; //T1工作模式2 8位自动重装
TH1 = 0xfd;
TL1 = 0xfd; //比特率9600
TR1 = 1; //启动T1定时器
SM0 = 0;
SM1 = 1; //串口工作方式1 10位异步
REN = 1; //串口允许接受
EA = 1;
ES = 1;
}
void main() //主程序
{
UART_init();
while(1)
{
if(RXFRMOK)
{
display_PM2_5((TXBUF[0]+TXBUF[1]*256)/10);
display_PM10((TXBUF[2]+TXBUF[3]*256)/10);
RXFRMOK=0; //处理完毕,清除消息
}
}
}
void UARTO_ISR() interrupt 4 //接收中断
{
uchar i,j; //用处提取接收缓冲器的字符
if(RI)
{
i=SBUF; //提取字符
RI=0;
if(RXFRMOK == 0) //判断是否接收到起始字符0XAA
{
if(RXAAOK) //如果接收到0XAA,继续接收后面的字符
{
RXBUF[RX_P]=i; //字符存入数组
if((RX_P>3)&&(RXBUF[RX_P] == 0xab) ) //判断是是否接收完毕一帧数据
{
RXFRMOK=1; //如果接收完成,置位RXFRMOK;
RXAAOK=0; //清零开始标志位RXAAOK
}
RX_P++; //接收计数器加一
}
if(!RXAAOK&&(i==0xaa))
{
RXAAOK=1;
RX_P=0;
}
}
}
for(j = 0;j < 4;j++)
{
TXBUF[j] = RXBUF[j+1]; //提取中间四位
}
/*for(j = 0;j < 4;j++)
{
SBUF = TXBUF[j];
while(!TI);
TI = 0;
}*/
}
能帮我改改吗,收不到数据 |
|