STM32F103RCT6开发板控制烟雾传感器模块代码
时间: 2023-06-20 22:09:23 浏览: 216
基于STM32烟雾传感器.zip_stm32-_基于stm32 mq-2_烟雾_烟雾 stm32_烟雾基于STM32
5星 · 资源好评率100%
以下是使用STM32F103RCT6开发板控制烟雾传感器模块的代码示例。代码中使用了ADC模块读取传感器模块的模拟信号,然后根据信号值进行判断,并通过串口输出烟雾浓度值。
```c
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
#define SMOKE_SENSOR_PIN GPIO_Pin_0
#define SMOKE_SENSOR_PORT GPIOA
#define SMOKE_SENSOR_ADC_CHANNEL ADC_Channel_0
void USART1_Init(void);
void USART1_SendByte(uint8_t byte);
void USART1_SendString(char* str);
void ADC1_Init(void);
uint16_t ADC1_ReadValue(uint8_t channel);
int main(void)
{
uint16_t smokeValue;
char smokeStr[10];
USART1_Init();
ADC1_Init();
while (1)
{
smokeValue = ADC1_ReadValue(SMOKE_SENSOR_ADC_CHANNEL);
if (smokeValue > 500) {
sprintf(smokeStr, "%d", smokeValue);
USART1_SendString("Smoke concentration: ");
USART1_SendString(smokeStr);
USART1_SendString("\r\n");
}
else {
USART1_SendString("No smoke detected\r\n");
}
delay_ms(1000);
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_SendByte(uint8_t byte)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, byte);
}
void USART1_SendString(char* str)
{
while (*str) {
USART1_SendByte(*str++);
}
}
void ADC1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = SMOKE_SENSOR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(SMOKE_SENSOR_PORT, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
uint16_t ADC1_ReadValue(uint8_t channel)
{
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
void delay_ms(uint32_t ms)
{
SysTick->LOAD = SystemCoreClock / 1000 * ms;
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
}
```
需要注意的是,代码中使用了串口输出烟雾浓度值,因此需要连接开发板的USART1的Tx和Rx引脚到电脑,以便通过串口调试助手查看输出结果。同时,代码中还需要定义一个延时函数`delay_ms()`,该函数使用了STM32的SysTick定时器实现,可以根据需要自行实现这个函数。
阅读全文