前面,我们用光强传感器做了很多实践小项目,那么今天我们把项目稍微变一下,把光强传感器换成气体传感器吧!原来光强传感器相关文章链接如下:
基于小熊派光强传感器BH1750实践(multi_timer+状态机工程应用)
基于小熊派光强传感器BH1750状态机驱动项目升级(带LCD屏显示)
基于小熊派光强传感器BH1750状态机驱动项目再度升级(带上位机曲线显示)
今天我们介绍的这款传感器是小熊派自带的一个模块,MQ-2气体传感器。
1、了解MQ-2
从手册中了解到,这是一个可以检测多种有害气体的传感器,看似很强大,等会我就来试一下,它是不是真的能检测出来。
2、小熊派E53_SF1电路原理图
这是模块对应底板的电路原理图,从电路原理图中了解到,这个传感器是一个模拟转数字的器件(ADC),接在PC2管脚。
3、实践需求定义
- 具备显示传感器数值(LCD实现)
- 具备声光报警功能(SF1板载LED、无源蜂鸣器)
- 当传感器数值超出用户设定阈值时声光报警(暂定用户设定阈值为600ppm)
- 实时曲线数据传输(自定义串口协议)
4、实现需求
4.1 stm32cubemx配置硬件驱动参数
由于前面已经写过很多软件上的设置了,所以这里只给出配置表,配置表可以通过CubeMX软件自动生成。
最后会得到一个pdf文件,如下:
4.2 软件实现
(1)传感器数据结构定义
typedef struct Sensor { /*烟感值*/ int Smoke_Value; /*报警灯状态*/ uint8_t Alarm_Led_Status : 1; /*报警蜂鸣器状态*/ uint8_t Alarm_Buzzer_Status : 1; /*是否打印曲线标志*/ uint8_t Display_Plot_Flag : 1 ; /*set方法*/ void (*led_control)(struct Sensor *sensor_handle, uint8_t status); void (*buzzer_control)(struct Sensor *sensor_handle, uint8_t status); void (*plot_control)(struct Sensor *sensor_handle, uint8_t status); /*get方法*/ int (*get_smoke_value)(struct Sensor *sensor_handle); int (*get_led_status)(struct Sensor *sensor_handle); int (*get_buzzer_status)(struct Sensor *sensor_handle); int (*get_plot_status)(struct Sensor *sensor_handle); } Sensor ; Sensor *MQ2_Sensor = NULL;
(2)实现传感器数据结构里对应的set/get
方法
//设置报警灯状态 void alarm_led_control(Sensor *sensor_handle, uint8_t status) { sensor_handle->Alarm_Led_Status = status ; if(sensor_handle->Alarm_Led_Status) HAL_GPIO_WritePin(SF1_LED_GPIO_Port, SF1_LED_Pin, GPIO_PIN_SET); else HAL_GPIO_WritePin(SF1_LED_GPIO_Port, SF1_LED_Pin, GPIO_PIN_RESET); } //设置报警蜂鸣器状态 void alarm_buzzer_control(Sensor *sensor_handle, uint8_t status) { sensor_handle->Alarm_Buzzer_Status = status ; if(sensor_handle->Alarm_Buzzer_Status) HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); else HAL_TIM_PWM_Stop(&htim16, TIM_CHANNEL_1); } //设置曲线显示 void plot_display_control(Sensor *sensor_handle, uint8_t status) { sensor_handle->Display_Plot_Flag = status ; } //获取led状态 int get_led_status(Sensor *sensor_handle) { return sensor_handle->Alarm_Led_Status ; } //获取蜂鸣器状态 int get_buzzer_status(Sensor *sensor_handle) { return sensor_handle->Alarm_Buzzer_Status ; } //获取曲线状态 int get_plot_status(Sensor *sensor_handle) { return sensor_handle->Display_Plot_Flag ; } //获取烟感值 int get_smoke_value(Sensor *sensor_handle) { HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1, 50); sensor_handle->Smoke_Value = HAL_ADC_GetValue(&hadc1); return sensor_handle->Smoke_Value ; }
(3)实现传感器接口注册接口
/*提供访问传感器的接口*/ Sensor mq2_sensor_interface = { /*设置接口*/ .led_control = alarm_led_control, .buzzer_control = alarm_buzzer_control, .plot_control = plot_display_control, /*获取接口*/ .get_led_status = get_led_status, .get_buzzer_status = get_buzzer_status, .get_plot_status = get_plot_status, .get_smoke_value = get_smoke_value }; /*注册传感器*/ int Sensor_Register(Sensor *sensor_handle) { if(NULL == MQ2_Sensor) { MQ2_Sensor = sensor_handle ; if(NULL == MQ2_Sensor) return -1 ; else { MQ2_Sensor->led_control(MQ2_Sensor, 0); MQ2_Sensor->buzzer_control(MQ2_Sensor, 0); MQ2_Sensor->plot_control(MQ2_Sensor, 0); } } return 0 ; }
(3)曲线传输协议定义
S1 序号 烟感值\r\n
(4)主程序实现
int main(void) { /* USER CODE BEGIN 1 */ int smoke_value = 0 ; int serial_number = 0 ; char procol_buf[20] = {0}; char display_buf[20] = {0}; static uint8_t buzzer_flag = 0; static uint8_t led_status = 0 ; static uint8_t plot_status = 0 ; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* 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_ADC1_Init(); MX_SPI2_Init(); MX_USART1_UART_Init(); MX_TIM16_Init(); /* USER CODE BEGIN 2 */ Sensor_Register(&mq2_sensor_interface); printf("led_status:%d\n", mq2_sensor_interface.get_led_status(&mq2_sensor_interface)); printf("buzzer_status:%d\n", mq2_sensor_interface.get_buzzer_status(&mq2_sensor_interface)); printf("plot_status:%d\n", mq2_sensor_interface.get_plot_status(&mq2_sensor_interface)); LCD_Init(); LCD_Clear(BLACK);//清屏为黑色 LCD_ShowString(5, 10, 240, 32, 32, "BearPi MQ-2");//显示字符串,字体大小32*32 Timer_Count = 0 ; /*设置曲线显示*/ mq2_sensor_interface.plot_control(&mq2_sensor_interface, 1); plot_status = mq2_sensor_interface.get_plot_status(&mq2_sensor_interface); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ //printf("烟感值:%d\n",mq2_sensor_interface.get_smoke_value(&mq2_sensor_interface)); /*判断是否100ms到了,该计数由Systick提供,1ms中断一次,100次则为100ms*/ if(100 == Timer_Count) { Timer_Count = 0 ; /*流水号自增*/ ++serial_number ; if(serial_number > 65535) serial_number = 0 ; smoke_value = mq2_sensor_interface.get_smoke_value(&mq2_sensor_interface) ; /*当烟感值大于600ppm,则报警*/ if(smoke_value > 600) { /*这里的buzzer_flag只执行一次,避免重复执行*/ if(0 == buzzer_flag) { buzzer_flag = 1 ; mq2_sensor_interface.buzzer_control(&mq2_sensor_interface, 1); } led_status = !led_status ; mq2_sensor_interface.led_control(&mq2_sensor_interface, led_status); } else { /*这里的buzzer_flag只执行一次,避免重复执行*/ if(1 == buzzer_flag) { buzzer_flag = 0 ; mq2_sensor_interface.buzzer_control(&mq2_sensor_interface, 0); led_status = 0 ; mq2_sensor_interface.led_control(&mq2_sensor_interface, led_status); } } /*在LCD上显示烟感值*/ sprintf(display_buf, "%d%d%d%dppm", smoke_value / 1000 % 100, smoke_value / 100 % 10, smoke_value / 10 % 10, smoke_value % 10); LCD_ShowString(80, 50 + 24 + 32, 240, 32, 32, display_buf); //显示字符串,字体大小32*32 /*拼接发送协议*/ sprintf(procol_buf, "S1 %d%d%d%d%d %d%d%d%d%d", serial_number / 10000, serial_number / 1000 % 100 % 10, serial_number / 100 % 10, serial_number / 10 % 10, serial_number % 10, smoke_value / 10000, smoke_value / 1000 % 100 % 10, smoke_value / 100 % 10, smoke_value / 10 % 10, smoke_value % 10 ); /*如果曲线标志为1,则打印曲线*/ if(1 == plot_status) printf("%s \r\n", procol_buf); } } /* USER CODE END 3 */ }
5、运行效果
曲线显示
由于上位机增加了多通道曲线显示以及其它综合测试功能,故现在还不能开源,后期等完善全部功能后就会开源。
样品测试:打火机气体(一般采用的是丁烷气体燃料)、75%酒精
实践工程下载
公众号后台回复:烟感 即可获取下载链接。
往期精彩
TKM32F499高性能MCU评估板试用之万事开头难,先点个灯来压压惊!