实验目的:了解ADC基本概念
电路图:
ADC(Analog-Digital Converter)模拟-数字转换器,它可以将引脚上连续变化的模拟电压转换为内存中存储的数字变量,建立模拟电路到数字电路的桥梁。
实验效果:
(一)单次非扫描实验步骤:
1. 选择通道
2. 选择为独立模式;
3. 设置数据为右对齐、非扫描模式、非连续模式。
4. 转换通道数为1,因为前面只勾选上了IN0
2、时钟配置:
ADC时钟来自RCC内部72M时钟,但是ADC时钟最大频率只能是14MHZ,而ADC的预分频器只能 选择2/4/6/8。综上所述,ADC的预分频器最大只能选择6分频,频率12MHZ
oled 配置:为方便,把前面相关博文复制一份在这:
用HAL库改写江科大的stm32入门例子4-1 OLED_江科大oled移植hal库-CSDN博客
GPIO引脚映射:(pb8、pb9)
i2c1设置:
引入oled驱动:
驱动文件在共享网盘中,有需要自行下载:
由于我们自己重新映射了iic 引脚,要在oled.中修改引脚:
main函数中引入oled.h
测试代码:
/*模块初始化*/OLED_Init(); //OLED初始化/*OLED显示*/OLED_ShowChar(1, 1, 'A'); //1行1列显示字符A
原文链接:https://blog.csdn.net/wh_xia_jun/article/details/138889005
3、main函数中校正ADC,固定写法
HAL_ADCEx_Calibration_Start(&hadc1);
4、编写转换并获取ADC函数
uint16_t StartAndGetOneResult(){HAL_ADC_Start(&hadc1); //启动ADC转换HalState= HAL_ADC_PollForConversion(&hadc1, 10);//等待转换完成if(HalState == HAL_OK){Ret=HAL_ADC_GetValue(&hadc1); //获取ADC转换结果} else{Ret=0;}//HAL_ADC_Stop(&hadc1);return Ret;
}
5、while中获取显示
while (1){ADValue= StartAndGetOneResult(); //启动ADC转换并获取结果OLED_ShowNum(1,9,ADValue,4); //显示AD转换值Voltage=(float) ADValue/ 4095 *3.3; //计算电压值OLED_ShowNum(2,9,(uint32_t)Voltage,1); //显示电压值整数部分OLED_ShowNum(2,11,((uint16_t)(Voltage * 100)) % 100,2); //显示电压值小数部分HAL_Delay(100);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}
main文件中完整代码:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "i2c.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "oled.h"/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
HAL_StatusTypeDef HalState; //HAL库函数返回状态
uint16_t Ret; //返回值
uint16_t ADValue; //AD转换值
float Voltage; //电压值
/* 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 */
//启动ADC转换并获取结果
uint16_t StartAndGetOneResult(){HAL_ADC_Start(&hadc1); //启动ADC转换HalState= HAL_ADC_PollForConversion(&hadc1, 10);//等待转换完成if(HalState == HAL_OK){Ret=HAL_ADC_GetValue(&hadc1); //获取ADC转换结果} else{Ret=0;}//HAL_ADC_Stop(&hadc1);return Ret;
}/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_ADC1_Init();MX_I2C1_Init();/* USER CODE BEGIN 2 *//*模块初始化*/OLED_Init(); //OLED初始化/*OLED显示*/OLED_ShowString(1,1,"ADValue:");OLED_ShowString(2,1,"Voltage:0.00V");HAL_ADCEx_Calibration_Start(&hadc1);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){ADValue= StartAndGetOneResult(); //启动ADC转换并获取结果OLED_ShowNum(1,9,ADValue,4); //显示AD转换值Voltage=(float) ADValue/ 4095 *3.3; //计算电压值OLED_ShowNum(2,9,(uint32_t)Voltage,1); //显示电压值整数部分OLED_ShowNum(2,11,((uint16_t)(Voltage * 100)) % 100,2); //显示电压值小数部分HAL_Delay(100);/* 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};RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;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_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* 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 */
(二)连续非扫描实验步骤:
1、与单次非扫描区别
连续模式打开 且 在代码中只需要执行一次开始转换即可,也不需要等待转换完成
完成代码如下:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "i2c.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "oled.h"/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
HAL_StatusTypeDef HalState; //HAL库函数返回状态
uint16_t Ret; //返回值
uint16_t ADValue; //AD转换值
float Voltage; //电压值
/* 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 */
//启动ADC转换并获取结果
// uint16_t StartAndGetOneResult(){
// HAL_ADC_Start(&hadc1); //启动ADC转换
// HalState= HAL_ADC_PollForConversion(&hadc1, 10);//等待转换完成
// if(HalState == HAL_OK){
// Ret=HAL_ADC_GetValue(&hadc1); //获取ADC转换结果
// } else{
// Ret=0;
// }
// //HAL_ADC_Stop(&hadc1);
// return Ret;
// }uint16_t StartAndGetOneResult(){Ret=HAL_ADC_GetValue(&hadc1);return Ret;
}/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_ADC1_Init();MX_I2C1_Init();/* USER CODE BEGIN 2 *//*模块初始化*/OLED_Init(); //OLED初始化/*OLED显示*/OLED_ShowString(1,1,"ADValue:");OLED_ShowString(2,1,"Voltage:0.00V");HAL_ADCEx_Calibration_Start(&hadc1);HAL_ADC_Start(&hadc1);//只执行一次开始转换/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){ADValue= StartAndGetOneResult(); //获取结果OLED_ShowNum(1,9,ADValue,4); //显示AD转换值Voltage=(float) ADValue/ 4095 *3.3; //计算电压值OLED_ShowNum(2,9,(uint32_t)Voltage,1); //显示电压值整数部分OLED_ShowNum(2,11,((uint16_t)(Voltage * 100)) % 100,2); //显示电压值小数部分HAL_Delay(100);/* 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};RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;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_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* 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 */
在无论是连续模式还是非连续模式下,一旦执行了HAL_ADC_Start(&hadc1);函数,ADC开始转换。
在非连续模式下,您需要使用HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);函数来等待转换完成,然后使用HAL_ADC_GetValue函数来读取ADC值。此函数会阻塞直到转换完成或者超时。
在连续模式下,由于转换持续进行,您可以直接使用HAL_ADC_GetValue函数来读取最新的ADC值,无需等待转换完成。
说明:
一、简介
它是一个12位逐次逼近形的ADC,转换一次耗时为1微妙(即转换频率为1MHZ),输入的电压范围是0~3.3V,对应的转换结果是0~4095(2^12),我理解,就是一个电子的电压与数值的字典表而已,把电压值映射为4059个数字。
二、逐次逼近形ADC原理
这里借助一个专门用于8通道的ADC转换芯片ADC0809的电路来举例,STM32中的ADC转换跟ADC0809在原理上是一样的。
1. ADDA/ADDB/ADDC/ALE是用来控制通道0~通道7中的一个通道接入芯片内部的。相当于是一个选择器。
2. Vref+/Vref-是数模转换器的电压参考值,是由一个模拟量来输出一个控制量。模拟量存在SAR中,数字量是模拟量通过DAC转换出来的电压,它的范围在Vref+/Vref-之间,DAC输出的这个数值量会输送到比较器,比较器会比较数字量与输入电压大小,如果DAC输出的数字量电压小于输入
电压,那么模拟量SAR会自动调大,输出一个新的DAC转换出来的数字量,不断循环比较。
3. 当逼近完成后(12位的ADC,利用二分法对比12次后),即DAC输出电压跟输入电压相当时,会给出一个EOC信号。这个结果通过三态锁存缓冲器输出。
三、STM32ADC框图
ps:30. ADC—电压采集 — [野火]STM32 HAL库开发实战指南——基于野火霸天虎开发板 文档
1、输入并转换
16个可选通道(F103C8T6只有10个通道)、1个芯片自带用于测量芯片温度的传感器、1个测试参考电压
2、输出
AD转换可分为两种模式
注入通道:最多只能选4个通道,且转换出来的结果会分别存放到4个寄存器中
规则通道:最多可选16个通道,但是只有一个寄存器用于存放结果,假如不想被覆盖,可以利用DMA请求把转换出来的结果依次转运出来。
本实验用规则通道。
输出的结果是12位的,而寄存器是16位的,所以结果存放在寄存器中就有两种模式
左对齐:左对齐的数据相当于真实值左移了4次,即放大了16倍右对齐:获得的数据就是真实值
右对齐:获得的数据就是真实值
3、触发转换方式
- 软件触发
- 硬件触发
4、时钟
ADC时钟来自RCC内部72M时钟,但是ADC时钟最大频率只能是14MHZ,而ADC的预分频器只能选择 2/4/6/8。综上所述,ADC的预分频器最大只能选择6分频,频率12MHZ.
5、四种模式
分两个维度: 单次/连续:转换后是否继续自动转换,单次就不继续自动转换 扫描/非扫描:是否支持多通道,扫描就可以多通道,组合起来四种模式!