我准备把红外接收器接受的红外遥控器键码传到计算机上,红外接收部分经过验证可以正确运行,不知道串口传输部分出现了什么问题,不能发送键码到计算机上,请高手帮忙看看出了什么问题?
代码如下:
#include<reg52.h>
sbit IR_INPUT = P3^3;
bit irflag = 0;
unsigned char ircode[4],mycode[6];
void InitInfrared()
{
IR_INPUT = 1;
TMOD &= 0x0f;
TMOD |= 0x10;
TR1 = 0;
ET1 = 0;
IT1 = 1;
EX1 = 1;
}
unsigned int GetHighTime()
{
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(IR_INPUT)
{
if(TH1 >= 0x40)
{
break;
}
}
TR1 = 0;
return(TH1 * 256 + TL1);
}
unsigned int GetLowTime()
{
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(!IR_INPUT)
{
if(TH1 >= 0x40)
{
break;
}
}
TR1 = 0;
return(TH1 * 256 + TL1);
}
void EXINT1_ISR() interrupt 2
{
unsigned char i,j;
unsigned char byt;
unsigned int time;
time = GetLowTime();
if((time < 7833) || (time > 8755))
{
IE1 = 0;
return;
}
time = GetHighTime();
if((time < 3686) || (time > 4608))
{
IE1 = 0;
return;
}
for(i = 0; i < 4; i++)
for(j = 0; j < 8; j++)
{
time = GetLowTime();
if((time < 313) || (time > 718))
{
IE1 = 0;
return;
}
time = GetHighTime();
if((time > 313) && (time < 718))
{
byt >>= 1;
}
else if((time > 1345) && (time < 1751))
{
byt >>= 1;
byt |= 0x80;
}
else
{
IE1 = 0;
return;
}
ircode = byt;
}
irflag = 1;
IE1 = 0;
}
void ConfigUART(unsigned int baud)
{
SCON = 0x50;
TMOD &= 0x0f;
TMOD |= 0x20;
TH1 = 256 - (11059200/12/32) / baud;
TL1 = TH1;
ET1 = 0;
TR1 = 1;
}
void main()
{
EA = 1;
InitInfrared();
ConfigUART(9600);
while(1)
{
if(irflag)
{
irflag = 0;
SBUF = ircode[2];
while(!TI);
TI = 0;
}
}
}转载
|