红外遥控的stmf4的程序
时间: 2024-06-09 11:10:38 浏览: 71
红外遥控器通常是通过红外发射管发射红外信号,然后通过红外接收器接收红外信号并解码。下面是一个使用STM32F4的程序示例:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_usart.h"
#define IR_RX_GPIO_PORT GPIOB
#define IR_RX_GPIO_PIN GPIO_Pin_2
#define IR_RX_GPIO_CLK RCC_AHB1Periph_GPIOB
#define IR_RX_EXTI_LINE EXTI_Line2
#define IR_RX_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
#define IR_RX_EXTI_PIN_SOURCE EXTI_PinSource2
#define IR_RX_EXTI_IRQ EXTI2_IRQn
#define BUFFER_SIZE 80
uint16_t irData[BUFFER_SIZE];
volatile uint8_t irDataIndex = 0;
volatile uint8_t irDataReady = 0;
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
static uint16_t lastValue = 0;
uint16_t value = TIM_GetCapture1(TIM2);
uint16_t delta = value - lastValue;
lastValue = value;
if(delta > 200 && delta < 3000)
{
if(irDataIndex < BUFFER_SIZE)
{
irData[irDataIndex++] = delta;
}
}
else if(delta > 15000 && delta < 20000)
{
irDataReady = 1;
}
else
{
irDataIndex = 0;
}
}
}
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line2);
TIM_Cmd(TIM2, ENABLE);
}
}
void initGPIO(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(IR_RX_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = IR_RX_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(IR_RX_GPIO_PORT, &GPIO_InitStruct);
GPIO_PinAFConfig(IR_RX_GPIO_PORT, GPIO_PinSource2, GPIO_AF_TIM2);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
}
void initTIM(void)
{
TIM_TimeBaseInitTypeDef TIM_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_InitStruct.TIM_Prescaler = 168 - 1;
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStruct.TIM_Period = 0xFFFF;
TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_InitStruct);
TIM_ICInitTypeDef TIM_ICInitStruct;
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
TIM_Cmd(TIM2, DISABLE);
}
void initEXTI(void)
{
EXTI_InitTypeDef EXTI_InitStruct;
SYSCFG_EXTILineConfig(IR_RX_EXTI_PORT_SOURCE, IR_RX_EXTI_PIN_SOURCE);
EXTI_InitStruct.EXTI_Line = IR_RX_EXTI_LINE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = IR_RX_EXTI_IRQ;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void initUSART(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART6, &USART_InitStruct);
USART_Cmd(USART6, ENABLE);
}
void sendUSART(const char *data)
{
while(*data)
{
while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET);
USART_SendData(USART6, *data++);
}
}
int main(void)
{
initGPIO();
initTIM();
initEXTI();
initUSART();
while(1)
{
if(irDataReady)
{
char buffer[20];
sprintf(buffer, "IR Data: %d\r\n", irData[0]);
sendUSART(buffer);
irDataIndex = 0;
irDataReady = 0;
}
}
}
```
该程序使用TIM2来捕获红外信号的高低电平时间,使用外部中断来触发TIM2的启动。当收到完整的红外信号后,将数据打印到串口。这只是一个简单的示例程序,你可以根据需要进行修改。
阅读全文