请给出用stm32c8t6驱动mq135初始化代码并采集PPM打印到串口,开始时使用校准模式进行校准,检测环境中的基准气体浓度,然后将其设置为参考值。确保在没有有害气体时PPM保持在400左右。
时间: 2024-03-17 13:44:54 浏览: 114
好的,以下是用stm32c8t6驱动mq135初始化代码并采集PPM打印到串口,包含校准模式的实现:
```c
#include "stm32f1xx_hal.h"
#include <stdio.h>
#define ADC_RESOLUTION 4096.0f
#define VREF 3.3f
// MQ135相关参数,需要根据实际情况进行调整
#define RL_VALUE 20.0f
#define RO_CLEAN_AIR_FACTOR 9.83f
ADC_HandleTypeDef hadc1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_ADC1_Init(void);
float getResistance(uint16_t raw_adc);
float getCorrectedResistance(float resistance);
float getPPM(float corrected_resistance);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
HAL_ADC_Start(&hadc1);
// 开始校准模式
float ro = 0;
for (int i = 0; i < 10; i++) {
HAL_ADC_PollForConversion(&hadc1, 100);
uint16_t raw_adc = HAL_ADC_GetValue(&hadc1);
float resistance = getResistance(raw_adc);
ro += resistance / RO_CLEAN_AIR_FACTOR;
}
ro = ro / 10.0f;
while (1)
{
HAL_ADC_PollForConversion(&hadc1, 100);
uint16_t raw_adc = HAL_ADC_GetValue(&hadc1);
float resistance = getResistance(raw_adc);
float corrected_resistance = getCorrectedResistance(resistance);
float ppm = getPPM(corrected_resistance, ro);
// 打印PPM值到串口
char msg[50];
sprintf(msg, "MQ135 PPM: %.2f\r\n", ppm);
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
HAL_Delay(1000);
}
}
// 计算电阻值
float getResistance(uint16_t raw_adc)
{
float voltage = (float)raw_adc * VREF / ADC_RESOLUTION;
return RL_VALUE * (VREF - voltage) / voltage;
}
// 校正电阻值
float getCorrectedResistance(float resistance)
{
return resistance / RO_CLEAN_AIR_FACTOR;
}
// 计算PPM值
float getPPM(float corrected_resistance, float ro)
{
return pow(10, ((log10(corrected_resistance / ro) - 1.0272f) / -0.662f));
}
// 初始化GPIO
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
// 初始化UART
static void MX_USART2_UART_Init(void)
{
__HAL_RCC_USART2_CLK_ENABLE();
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
// 初始化ADC
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
__HAL_RCC_ADC1_CLK_ENABLE();
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
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_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
```
需要注意的是,MQ135的基准气体浓度需要根据实际情况进行调整,因为不同的环境中基准气体浓度可能会有所不同。此外,需要根据自己的硬件连接情况进行相应的引脚配置。
阅读全文