本文章基于国信长天M4开发板,讲述了ADC数据采集的应用,祝各位学生蓝桥杯比赛取得好成绩!
由上图可以看出,开发板上有两个可调电阻R37, R38,分别连接到PB15,和PB12。查阅芯片的数据手册,可知:
PB12 --> ADC1_IN11
PB15 --> ADC2_IN15
下面将基于硬件原厂提供的显示屏示例代码:HAL_06_LCD
介绍ADC的使用步骤:
① 添加 stm32g4xx_hal_adc.c,stm32g4xx_hal_adc_ex.c
双击Drivers/STM32G4xx_HAL_Driver,打开添加文件对话框,在向上一级
-> Drivers
-> STM32G4xx_HAL_Driver
-> Src
中,找到stm32g4xx_hal_adc.c
和 stm32g4xx_hal_adc_ex.c
并添加。添加后如下图所示:
② 修改 stm32g4xx_hal_conf.h
文件
在如下位置找到该文件,并打开:
取消注释 #define HAL_ADC_MODULE_ENABLED
这一行,取下注释后如下图所示:
③ main.c
添加#include "stm32g4xx_hal_adc.h"
在 main.c
适当位置添加#include "stm32g4xx_hal_adc.h"
,添加后如下图所示:
④ 复制如下 ADC 初始化及获取ADC值的代码
注意:下列代码包含了ADC1_IN11和ADC2_IN15的初始化,同学们按需复制!
ADC_HandleTypeDef hadc1 , hadc2; //声明两个ADC结构体
uint16_t getADC1(void) // 获取PB12引脚的电压(对应R38电阻)
{
uint16_t adc = 0;
HAL_ADC_Start(&hadc1);
adc = HAL_ADC_GetValue(&hadc1);
return adc;
}
static void MX_ADC1_Init(void) //ADC1_IN11初始化
{
ADC_ChannelConfTypeDef sConfig = {
0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {
0};
GPIO_InitTypeDef GPIO_InitStruct = {
0};
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); //配置ADC12的时钟
__HAL_RCC_ADC12_CLK_ENABLE(); //使能ADC12的时钟
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; //分频系数
hadc1.Init.Resolution = ADC_RESOLUTION_12B; //ADC数据位数
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; //ADC数据对其
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
HAL_ADC_Init(&hadc1); //初始化ADC1
sConfig.Channel = ADC_CHANNEL_11; //设置采样通道为 11(PB12引脚)
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
HAL_ADC_ConfigChannel(&hadc1, &sConfig); //初始化ADC采样通道
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // 初始化PB12引脚为模拟模式
}
uint16_t getADC2(void) // 获取PB15引脚的电压(对应R37电阻)
{
uint16_t adc = 0;
HAL_ADC_Start(&hadc2);
adc = HAL_ADC_GetValue(&hadc2);
return adc;
}
static void MX_ADC2_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {
0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {
0};
GPIO_InitTypeDef GPIO_InitStruct = {
0};
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__HAL_RCC_ADC12_CLK_ENABLE();
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
HAL_ADC_Init(&hadc2);
sConfig.Channel = ADC_CHANNEL_15; //设置采样通道为 15(PB15引脚)
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
HAL_ADC_ConfigChannel(&hadc2, &sConfig);
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); // 初始化PB12引脚为模拟模式
}
⑤ 在main()
中调用 MX_ADC1_Init();
, MX_ADC2_Init();
如下图所示,在main()
函数的适当位置调用 MX_ADC1_Init();
, MX_ADC2_Init();
⑥ 获取电压值
在main()
的while()
中插入如下代码:
char buf[64] = {
0};
sprintf(buf , "ADC1(R38): %.2f V", (3.3 * getADC1())/4096);
LCD_DisplayStringLine(Line8, (uint8_t *)buf);
sprintf(buf , "ADC2(R37): %.2f V", (3.3 * getADC2())/4096);
LCD_DisplayStringLine(Line9, (uint8_t *)buf);
⑦ 运行效果图
ADC使用步骤总结如下:
stm32g4xx_hal_adc.c
,stm32g4xx_hal_adc_ex.c
stm32g4xx_hal_conf.h
,取消注释 HAL_ADC_MODULE_ENABLED
#include "stm32g4xx_hal_adc.h"
main()
上面;MX_ADC1_Init();
, MX_ADC2_Init();
getADC1()
,getADC2()
获取ADC值如果此文章对你有帮助,欢迎点赞
关注
收藏
转发
.