基于stm32的mq-7,gp2y1014au传感器检测一氧化碳,pm2.5浓度装置的代码,要有阈值报警模块
时间: 2023-12-25 07:02:54 浏览: 119
以下是基于STM32的MQ-7、GP2Y1014AU传感器检测一氧化碳和PM2.5浓度的代码,同时包含阈值报警模块。在代码中,使用了ADC采集传感器数据,并通过串口输出数据和报警信息。
```
#include "main.h"
#include "stdio.h"
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
UART_HandleTypeDef huart1;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_ADC1_Init(void);
/* Private user variables ----------------------------------------------------*/
float CO_concentration = 0.0;
float PM25_concentration = 0.0;
float CO_threshold = 50.0; // 设定CO阈值为50 ppm
float PM25_threshold = 75.0; // 设定PM2.5阈值为75 ug/m3
/* Private user functions ----------------------------------------------------*/
void UART_Print(char *str) {
HAL_UART_Transmit(&huart1, (uint8_t*) str, strlen(str), HAL_MAX_DELAY);
}
void UART_Println(char *str) {
UART_Print(str);
UART_Print("\r\n");
}
/* Main function -------------------------------------------------------------*/
int main(void) {
/* MCU Configuration--------------------------------------------------------*/
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
/* Infinite loop -----------------------------------------------------------*/
while (1) {
// 采集CO浓度
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
uint16_t CO_adc_value = HAL_ADC_GetValue(&hadc1);
CO_concentration = (float) CO_adc_value / 4096 * 5000 / 200; // 5000 mV的参考电压,200 ppm/V的灵敏度
char CO_concentration_str[10];
sprintf(CO_concentration_str, "%.2f", CO_concentration);
// 采集PM2.5浓度
// ...
// 判断是否超过阈值
if (CO_concentration > CO_threshold) {
UART_Println("CO concentration exceeds threshold!");
// 发送报警信息
// ...
}
if (PM25_concentration > PM25_threshold) {
UART_Println("PM2.5 concentration exceeds threshold!");
// 发送报警信息
// ...
}
// 输出采集的数据
char data_str[50];
sprintf(data_str, "CO: %s ppm, PM2.5: %s ug/m3", CO_concentration_str, "N/A");
UART_Println(data_str);
HAL_Delay(1000);
}
}
/* ADC1 Initialization Function */
static void MX_ADC1_Init(void) {
ADC_ChannelConfTypeDef sConfig = {0};
/* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK) {
Error_Handler();
}
/* Configure the selected ADC channel */
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler();
}
}
/* USART1 Initialization Function */
static void MX_USART1_UART_Init(void) {
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK) {
Error_Handler();
}
}
/* GPIO Initialization Function */
static void MX_GPIO_Init(void) {
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure GPIO pin : PA5 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* System Clock Configuration */
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
Error_Handler();
}
}
/* This function is executed in case of error occurrence. */
void Error_Handler(void) {
/* User code can be inserted here to handle errors */
while (1) {
}
}
```
阅读全文