本文主要是介绍STM32F4(HAL)读取二氧化碳传感器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
二氧化碳传感器:
二氧化碳是空气中的主要成分之一,与咱们的日子密切相关。跟着现代社会的不断进步,二氧化碳传感器被广泛地运用,来优化咱们的生产和日子,如监测农业温室中的二氧化碳含量并及时通风以添加作物产量,监测矿井中的二氧化碳浓度以确保工人的安全等。
此外,人体对空气中二氧化碳的增加十分敏感,二氧化碳含量每添加0.5%,就会引起人体的显着反响。在自然环境中,空气中二氧化碳的正常含量为0.04%(400PPM),在大城市有时到达500PPM。当室内无人时,二氧化碳的浓度一般为500至700PPM。
众所周知,二氧化碳作为主要气体之一,对农业、工业等各个领域都有重要的影响,随着现代社会的不断进步,二氧化碳的含量也逐渐变多,研究表明地球的温室效应从工业革命就开始形成了,如今大有愈演愈烈的态势。
目前,人类活动产生的二氧化碳排放已达到历史峰值。最新数据显示,2011年全球二氧化碳排放量是1850年的163倍。
本次实验传感器:
炜盛科技的MH-Z16型号传感器
产品描述:
产品描述 MH-Z16 二氧化碳气体传感器(以下简称传感器) 是一个通用智能小型传感器,利用非色散红外(NDIR)原 理对空气中存在的CO2 进行探测,具有很好的选择性,无 氧气依赖性,寿命长。内置温度补偿;同时具有数字输出 与PWM输出,方便使用。该传感器是将成熟的红外吸收气 体检测技术与精密光路设计、精良电路设计紧密结合而制 作出的高性能传感器。
参数:
引脚定义:
本次实验已UART传输信号为例
硬件连接:
将传感器的 Vin-GND-RXD-TXD 分别接至用户的 5V-GND-TXD-RXD。(用户端须使用 TTL 电平,如 果是 RS232 电平,须进行转换)。
软件设置:
将串口波特率设置为 9600,数据位设置为 8 位,停止位设置为 1 位、奇偶校验位设置为无。
1、首先将读取气体指令通过串口发送给二氧化碳传感器,
通过单片机设定的串口将 0XFF 0X01 0X86 0X00 0X00 0X00 0X00 0X00 0X79依次发送给二氧化碳传感器
2、随后二氧化碳传感器将9Byte数据返回给单片机,单片机将数据接收到进行解析,解析过程如下:
(1) 将Byte1+Byte2+Byte3+Byte4+Byte5+Byte6+Byte7相加的第八位与Byte8相比相等表示数据传输正确。
(2)将Byte2 * 256 + Byte3 就是传感器采集的二氧化碳的浓度
代码部分:
uart.c
#include "usart.h"UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;/* USART1 init function */
/*配置串口1和串口2*/
void MX_USART1_UART_Init(void)
{huart1.Instance = USART1;huart1.Init.BaudRate = 9600;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();}USART1->CR1 |= 1<<5;}
/* USART2 init function */void MX_USART2_UART_Init(void)
{huart2.Instance = USART2;huart2.Init.BaudRate = 9600;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();}USART2->CR1 |= 1<<5;}void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{/*开启串口时钟、配置复用GPIO、配置中断优先级,使能中断*/GPIO_InitTypeDef GPIO_InitStruct = {0};if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspInit 0 *//* USER CODE END USART1_MspInit 0 *//* USART1 clock enable */__HAL_RCC_USART1_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**USART1 GPIO ConfigurationPA9 ------> USART1_TXPA10 ------> USART1_RX*/GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF7_USART1;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USART1_IRQn);}else if(uartHandle->Instance==USART2){/* USART2 clock enable */__HAL_RCC_USART2_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**USART2 GPIO ConfigurationPA2 ------> USART2_TXPA3 ------> USART2_RX*/GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF7_USART2;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* USART2 interrupt Init */HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);HAL_NVIC_EnableIRQ(USART2_IRQn);}
}void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{/*串口反初始化*/if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspDeInit 0 *//* USER CODE END USART1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_USART1_CLK_DISABLE();/**USART1 GPIO ConfigurationPA9 ------> USART1_TXPA10 ------> USART1_RX*/HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);/* USART1 interrupt Deinit */HAL_NVIC_DisableIRQ(USART1_IRQn);}else if(uartHandle->Instance==USART2){/* USER CODE BEGIN USART2_MspDeInit 0 *//* USER CODE END USART2_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_USART2_CLK_DISABLE();/**USART2 GPIO ConfigurationPA2 ------> USART2_TXPA3 ------> USART2_RX*/HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);/* USART2 interrupt Deinit */HAL_NVIC_DisableIRQ(USART2_IRQn);}
}
uart.h
#ifndef __USART_H__
#define __USART_H__#ifdef __cplusplus
extern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "main.h"extern UART_HandleTypeDef huart1;extern UART_HandleTypeDef huart2;void MX_USART1_UART_Init(void);
void MX_USART2_UART_Init(void);#ifdef __cplusplus
}
#endif#endif /* __USART_H__ */
main.c
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"/* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
uint8_t uart1_sbuf[10];
uint8_t uart1_flag;
uint8_t i;
uint8_t uart2_sbuf[10];
uint8_t j;
uint8_t uart2_SendByte[9] = {0xff,0x01,0x86,0,0,0,0,0,0x79}; //发送给二氧化碳的读取命令
void USART_Send(uint8_t dat); //串口二发送一字节数据
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void USART_SendHex(uint8_t * dat); //串口二发送hex数据
/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{HAL_Init();/* Configure the system clock */SystemClock_Config();/* Initialize all configured peripherals */MX_GPIO_Init();MX_USART1_UART_Init();MX_USART2_UART_Init();/* USER CODE BEGIN 2 *//* USER CODE END 2 */USART_SendHex(uart2_SendByte);/* Infinite loop *//* USER CODE BEGIN WHILE */while (1){
// if( i >= 9)
// {
// i = 0;
// HAL_UART_Transmit(&huart1,uart1_sbuf,9,100);
// }if( j >= 9) //串口二接收到9个数据串口一将数据发送出去{j = 0;HAL_UART_Transmit(&huart1,uart2_sbuf,9,100);
// HAL_UART_Transmit(&huart1,(uint8_t *)"\r\n",2,100);
// uint8_t p = ~(uart2_sbuf[1]+uart2_sbuf[2]+uart2_sbuf[3]+uart2_sbuf[4]+uart2_sbuf[5]+uart2_sbuf[6]+uart2_sbuf[7])+1;
// if(p != uart2_sbuf[8])
// USART_Send(0);
// else
// USART_Send((uart2_sbuf[2]*256)+uart2_sbuf[3]);}/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
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_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM = 8;RCC_OscInitStruct.PLL.PLLN = 168;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;RCC_OscInitStruct.PLL.PLLQ = 4;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_DIV4;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 */
void USART1_IRQHandler(void)
{if(USART1->SR & (1<<5)){USART1->SR &= ~(1<<5);uart1_sbuf[i++] = USART1->DR;}
}
void USART2_IRQHandler(void)
{/* USER CODE BEGIN USART2_IRQn 0 *//* USER CODE END USART2_IRQn 0 *//* USER CODE BEGIN USART2_IRQn 1 */if(USART2->SR & (1<<5)){USART2->SR &= ~(1<<5);uart2_sbuf[j++] = USART2->DR;}/* USER CODE END USART2_IRQn 1 */
}void USART_SendHex(uint8_t * dat) //串口二发送hex数据
{uint8_t i;USART2 -> DR = 0xff;while(!(USART2->SR & (1 << 6))); //等待发送完成USART2->SR &= ~(1 << 6); for(i = 0; i < 9;){USART2 -> DR = dat[i++];while(!(USART2->SR & (1 << 6))); //等待发送完成USART2->SR &= ~(1 << 6); //清空标志位}
}
void USART_Send(uint8_t dat) //串口二发送一字节数据
{USART2 -> DR = dat;while(!(USART2->SR & (1 << 6))); //等待发送完成USART2->SR &= ~(1 << 6);
}
/* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
这篇关于STM32F4(HAL)读取二氧化碳传感器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!