当前位置:   article > 正文

STM32—ADC详解入门(ADC读取烟雾传感器的值)_stm32 adc

stm32 adc

目录

一、ADC是什么

二、ADC的性能指标

三、ADC特性

四、ADC通道

五、ADC转换顺序

六、ADC触发方式

七、ADC转化时间

八、ADC转化模式

九、实验(使用ADC读取烟雾传感器的值)

1、配置

2、代码


一、ADC是什么

        ADC是Analog-to-DigitalConverter的缩写。指模/数转换器或者模拟/数字转换器。是指将连续变量的模拟信号转换为离散的数字信号的器件。典型的模拟数字转换器将模拟信号转换为表示一定比例电压值的数字信号。

二、ADC的性能指标

        1.量程:能测量的电压范围。

        2.分辨率:ADC能辨别的最小模拟量,通常以输出二进制数的位数表示,比如:8、10、12、 16位等;位数越多,分辨率越高,一般来说分辨率越高,转化时间越长。

        3.转化时间:从转换开始到获得稳定的数字量输出所需要的时间称为转换时间。

三、ADC特性

        1.转换速度:12位精度下转换速度可高达1MHZ。

        2.供电电压:V SSA :0V,V DDA :2.4V~3.6V。

        3.ADC输入范围:VREF- ≤ VIN ≤ VREF+。

        4.采样时间:采样时间可配置,采样时间越长, 转换结果相对越准确, 但是转换速度就越慢。

        5.ADC 的数据结果:可以左对齐或右对齐方式存储在 16 位数据寄存器中。

四、ADC通道

        芯片STM32F103c8T6,总共2个ADC(ADC1,ADC2),每个ADC有18个转换通道: 16个外部通道、 2个内部通道(温度 传感器、内部参考电压)。外部的16个通道在转换时又分为规则通道注入通道,其中规则通道最多有16路,注入通道最多 有4路。规则组:正常排队的人; 注入组:有特权的人。

                      

五、ADC转换顺序

        每个ADC只有一个数据寄存器,16个通道一起共用这个寄存器,所以需要指定规则转换通道的转换顺序,不然数据就会错乱。

        1.规则通道中的转换顺序由三个寄存器控制:SQR1、SQR2、SQR3,它们都是32位寄存器。SQR寄 存器控制着转换通道的数目和转换顺序,只要在对应的寄存器位SQx中写入相应的通道,这个通 道就是第x个转换。

                2.注入通道的转换也是通过注入寄存器来控制,但只有一个 JSQR寄存器来控制,控制关系如下图, 注入序列的转换顺序是从JSQx[ 4 : 0 ](x=4-JL[1:0])开始。只有当JL=4的时候,注入通道的转换 顺序才会按照JSQ1、JSQ2、JSQ3、JSQ4的顺序执行。

        

六、ADC触发方式

        1. (软件)通过向控制寄存器ADC-CR2的ADON位写1来开启转换,写0停止转换。

        2. (硬件)也可以通过外部事件(如定时器)进行转换。

七、ADC转化时间

        ADC是挂载在APB2总线(PCLK2)上的,经过分频器得到ADC时钟(ADCCLK),最高 14 MHz。

        转换时间=采样时间+12.5个周期  

        12.5个周期是固定的,一般设置 PCLK2=72M,经过 ADC 预分频器能分频到最大的时钟只能 是 12M,采样周期设置为 1.5 个周期,算出最短的转换时间为 1.17us。

八、ADC转化模式

    扫描模式:

        关闭扫描模式:只转换ADC_SQRx或ADC_JSQR选中的第一个通道。

        打开扫描模式:扫描所有被ADC_SQRx或ADC_JSQR选中的所有通道。

    单次转换/连续转换:

        单次转换:只转换一次。

        连续转换:转换一次之后,立马进行下一次转换。

九、实验(使用ADC读取烟雾传感器的值)

1、配置

         RCC

        SYS

         时钟(在七、ADC转化时间有解释最高 14 MHz) 

       串口

         ADC

         使用printf作为串口输出需设置

        接线

        芯片对应的ADC的引脚接烟雾传感器的AO引脚。

2、代码

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <stdio.h>//FILE所需头文件//====================================================
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. /* USER CODE BEGIN PFP */
  43. /* USER CODE END PFP */
  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */
  46. int fputc(int ch,FILE *f)//使用printf输出//=============================================
  47. {
  48. unsigned char temp[1] = {ch};
  49. HAL_UART_Transmit(&huart1,temp,1,0xffff);
  50. return ch;
  51. }
  52. /* USER CODE END 0 */
  53. /**
  54. * @brief The application entry point.
  55. * @retval int
  56. */
  57. int main(void)
  58. {
  59. /* USER CODE BEGIN 1 */
  60. uint32_t smoke_value = 0;//定义一个返回值类型的变量接收ADC转换数据//==================
  61. /* USER CODE END 1 */
  62. /* MCU Configuration--------------------------------------------------------*/
  63. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  64. HAL_Init();
  65. /* USER CODE BEGIN Init */
  66. /* USER CODE END Init */
  67. /* Configure the system clock */
  68. SystemClock_Config();
  69. /* USER CODE BEGIN SysInit */
  70. /* USER CODE END SysInit */
  71. /* Initialize all configured peripherals */
  72. MX_GPIO_Init();
  73. MX_ADC1_Init();
  74. MX_USART1_UART_Init();
  75. /* USER CODE BEGIN 2 */
  76. /* USER CODE END 2 */
  77. /* Infinite loop */
  78. /* USER CODE BEGIN WHILE */
  79. while (1)
  80. {
  81. /* USER CODE END WHILE */
  82. /* USER CODE BEGIN 3 */
  83. HAL_ADC_Start(&hadc1);//启动ADC1转换//======================================
  84. HAL_ADC_PollForConversion(&hadc1,50);//等待ADC转换完成(句柄,超时时间)
  85. smoke_value = HAL_ADC_GetValue(&hadc1); //读取ADC转换数据
  86. printf("smoke_value = %f\r\n", 3.3/4096 * smoke_value);//输出转换后的烟雾传感器的值的数据
  87. HAL_Delay(500);
  88. }
  89. /* USER CODE END 3 */
  90. }
  91. /**
  92. * @brief System Clock Configuration
  93. * @retval None
  94. */
  95. void SystemClock_Config(void)
  96. {
  97. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  98. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  99. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  100. /** Initializes the RCC Oscillators according to the specified parameters
  101. * in the RCC_OscInitTypeDef structure.
  102. */
  103. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  104. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  105. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  106. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  107. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  108. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  109. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  110. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  111. {
  112. Error_Handler();
  113. }
  114. /** Initializes the CPU, AHB and APB buses clocks
  115. */
  116. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  117. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  118. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  119. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  120. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  121. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  122. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  123. {
  124. Error_Handler();
  125. }
  126. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  127. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  128. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  129. {
  130. Error_Handler();
  131. }
  132. }
  133. /* USER CODE BEGIN 4 */
  134. /* USER CODE END 4 */
  135. /**
  136. * @brief This function is executed in case of error occurrence.
  137. * @retval None
  138. */
  139. void Error_Handler(void)
  140. {
  141. /* USER CODE BEGIN Error_Handler_Debug */
  142. /* User can add his own implementation to report the HAL error return state */
  143. __disable_irq();
  144. while (1)
  145. {
  146. }
  147. /* USER CODE END Error_Handler_Debug */
  148. }
  149. #ifdef USE_FULL_ASSERT
  150. /**
  151. * @brief Reports the name of the source file and the source line number
  152. * where the assert_param error has occurred.
  153. * @param file: pointer to the source file name
  154. * @param line: assert_param error line source number
  155. * @retval None
  156. */
  157. void assert_failed(uint8_t *file, uint32_t line)
  158. {
  159. /* USER CODE BEGIN 6 */
  160. /* User can add his own implementation to report the file name and line number,
  161. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  162. /* USER CODE END 6 */
  163. }
  164. #endif /* USE_FULL_ASSERT */
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/802602
推荐阅读
相关标签
  

闽ICP备14008679号