stm32f4的红外遥控hx1838程序
时间: 2023-10-30 12:09:24 浏览: 298
以下是基于STM32F4的HX1838红外遥控程序示例:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "misc.h"
#define IR_PIN GPIO_Pin_0
#define IR_GPIO GPIOA
#define IR_RCC RCC_AHB1Periph_GPIOA
volatile uint32_t tick_count = 0;
volatile uint32_t last_edge_time = 0;
volatile uint32_t ir_code = 0;
void SysTick_Handler(void)
{
tick_count++;
}
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
uint32_t current = tick_count;
uint32_t elapsed = current - last_edge_time;
last_edge_time = current;
if(elapsed > 7000) // Start bit
{
ir_code = 0;
}
else if(elapsed > 3500) // End bit
{
// Process received IR code here
}
else // Data bit
{
ir_code <<= 1;
if(elapsed > 1000) // Logic 1
{
ir_code |= 1;
}
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void init_systick(void)
{
SysTick_Config(SystemCoreClock / 1000);
}
void init_ir_receiver(void)
{
GPIO_InitTypeDef gpio_init;
EXTI_InitTypeDef exti_init;
NVIC_InitTypeDef nvic_init;
RCC_AHB1PeriphClockCmd(IR_RCC, ENABLE);
gpio_init.GPIO_Pin = IR_PIN;
gpio_init.GPIO_Mode = GPIO_Mode_IN;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_OType = GPIO_OType_PP;
gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(IR_GPIO, &gpio_init);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
exti_init.EXTI_Line = EXTI_Line0;
exti_init.EXTI_Mode = EXTI_Mode_Interrupt;
exti_init.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
exti_init.EXTI_LineCmd = ENABLE;
EXTI_Init(&exti_init);
nvic_init.NVIC_IRQChannel = EXTI0_IRQn;
nvic_init.NVIC_IRQChannelPreemptionPriority = 0x00;
nvic_init.NVIC_IRQChannelSubPriority = 0x00;
nvic_init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic_init);
}
int main(void)
{
init_systick();
init_ir_receiver();
while(1)
{
// Your code here
}
}
```
这个程序使用了STM32F4的SysTick定时器来计算时间,并使用了外部中断EXTI0来接收HX1838红外解码器的信号。当接收到红外信号时,程序将计算时间间隔并解码数据。你可以在"Process received IR code here"注释下面的代码中添加你自己的逻辑来处理解码后的数据。
阅读全文