STM32的AD采集光敏电阻电路的电压并通过串口发送和液晶显示
时间: 2023-06-19 18:03:59 浏览: 368
stm32f103串口ad采集发送程序.zip_32串口采集发送_AD串口发送_ina226程序_scalev1g_信息采集
首先,需要将光敏电阻连接到STM32的ADC模块上,以便测量光敏电阻的电压值。其次,需要配置ADC模块的参数,如采样时间、采样速率等。然后,通过编写代码读取ADC模块的值,并将其转换为电压值。最后,将电压值通过串口发送或显示在液晶屏上。
下面是一个简单的示例代码:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((u32)0x4001244C)
static __IO uint32_t TimingDelay;
void Delay(__IO uint32_t nTime);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void ADC_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
ADC_Configuration();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
uint16_t adc_value = ADC_GetConversionValue(ADC1);
float voltage = (float)adc_value / 4096 * 3.3;
char buffer[64];
sprintf(buffer, "Voltage: %.2f V\r\n", voltage);
USART_SendString(buffer);
Delay(1000);
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
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);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
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_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
}
void USART_SendChar(char ch)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
}
void USART_SendString(char *str)
{
while (*str)
{
USART_SendChar(*str++);
}
}
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0);
}
void SysTick_Handler(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
```
这段代码中,我们使用了PA0作为ADC输入引脚,PA9作为USART1的TX引脚,PA10作为USART1的RX引脚。在ADC_Configuration函数中,我们配置了ADC1的参数,将PA0作为ADC输入通道,并设置了采样时间。在主函数中,我们通过ADC_GetConversionValue函数读取ADC1的值,并将其转换为电压值。最后,我们将电压值通过USART_SendString函数发送到串口,或者通过LCD显示。
阅读全文