![](https://i-blog.csdnimg.cn/blog_migrate/3efa3601c9082c63f33a7f3d168ee833.png)
![](https://i-blog.csdnimg.cn/blog_migrate/23b4f6d064771f1d41f5be993f1a2d05.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1c1833e42113bf3c589a91c78ebc9296.png)
本章所要实现的功能是:使用外部中断功能将遥控器键值编码数据解码后通 过串口打印输出,同时控制 DS0 指示灯闪烁,提示系统运行。程序框架如下: (1)使能 PB9 端口时钟,映射 PB9 至外部中断线上等 (2)编写红外解码函数(在 EXTI 中断处理) (3)编写主函数
hwjs.h+hwjs.c
#ifndef _hwjs_H
#define _hwjs_H
#include "system.h"
extern u32 Hwjs_Key;
extern u8 hwjs_bz;
#define HWJS_PIN GPIO_Pin_9
#define HWJS_PORT GPIOB
#define HWJS_PORT_RCC RCC_APB2Periph_GPIOB
void Hwjs_Init();
u8 Hwjs_Time();
void EXTI9_5_IRQHandler();
#endif
#include "hwjs.h"
#include "SysTick.h"
u32 Hwjs_Key=0;
u8 hwjs_bz=0;
void Hwjs_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(HWJS_PORT_RCC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE );
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin=HWJS_PIN;
GPIO_Init(HWJS_PORT,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
EXTI_ClearITPendingBit(EXTI_Line9);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占式优先级,按自己需求配置
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//响应式优先级
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_InitStructure.EXTI_Line=EXTI_Line9;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;//下降沿触发
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
u8 Hwjs_Time()
{
u8 t=0;
while(GPIO_ReadInputDataBit(GPIOB,HWJS_PIN)==1)
{
t++;
delay_us(20);
if(t>250)return t;
}
return t;
}
void EXTI9_5_IRQHandler()//中断函数进行解码
{
u8 i=0,tim=0,OK=0,HL=0,num=0;
while(1)
{
if(GPIO_ReadInputDataBit(GPIOB,HWJS_PIN)==1)
{
tim=Hwjs_Time();
if(tim>=250)break;
if(tim>=200&&tim<=250)OK=1;//检测引导码,4-5ms之间,开发手册上为4.5ms
else if(tim>60&&tim<90)
{
HL=1;
}
else if(tim>20&&tim<40)
{
HL=0;
}
if(OK==1)
{
Hwjs_Key<<=1;
Hwjs_Key |=HL;//顺序不能变
}
num++;
if(num>=32)
{
hwjs_bz=1;
break;
}
}
}
EXTI_ClearITPendingBit(EXTI_Line9);//清除中断标志
}