stm32 红外遥控控制触发中断代码
时间: 2023-09-06 10:12:43 浏览: 87
基于stm32红外控制代码
以下是基于STM32的红外遥控控制触发中断代码:
```
#include "stm32f10x.h"
#define IR_PIN GPIO_Pin_0
#define IR_PORT GPIOA
uint16_t IR_Value = 0;
uint8_t IR_Count = 0;
uint8_t IR_Data[4] = {0};
void EXTI0_IRQHandler(void) {
if (EXTI_GetITStatus(EXTI_Line0) != RESET) {
IR_Value = TIM_GetCounter(TIM2);
if ((IR_Value > 8000) && (IR_Value < 10000)) {
IR_Count = 0;
}
IR_Data[IR_Count] = IR_Value;
IR_Count++;
TIM_SetCounter(TIM2, 0);
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
int main(void) {
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = IR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(IR_PORT, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
while (1) {
// Do something here
}
}
```
这段代码使用了STM32的外部中断(EXTI)和定时器(TIM)模块来实现红外遥控控制触发中断。当外部中断检测到红外信号的上升沿或下降沿时,会触发中断服务程序(IRQHandler),并通过定时器来测量红外信号的脉宽。通过测量不同脉宽的时间,可以识别不同的信号,从而实现遥控控制。
阅读全文