2.解码程序
uint32_t TIM3_Over_Cnt = 0;//tim3溢出次数
uint32_t TIM3_Sum_Cnt = 0;//两次下降沿之间的时间间隔
uint32_t cnt0 = 0;
uint8_t IR_Data[60];
void TIM3_IRQHandler(void)
{
/* USER CODE BEGIN TIM3_IRQn 0 */
/* USER CODE END TIM3_IRQn 0 */
// HAL_TIM_IRQHandler(&htim3);
/* USER CODE BEGIN TIM3_IRQn 1 */
if(__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE)) //定时器溢出中断
{
__HAL_TIM_CLEAR_FLAG(&htim3, TIM_FLAG_UPDATE); //清除中断标记
TIM3_Over_Cnt++;
}
cnt0 = __HAL_TIM_GET_COUNTER(&htim3);
TIM3_Sum_Cnt = (TIM3_Over_Cnt << 16) + cnt0;//获取计数器的值
__HAL_TIM_SetCounter(&htim3,0);//清零重新计数
TIM3_Over_Cnt = 0;//清零重新计数
if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_CC1) != RESET)//TIM3CH1捕获中断
{
if(StartRevFlag == 1)//接收到引导码,开始解码
{
if(TIM3_Sum_Cnt > 36000)//大于36ms认为是结束
{
RevComplete = 1;//解码完成
IR_Tick = 0;
}
else if(RevComplete == 0)
{
if(TIM3_Sum_Cnt > 1000 && TIM3_Sum_Cnt < 1300)//1ms~1.3ms认为是低电平
IR_Data[IR_Idx] = 0;
else if(TIM3_Sum_Cnt > 2100 && TIM3_Sum_Cnt < 2400)//2.1ms~2.4ms认为是高电平
IR_Data[IR_Idx] = 1;
else //接收错误,重新开始
StartRevFlag = 0;
IR_Idx++;
if(IR_Idx > 59)
IR_Idx = 59;
}
}
else
{
if(TIM3_Sum_Cnt > 13000 && TIM3_Sum_Cnt < 14000)//13~14ms引导码
{
StartRevFlag = 1;
}
IR_Tick = 0;
RevComplete = 0;//解码完成标志置零
IR_Idx = 0;//有效解码位
TIM3_Over_Cnt = 0;
TIM3_Sum_Cnt = 0;//定时器计数清零
}
__HAL_TIM_CLEAR_IT(&htim3, TIM_IT_CC1);
}
/* USER CODE END TIM3_IRQn 1 */
}
void IR_Rev()
{
uint8_t num = IR_Idx / 8;
uint8_t IRValue[8];
if(RevComplete == 1 && StartRevFlag == 1 && IR_Tick > 20)
{
if(num > 7)
num = 7;
for(uint8_t j=0;j
{
for(uint8_t i = 0;i< 8;i++)
{
IRValue[j] = IRValue[j]>>1;
if(IR_Data[j*8+i])
IRValue[j] |= 0x80;
}
}
if(IRValue[0] == 0x00 && IRValue[1] == 0xFF)//地址码正确
{
switch(IRValue[2])//判断数据码
{
case 0x46:
KeyValue = S_key_Menu;
break;
case 0x43:
KeyValue = S_key_Set;
break;
case 0x40:
KeyValue = S_key_Rst;
break;
case 0x15:
KeyValue = S_key_Down;
break;
case 0x09:
KeyValue = S_key_Up;
break;
}
}
StartRevFlag = 0;
RevComplete = 0;
IR_Tick = 0;
}
}
推荐阅读:
【开源项目】SFUD--通用串口Flash驱动库的移植和使用
STM32CubeMX系列教程
开源自己做的4.3寸触摸屏,SWM32单片机+LVGL
四位数显表头设计
几种RS485隔离通讯的方案介绍