【STM32】【HAL库】遥控关灯2 分机
相关连接
【STM32】【HAL库】遥控关灯0 概述
【STM32】【HAL库】遥控关灯1主机
【STM32】【HAL库】遥控关灯2 分机
【STM32】【HAL库】遥控关灯3 遥控器
需求
接收RF433和红外信号,根据信号内容控制舵机
硬件设计
主控采用stm32F103c6
STM32
433接收
其他接口
软件设计
接收RF433/红外的信号,并完成动作即可
相关链接
舵机驱动
NEC
RF433
舵机关灯思路
HAL初始化
定时器1
红外和RF433的计时
设置为分频后1us,默认溢出数,开中断
定时器2
用作舵机控制的PWM生成
每隔20us触发一次中断
GPIO
LED:用作指示灯,推挽输出即可
GPIO
舵机控制信号
配置为开漏浮空(外部接上拉电阻到5V),配置为最高等级(避免复位时让电机出现误动作)
GPIO
RF433输入
配置为边沿中断模式
GPIO
红外输入
配置为下降沿中断模式
硬件看门狗
32分频,溢出值4000
每(32/40k*4000=3.2s)触发一次
本程序目的是让程序每3.2s重启一次,因此只在需要操作舵机时喂狗,主循环无喂狗
程序
中断回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if (htim == &Steering_Engine_TIM){if (M_EN == 1)Steering_Engine_Action();elseHAL_GPIO_WritePin(Steering_Engine_GPIOx, Steering_Engine_GPIO_Pin, GPIO_PIN_SET);}else if (htim == &htim3){}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{if (GPIO_Pin == GPIO_PIN_2){if (IR_NEC_Read_ins == 0)RF_Read_Decode();}else if (GPIO_Pin == GPIO_PIN_3){IR_NEC_Read_Decode(air);}
}
主循环处理函数
if (IR_NEC_Read_OK){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);// printf("%02X%02X%02X%02X\r\n", IR_NEC_Read_Dat[0], IR_NEC_Read_Dat[1], IR_NEC_Read_Dat[2], IR_NEC_Read_Dat[3]);if (IR_NEC_Read_Dat[0] == 0x4D && IR_NEC_Read_Dat[1] == 0xb2 && IR_NEC_Read_Dat[2] == 0xa3 && IR_NEC_Read_Dat[3] == 0x5C)OPEN();else if (IR_NEC_Read_Dat[0] == 0x4D && IR_NEC_Read_Dat[1] == 0xb2 && IR_NEC_Read_Dat[2] == 0x59 && IR_NEC_Read_Dat[3] == 0xa6)CLOSE();if (IR_NEC_Read_Dat[0] == 0x84 && IR_NEC_Read_Dat[1] == 0xff && IR_NEC_Read_Dat[2] == 0x81 && IR_NEC_Read_Dat[3] == 0x7e)OPEN();else if (IR_NEC_Read_Dat[0] == 0x84 && IR_NEC_Read_Dat[1] == 0xff && IR_NEC_Read_Dat[2] == 0x01 && IR_NEC_Read_Dat[3] == 0xfe)CLOSE();HAL_IWDG_Refresh(&hiwdg);HAL_Delay(500);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);IR_NEC_Read_Dat[0] = 0;IR_NEC_Read_Dat[1] = 0;IR_NEC_Read_Dat[2] = 0;IR_NEC_Read_Dat[3] = 0;IR_NEC_Read_OK = 0;}if (RF_READ_OK){HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);// printf("%02X%02X%02X\r\n", RF_READ_data[0], RF_READ_data[1], RF_READ_data[2]);if (RF_READ_data[0] == 0xac && RF_READ_data[1] == 0x22 && RF_READ_data[2] == 0x00)OPEN();else if (RF_READ_data[0] == 0xac && RF_READ_data[1] == 0x22 && RF_READ_data[2] == 0xff)CLOSE();HAL_IWDG_Refresh(&hiwdg);HAL_Delay(500);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);RF_READ_data[0] = 0;RF_READ_data[1] = 0;RF_READ_data[2] = 0;RF_READ_OK = 0;// __set_FAULTMASK(1);// NVIC_SystemReset();}
开关灯控制函数
void OPEN()
{M_EN = 1;HAL_IWDG_Refresh(&hiwdg);Steering_Engine_360(0, 30);HAL_Delay(500);HAL_IWDG_Refresh(&hiwdg);Steering_Engine_360(1, 40);HAL_Delay(80);HAL_IWDG_Refresh(&hiwdg);Steering_Engine_Stop();M_EN = 0;
}
void CLOSE()
{M_EN = 1;HAL_IWDG_Refresh(&hiwdg);Steering_Engine_360(1, 30);HAL_Delay(500);HAL_IWDG_Refresh(&hiwdg);Steering_Engine_360(0, 30);HAL_Delay(80);HAL_IWDG_Refresh(&hiwdg);Steering_Engine_Stop();M_EN = 0;
}
成品
另外app开发很简单,百度凑凑就行了,源码同样在GitHub上,请自行查看即可
GitHub