void TIM2_IRQHandler( void )
{
static INT16U IR_LastPluse = 0;
static INT8U IR_Sta = 0;
static INT32U IR_Code = 0;
static INT8U IR_PluseCnt = 0;
static INT8U IR_Up = 0;
INT16U IR_ThisPluse;
INT16U IR_PluseSub;
if ( TIM_GetITStatus( TIM2, TIM_IT_CC2 ) == SET )
{
TIM_ClearFlag( TIM2, TIM_IT_CC2 );
IR_Up = 0;
IR_ThisPluse = TIM_GetCapture2( TIM2 );
if ( IR_ThisPluse > IR_LastPluse )
{
IR_PluseSub = IR_ThisPluse - IR_LastPluse;
}
else {
IR_PluseSub = 0xffff - IR_LastPluse + IR_ThisPluse;
}
IR_LastPluse = IR_ThisPluse;
IR_PluseCnt++;
if ( IR_PluseCnt == 2 )
{
if (( IR_PluseSub > 5000 ) && ( IR_PluseSub < 8000 ))
{
IR_Sta = 0x01;
}
}
else if ( IR_Sta & 0x01 ) // 如果引导成功,开始接码
{
if (( IR_PluseSub < 450 ) || ( IR_PluseSub > 1300 ))
{
IR_Sta = 0;
IR_PluseCnt = 0;
IR_Code = 0;
}
else
{
IR_Code <<= 1;
if (( IR_PluseSub > 900 ) && ( IR_PluseSub < 1300 ))
{
IR_Code |= 0x01;
}
if ( IR_PluseCnt == 34 )
{
IR_Key = IR_Code;
IR_Sta = 0x02;
printf( "IR Down is : 0x%8x\n\n", IR_Key );
}
}
}
else if ( IR_Sta & 0x02 )
{
switch ( IR_PluseCnt )
{
case 35:
{
if ( ( IR_PluseSub < 4500 ) || ( IR_PluseSub > 7000 ) )
{
IR_PluseCnt--;
}
break;
}
case 36:
{
IR_PluseCnt = 34;
if ( ( IR_PluseSub > 45000) && ( IR_PluseSub < 60000 ) )
{
IR_Key = IR_Code;
printf( "IR continue is : 0x%8x\n\n", IR_Key );
}
break;
}
}
}
}
if ( TIM_GetITStatus( TIM2, TIM_IT_Update ) == SET )
{
TIM_ClearFlag( TIM2, TIM_IT_Update );
if ( GPIO_ReadInputDataBit( GPIOA, GPIO_Pin_1 ) == SET )
{
IR_Up++;
if ( IR_Up >= 2 )
{
IR_Code = 0;
IR_Sta = 0;
IR_PluseCnt = 0;
}
}
}
}
上一篇:关于STM32的ADC/DAC问题集锦
下一篇:STM32 RCC实验MCO脚输出时钟波形
推荐阅读最新更新时间:2024-03-16 15:14