stm32红外示例代码
时间: 2023-11-26 19:46:16 浏览: 60
以下是一个基于STM32的红外NEC协议接收端示例代码:
```c
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_tim.h"
#include "misc.h"
#define IR_IN GPIO_Pin_0
#define IR_IN_GPIO GPIOA
#define IR_IN_RCC RCC_APB2Periph_GPIOA
#define IR_IN_EXTI EXTI_Line0
#define IR_IN_EXTI_PORTSOURCE GPIO_PortSourceGPIOA
#define IR_IN_EXTI_PINSOURCE GPIO_PinSource0
#define IR_IN_EXTI_IRQn EXTI0_IRQn
#define TIM_PERIOD 65535
volatile uint16_t ir_data = 0;
volatile uint8_t ir_data_ready = 0;
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
ir_data_ready = 1;
}
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(IR_IN_EXTI) != RESET)
{
EXTI_ClearITPendingBit(IR_IN_EXTI);
if (TIM_GetCounter(TIM2) > 10000)
{
ir_data = 0;
}
else if (TIM_GetCounter(TIM2) > 5000)
{
ir_data = (ir_data << 1) | 1;
}
else
{
ir_data = ir_data << 1;
}
TIM_SetCounter(TIM2, 0);
}
}
void ir_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(IR_IN_RCC,