帮我写一个,MQ-135在STM32F103C8T6的测试代码,要求具有串口调试的功能
时间: 2023-05-25 18:07:14 浏览: 70
以下是一个基本的MQ-135在STM32F103C8T6的测试代码,具有串口调试的功能。其中,已经配置了ADC的通道和GPIO引脚,可以根据实际情况进行修改。
```
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
#define MQ_PIN GPIO_Pin_1
#define MQ_PORT GPIOA
#define UART_PORT GPIOA
#define UART_TX_PIN GPIO_Pin_9
#define UART_RX_PIN GPIO_Pin_10
ADC_InitTypeDef ADC_InitStructure;
USART_InitTypeDef USART_InitStructure;
void GPIO_Configuration(void);
void ADC_Configuration(void);
void USART_Configuration(void);
void Delay_us(uint32_t nTime);
int main(void)
{
uint16_t raw_value;
float ppm_value;
SystemInit();
GPIO_Configuration();
USART_Configuration();
ADC_Configuration();
while(1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
raw_value = ADC_GetConversionValue(ADC1);
ppm_value = 116.6020682 * (pow((double)raw_value/1024.0, -2.769034857));
char buffer[50];
sprintf(buffer, "%.2f", ppm_value);
USART_SendString(buffer);
USART_SendString("\r\n");
Delay_us(100000);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// MQ_SENSOR PIN
GPIO_InitStructure.GPIO_Pin = MQ_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(MQ_PORT, &GPIO_InitStructure);
// UART TX PIN
GPIO_InitStructure.GPIO_Pin = UART_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(UART_PORT, &GPIO_InitStructure);
// UART RX PIN
GPIO_InitStructure.GPIO_Pin = UART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(UART_PORT, &GPIO_InitStructure);
}
void ADC_Configuration(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_28Cycles5);
ADC_Cmd(ADC1, ENABLE);
Delay_us(10000);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
void USART_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void Delay_us(uint32_t nTime)
{
int i,j;
for(i=0;i<nTime;i++)
{
for(j=0;j<10;j++);
}
}
void USART_SendString(char* pBuffer)
{
while(*pBuffer != '\0')
{
USART_SendData(USART1, (uint16_t)*pBuffer++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)