帮忙看一下红外通信程序(NEC协议)
不知道这个标题是否符合要求啊,因为有比较多的问题,很难在标题上都体现出来,希望莫老大不要封号啊!!程序:
#include <reg52.h>
typedef unsigned char uint8;
sbit Ir_Pin = P3^3;
uint8 Ir_Buf; //用于保存解码结果
/*
* 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);
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:
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 >>= 1;
if (temp > 1032) //1120us
Ir_Buf |= 0x80;
}
}
UART_Send_Byte(Ir_Buf);
UART_Send_Byte(Ir_Buf);
UART_Send_Byte(Ir_Buf);
UART_Send_Byte(Ir_Buf);
}
}
问题:
1、为什么把定时0的TH0 TL0 设置为0?
2、while (Ir_Pin && (TH0&0x80)==0); 这个语句是什么意思?
3、那两个for循环是什么意思?
仅供参考:
1. 初值都是零,开始计数。
2. 电平为高 && 超时判断。
3. 得到具体的码值。 2楼已经给出了正解,小弟也刚看了这个代码,虽然没有实际下到开发板测试,但我看代码应该没什么问题,是小宋老师写的。你应该是C语言功底不行,好好先学一下C语言你就知道那两个 for 循环用来干嘛了。
页:
[1]