收藏0 举报
#define IR_IN GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) // PA1 红外接收DQ引脚 unsigned char ir_code[4]; // 解码值保存变量 unsigned char ir_decode_ok_flag = RESET; // 解码成功标志位 /* NEC红外编码: 引导码 + 地址码 + 地址码(取反) + 数据 + 数据(取反) 引导吗:0.56ms(低电平) + 2.25ms(高电平) 数据1: 0.56ms(低电平) + 1.12ms(高电平) */ // 红外解码程序,100us定期执行就可以,将该函数放在100us的定时器中即可。 void Ir_Decode(void) { static unsigned int l_cnt = 0; // 低电平时间计数 static unsigned int h_cnt = 0; // 高电平时间计数 static unsigned int l_cnt_save = 0; // 保存低电平时长 static unsigned int h_cnt_save = 0; // 保存高电平时长 static unsigned char falling_edge_valid_flag = RESET; // IR电平由高变低标志位 static unsigned char rcv_sync_ok_flag = RESET; // 同步码接收成功标志位 static unsigned char bit_value = 0; // 位解码值 static unsigned char bit_rcv_cnt = 0; // 位接收个数变量 if( RESET == IR_IN ) { if( 0 == l_cnt ) // IR由高变低后立马记录上次测得的高电平时长 { h_cnt_save = h_cnt; falling_edge_valid_flag = SET; } l_cnt ++; if( l_cnt > 1600 ) // 防止计数溢出 { l_cnt = 1600; } h_cnt = 0; // 计数清零 } else { if( 0 == h_cnt ) // IR由低变高后立马记录上次测得的低电平时长 { l_cnt_save = l_cnt; } h_cnt ++; if( h_cnt > 1600 ) // 防止计数溢出 { h_cnt = 1600; } l_cnt = 0; // 计数清零 if(ir_decode_ok_flag == 1) { if(h_cnt > 1200) ir_decode_ok_flag = 2; // 短按 } } if( SET == falling_edge_valid_flag ) { falling_edge_valid_flag = RESET; /* 位解码 */ if( ((l_cnt_save >= 3)&&(l_cnt_save <= 9)) && // 560us低电平, 560us高电平 ((h_cnt_save >= 3)&&(h_cnt_save <= 9)) ) { bit_value = 0; } else if( ((l_cnt_save >= 3)&&(l_cnt_save <= 9)) && // 560us低电平,1680us高电平 ((h_cnt_save >= 14)&&(h_cnt_save <= 20)) ) { bit_value = 1; } else { bit_value = 2; } if( SET == rcv_sync_ok_flag ) { if((1 == bit_value) || (0 == bit_value) ) { if( bit_rcv_cnt < 8 ) { ir_code[0] |= (bit_value<< (bit_rcv_cnt%8)); } else if( bit_rcv_cnt < 16 ) { ir_code[1] |= (bit_value<< (bit_rcv_cnt%8)); } else if( bit_rcv_cnt < 24 ) { ir_code[2] |= (bit_value<< (bit_rcv_cnt%8)); } else if( bit_rcv_cnt < 32 ) { ir_code[3] |= (bit_value<< (bit_rcv_cnt%8)); } if( bit_rcv_cnt >= 31 ) { ir_decode_ok_flag = SET; rcv_sync_ok_flag = RESET; } bit_rcv_cnt ++; } else { rcv_sync_ok_flag = RESET; // 位接收错误,重新解码 } } if( ((l_cnt_save >= 87)&&(l_cnt_save <= 93)) && ((h_cnt_save >= 42)&&(h_cnt_save <= 48)) ) // 同步码,9ms低电平,4.5ms高电平 { rcv_sync_ok_flag = SET; bit_rcv_cnt = 0; ir_code[0] = 0; ir_code[1] = 0; ir_code[2] = 0; ir_code[3] = 0; } else if(((l_cnt_save >= 87)&&(l_cnt_save <= 93)) && ((h_cnt_save >= 20)&&(h_cnt_save <= 25)) ) { printf("repeate code\r\n"); ir_decode_ok_flag = 3; //长按 } } }
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
71
617
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号