一. CUBEMX配置使用内部HSI时钟源64mhz
TIME 配置
二. NEC红外原理
引导码: 由9ms的低电平+4.5ms的高电平组成。
收到数据位0: 0.56m低电平+ 0.56ms的高电平。
收到数据位1: 0.56ms低电平+1.68ms的高电平。
NEC协议一次完整的传输包含: 引导码、8位用户码、8位用户反码、8位数据码、8位数据反码。
三. 代码说明
初始化time
void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
/**TIM2 GPIO Configuration
PA0-WKUP ------> TIM2_CH1
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* TIM2 interrupt Init */
NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
NVIC_EnableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
TIM_InitStruct.Prescaler = 64-1;
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_InitStruct.Autoreload = 65535;
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
LL_TIM_Init(TIM2, &TIM_InitStruct);
LL_TIM_DisableARRPreload(TIM2);
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
LL_TIM_DisableMasterSlaveMode(TIM2);
LL_TIM_IC_SetActiveInput(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
LL_TIM_IC_SetPrescaler(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
LL_TIM_IC_SetFilter(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);
/* USER CODE BEGIN TIM2_Init 2 */
//自己添加的
LL_TIM_ClearFlag_CC1(TIM2);
LL_TIM_ClearFlag_UPDATE(TIM2);
LL_TIM_EnableIT_CC1(TIM2);
LL_TIM_EnableIT_UPDATE(TIM2);
LL_TIM_CC_EnableChannel(TIM2,LL_TIM_CHANNEL_CH1); //这个不要忘记打开了,不然无法捕获
LL_TIM_EnableCounter(TIM2);
/* USER CODE END TIM2_Init 2 */
}
中断函数
uint16_t IR_time=0; //计数保存
uint32_t IRDATA=0;//红外解码保存
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
if(LL_TIM_IsActiveFlag_CC1(TIM2))//边沿捕获中断
{
//如果为上升沿
if (LL_TIM_IC_GetPolarity(TIM2,LL_TIM_CHANNEL_CH1)==LL_TIM_IC_POLARITY_RISING)//获取捕获边沿
{
LL_TIM_SetCounter(TIM2,0);//计数器归零
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_FALLING);//切换为下降沿
}
else//如果为下降沿
{
LL_TIM_IC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);//切换为上升沿
IR_time=LL_TIM_GetCounter(TIM2);//获取上升沿时间
if(((IR_time>3500)&&(IR_time<5000)))//获取到起始码9ms低电平+4ms高电平
{
IRDATA=0;
//LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);
}
else if((IR_time>460)&&(IR_time<760))//获取到‘0’高电平,560us高电平
{
IRDATA<<=1;//左移置0
}
else if((IR_time>1500)&&(IR_time<1980))//获取到‘1’高电平,1690us高电平
{
IRDATA<<=1;
IRDATA|=0x01;//置1
}
}
}
if(LL_TIM_IsActiveFlag_UPDATE(TIM2))//溢出中断
{
LL_TIM_ClearFlag_UPDATE(TIM2);
}
LL_TIM_ClearFlag_CC1(TIM2);
/* USER CODE END TIM2_IRQn 0 */
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
main函数
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t i=100;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
/* System interrupt init*/
NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
/* SysTick_IRQn interrupt configuration */
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),15, 0));
/** NOJTAG: JTAG-DP Disabled and SW-DP Enabled
*/
LL_GPIO_AF_Remap_SWJ_NOJTAG();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
printf("stm32 hello\n");
while (1)
{
// LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
// LL_mDelay(500);
// LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
// LL_mDelay(500);
LL_mDelay(10);
if (0!=IRDATA)
{
LL_mDelay(500);
printf("0x%08x\n",IRDATA);
}
switch(IRDATA)
{
case (0xffa25d): LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);IRDATA=0; break;
case (0xff629d): LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);IRDATA=0; break;
case (0xffe21d): LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);IRDATA=0; break;
case (0xff22dd): LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15|LL_GPIO_PIN_14|LL_GPIO_PIN_13);IRDATA=0;break;
case (0xff02fd): LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15|LL_GPIO_PIN_14|LL_GPIO_PIN_13);IRDATA=0; break;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
. 试验结果
串口打印
stm32 hello
0x00ffa25d
0x00ffa25d
0x00ff629d
0x00ff629d
0x00ffe21d
0x00ffe21d
00和ff互为反码
a2和5d互为反码
验证OK
|
奖励500家园币~