ISR(TIMER1_CAPT_vect)
{
g_TimeBuffer = ICR1; //读取捕捉到的时间值
if (g_count == 0)
{
TCNT1 = 0; //初始化计数器的值,以便捕捉用
TCCR1B |= (1<<ICES1); //设置成上升沿触发
g_count++;
return;
}
if (g_count == 1) //高电平触发后
{
TCNT1 = 0;
TCCR1B &= ~(1<<ICES1); //设置成下降沿触发
Low_cnt = g_TimeBuffer; //保存低电平的时间宽度
g_count++;
return;
}
if (g_count == 2) //低电平触发后
{
TCNT1 = 0;
TCCR1B |= (1<<ICES1); //设置成上升沿触发
High_cnt = g_TimeBuffer;//保存高电平的时间宽度
g_count = 1;
//========已经接收到一个完整的低电平和搞电平时间宽度,根据宽度判断码是0或1或引导码==========
//判断是否是引导码
if (((Low_cnt > 8000)&&(Low_cnt < 10000))&&((High_cnt > 3500)&&(High_cnt < 5500)))
{
Number_cnt = 0;
IrData = 0;
}
//判断是否是"0"码
else if (((Low_cnt > 460)&&(Low_cnt < 660))&&((High_cnt > 460)&&(High_cnt < 660)))
{
IrData >>=1;
IrData &= ~0x80000000;
if (Number_cnt == 31) //判断是否是最后一个码
{
g_bit_rec_complete = 1;
Number_cnt = 0;
g_count = 0;
Low_cnt = 0;
High_cnt = 0;
TCCR1B &= ~(1<<ICES1); //设置成下降沿触发
return;
}
Number_cnt++;
}
//判断是否是"1"码
else if (((Low_cnt > 460)&&(Low_cnt < 660))&&((High_cnt > 1590)&&(High_cnt < 1790)))
{
IrData >>=1;
IrData |= 0x80000000;
我在这里从串口输出键码Send_Char(IrData );
if (Number_cnt == 31) //判断是否是最后一个码
{
g_bit_rec_complete = 1;
Number_cnt = 0;
g_count = 0;
Low_cnt = 0;
High_cnt = 0;
TCCR1B &= ~(1<<ICES1); //设置成下降沿触发
return;
}
Number_cnt++;
}
}
}
输出一堆乱码
而且不是每次都能响应,这是个红外解码程序 |