/*
* 红外接收数据,查询方式,并通过串口发送
*
* 晶振:11.0592M
*/
#include <reg52.h>
typedef unsigned char uint8;
sbit Ir_Pin = P3^3;
uint8 Ir_Buf[4]; //用于保存解码结果
/*
* UART初始化
* 波特率:9600
*/
void uart_init(void)
{
TMOD = 0x21; //此处使用两个定时器发生中断
SCON = 0x50;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}
/*
* UART发送一字节
*/
void UART_Send_Byte(uint8 dat)
{
SBUF = dat;
while (TI == 0);
TI = 0;
}
/*
* 获取低电平时间
*/
unsigned int Ir_Get_Low()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (!Ir_Pin && (TH0&0x80)==0); /*此处不明白为什么TH0要与上0x80*/
//此处过了30000多us就停止了!
TR0 = 0;
return (TH0 * 256 + TL0); //把十进制数转换一把
}
/*
* 获取高电平时间
*/
unsigned int Ir_Get_High()
{
TL0 = 0;
TH0 = 0;
TR0 = 1;
while (Ir_Pin && (TH0&0x80)==0);
TR0 = 0;
return (TH0 * 256 + TL0);
}
main()
{
unsigned int temp;
char i,j;
uart_init();
while (1)
{
start: //此数为goto语句的用法
while (Ir_Pin);
temp = Ir_Get_Low(); // 获取低电平的时间
if ((temp < 7833) || (temp > 8755)) //引导脉冲低电平8500~9500us
goto start;
temp = Ir_Get_High(); //获取高电平的时间
if ((temp < 3686) || (temp > 4608)) //引导脉冲高电平4000~5000us
goto start;
for (i=0; i<4; i++) //4个字节
{
for (j=0; j<8; j++) //每个字节8位
{
temp = Ir_Get_Low();
if ((temp < 184) || (temp > 737)) //200~800us
goto start;
temp = Ir_Get_High();
if ((temp < 184) || (temp > 1843)) //200~2000us
goto start;
Ir_Buf[i] >>= 1;
if (temp > 1032) //1120us
Ir_Buf[i] |= 0x80;
}
}
UART_Send_Byte(Ir_Buf[0]);
UART_Send_Byte(Ir_Buf[1]);
UART_Send_Byte(Ir_Buf[2]);
UART_Send_Byte(Ir_Buf[3]);
}
}
就是TH为什么非得与上0x80呢,求大家帮助一下啊,赶集了啊! |