|
比如发“12-34”可显示却是“1244”丢了三怎么回事,求高手解析
#include <reg52.h>
#include <intrins.h>
#include<absacc.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char a,i,b,b1,a1,a2,b2,n,m,c,d;
static unsigned char data CN[10];
static unsigned char data CT;
uint Timer0_t;Timer0_cont;
typedef bit BOOL ;
BOOL flag;
static uint Timer0_Spac=50; //延时0.5秒
sbit DQ=P1^4;
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--);
}
void display(uint mm)
{
P0=seg7[mm];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x02;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
}
void display1(uint mm)
{
P0=seg7[mm];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x04;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
}
void display2(uint mm)
{
P0=seg7[mm];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x20;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
}
void display3(uint mm)
{
P0=seg7[mm];
P2 |= 0xE0;
P2 &= 0x1F;
P0 = 0x40;
P2 |= 0xC0;
P2 &= 0x3F;
delay1(10);
}
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 serinit() //初始化
{
TMOD=0x21;
TH0 =0xd8; //赋T0的预置值0x1000,溢出30次就是1秒钟
TL0 =0xf0;
TH1=0xf3;
TL1=0xf3;
TR1=1;
REN=1;
RI=0;
TR0=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=b1;
a=b2;
c=a1;
d=a2;
ES=1;
flag=0;
}
return a,b,c,d;
}
void COMOUT(uchar Data)
{
SBUF=Data;
while(!TI);
TI=0;
}
void main()
{
buzzer = 0; //高有效
P25=1;
P26=0;
P27=1;
P25=0;
P26=0;
P27=0;
serinit();
while(1)
{
serial();
display(a);
display1(b);
display2(c);
display3(d);
}
}
void ser() interrupt 4 //接收成功后自动进入串口中断程序
{
if(RI)
{
Timer0_cont=0;
CN[CT] = SBUF; //将寄存器数据传递给临时寄存器。
if (CT>4)
{
CT=0;
}
CT++;
RI=0;
COMOUT(SBUF);
COMOUT(CT);
if(CN[2]==0X2D && CT==4)
{
b2=ASCII2HEX(CN[0]);
b1=ASCII2HEX(CN[1]);
a2=ASCII2HEX(CN[3]);
a1=ASCII2HEX(CN[4]);
for(i=0;i<10;i++)
{
CN[i]=0;
}
CT=0;
flag=1; //标志位置一
// serial();
}
}
}
void timer0_int(void) interrupt 1 // 定时器0中断是1号
{
uchar i;
// TR0=0;
TH0 =0xd8;
TL0 =0xf0;
Timer0_cont++;
if (Timer0_cont>=Timer0_Spac) //串口数据定时复位
{
TR0=0;
for (i=0;i<10;i++)
{
CN[i]=0;
}
CT=0;
Timer0_cont=0;
/// ES=1;
}
TR0=1;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|