一、红外遥控的原理
简单来说就是,红外发送器这边,发送脉冲信号时是高电平,发送的不是脉冲信号就是低电平,红外接收器这边,接收脉冲信号是低电平,接收的不是脉冲信号就是高电平文章来源:https://www.toymoban.com/news/detail-691547.html
代码实现:(这里使用的是stm32f103rct6单片机)文章来源地址https://www.toymoban.com/news/detail-691547.html
u32 inf_val;
u8 inf_flag;
void Infrared_Remote_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
/*1、打开外设时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
/*2、配置GPIO*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStruct);
/*3、中断线的映射*/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource15);
/*4、配置EXTI*/
EXTI_InitStruct.EXTI_Line = EXTI_Line15;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
/*5、配置NVIC*/
NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
EXTI_ClearITPendingBit(EXTI_Line15);
}
u8 Infrared_Remote_Delay(void)
{
u8 tim = 0;
while(PBin(15))
{
delay_us(50);
tim++;
if(tim == 255)
{
return tim;
}
}
return tim;
}
void EXTI15_10_IRQHandler(void)
{
u8 high_time,flag,data = 0,n = 0;
while(1)
{
if(PBin(15))
{
high_time = Infrared_Remote_Delay();
if(high_time == 255)
break;
if(high_time > 70 && high_time < 100)//收到引导码
{
flag = 1;
}
else if(high_time > 6 && high_time < 12)//收到0
{
data = 0;
}
else if(high_time > 30 && high_time < 36)//收到1
{
data = 1;
}
if(flag)//如果收到引导码
{
inf_val <<= 1;
inf_val += data;
if(n == 32)//因为inf_val是32位的
{
inf_flag = 1;
break;
}
}
n++;
}
}
EXTI_ClearITPendingBit(EXTI_Line15);
}
到了这里,关于stm32---用外部中断实现红外接收器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!