你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32G431RBTx 基本模块 ADC

[复制链接]
STMCU小助手 发布时间:2023-3-1 22:33
前言
G4板载了四个电位器,其中左边两个是电压采集用的,本篇文章将讲述如何单片机的adc功能采集左边两个滑变的电压值。

ADC
1.原理图以及配置元素


4f67da4e054240519ffcc6e90163b43e.png

分析:由图知,PB15作为左边第一个旋钮的采集引脚,PB12作为左边第二个旋钮的采集引脚。


2.CubeMx的配置步骤

86e14b33069c4746a6cc37c2d0532deb.png

将PB12设置为ADC1的通道11,PB15设置为ADC2的通道15。进入Analog中的ADC1和ADC2。

b940f4eb69bb4a42bc5a73b06e99c877.png

只需激活ADC1通道11的Single-ended采集功能,不需要其他配置。
8b6b035ed9564a88a7d5688d13868b79.png

只需激活ADC2通道15的Single-ended采集功能,不需要其他配置。

3.生成工程

802590c17c294ec586a1155632ef3654.png

点击完生成代码之后进入工程文件夹中的bsp文件夹

af60d6879d7b42c9941c7f654deb0f93.png

在bsp文件中新建badc.c和badc.h文件(不用adc.c和adc.h命名是为了防止与CubeMx生成的文件起冲突)
打开工程将两个文件添加到工程中去

f2476093726d4efe9b95551485c7d056.png

至此工程就建立完毕了

4.测试代码

badc.h:
  1. #ifndef __BADC_H__
  2. #define __BADC_H__

  3. #include "main.h"
  4. double getADC(ADC_HandleTypeDef *hadc);

  5. #endif

复制代码

badc.c:
  1. #include "badc.h"

  2. double getADC(ADC_HandleTypeDef *hadc)
  3. {
  4.         unsigned int adc;
  5.         HAL_ADC_Start(hadc);
  6.         adc = HAL_ADC_GetValue(hadc);
  7.         return adc * 3.3 / 4096;
  8. }

复制代码

main.c:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2023 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "adc.h"
  23. #include "tim.h"
  24. #include "gpio.h"

  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "led.h"
  28. #include "lcd.h"
  29. #include "stdio.h"
  30. #include "interrupt.h"
  31. #include "badc.h"
  32. /* USER CODE END Includes */

  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */

  35. /* USER CODE END PTD */

  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */

  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */

  41. /* USER CODE END PM */

  42. /* Private variables ---------------------------------------------------------*/

  43. /* USER CODE BEGIN PV */
  44. extern struct keys key[];
  45. unsigned char pa6_duty = 10; //设置PA6初始PWM波占空比为10%
  46. unsigned char pa7_duty = 10; //设置PA7初始PWM波占空比为10%
  47. /* USER CODE END PV */

  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. /* USER CODE BEGIN PFP */
  51. void Dispose_Key(void);
  52. void LCD_Display(void);
  53. /* USER CODE END PFP */

  54. /* Private user code ---------------------------------------------------------*/
  55. /* USER CODE BEGIN 0 */

  56. /* USER CODE END 0 */

  57. /**
  58.   * @brief  The application entry point.
  59.   * @retval int
  60.   */
  61. int main(void)
  62. {
  63.   /* USER CODE BEGIN 1 */

  64.   /* USER CODE END 1 */

  65.   /* MCU Configuration--------------------------------------------------------*/

  66.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  67.   HAL_Init();

  68.   /* USER CODE BEGIN Init */

  69.   /* USER CODE END Init */

  70.   /* Configure the system clock */
  71.   SystemClock_Config();

  72.   /* USER CODE BEGIN SysInit */

  73.   /* USER CODE END SysInit */

  74.   /* Initialize all configured peripherals */
  75.   MX_GPIO_Init();
  76.   MX_TIM3_Init();
  77.   MX_TIM16_Init();
  78.   MX_TIM17_Init();
  79.   MX_ADC1_Init();
  80.   MX_ADC2_Init();
  81.   /* USER CODE BEGIN 2 */
  82.         LCD_Init();
  83.   /* USER CODE END 2 */

  84.   /* Infinite loop */
  85.   /* USER CODE BEGIN WHILE */
  86.         LCD_Clear(Black);        //清屏并将背景设为黑色
  87.         LCD_SetBackColor(Black); //设置字体背景颜色为黑色
  88.         LCD_SetTextColor(White); //设置字体文本颜色为白色

  89.        
  90.         HAL_TIM_Base_Start_IT(&htim3);
  91.         HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
  92.         HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
  93.         LED_Disp(0x00);
  94.   while (1)
  95.   {
  96.     /* USER CODE END WHILE */

  97.     /* USER CODE BEGIN 3 */
  98.                
  99.                 Dispose_Key();
  100.     LCD_Display();
  101.                 char text[30];
  102.                 sprintf(text, "    V1:%.2f", getADC(&hadc1));
  103.                 LCD_DisplayStringLine(Line6, (unsigned char *)text);
  104.                 sprintf(text, "    V2:%.2f", getADC(&hadc2));
  105.                 LCD_DisplayStringLine(Line7, (unsigned char *)text);
  106.   }
  107.   /* USER CODE END 3 */
  108. }

  109. /**
  110.   * @brief System Clock Configuration
  111.   * @retval None
  112.   */
  113. void SystemClock_Config(void)
  114. {
  115.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  116.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  117.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  118.   /** Configure the main internal regulator output voltage
  119.   */
  120.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  121.   /** Initializes the RCC Oscillators according to the specified parameters
  122.   * in the RCC_OscInitTypeDef structure.
  123.   */
  124.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  125.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  126.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  127.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  128.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
  129.   RCC_OscInitStruct.PLL.PLLN = 20;
  130.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  131.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  132.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  133.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  134.   {
  135.     Error_Handler();
  136.   }
  137.   /** Initializes the CPU, AHB and APB buses clocks
  138.   */
  139.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  141.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  144.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  145.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  146.   {
  147.     Error_Handler();
  148.   }
  149.   /** Initializes the peripherals clocks
  150.   */
  151.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
  152.   PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
  153.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  154.   {
  155.     Error_Handler();
  156.   }
  157. }

  158. /* USER CODE BEGIN 4 */
  159. void Dispose_Key(void)
  160. {
  161.         if(key[1].single_flag == 1)
  162.         {
  163.                 pa6_duty += 10; //每次B2按键按下PA6占空比加10%
  164.                 if(pa6_duty > 90) //加到90%重新返回10%
  165.                         pa6_duty = 10;
  166.                 __HAL_TIM_SetCompare(&htim16, TIM_CHANNEL_1, pa6_duty);//将pa6_duty的值设置到比较寄存器中
  167.                 key[1].single_flag = 0;
  168.         }
  169.         if(key[2].single_flag == 1)
  170.         {
  171.                 pa7_duty += 10; //每次B2按键按下PA6占空比加10%
  172.                 if(pa7_duty > 90) //加到90%重新返回10%
  173.                         pa7_duty = 10;
  174.                 __HAL_TIM_SetCompare(&htim17, TIM_CHANNEL_1, pa7_duty);//将pa6_duty的值设置到比较寄存器中
  175.                 key[2].single_flag = 0;
  176.         }
  177. }

  178. void LCD_Display(void)
  179. {
  180.         char text[30];
  181.         sprintf(text, "      Para");
  182.         LCD_DisplayStringLine(Line0, (unsigned char *)text);
  183.         sprintf(text, "    PA6:%d%%", pa6_duty);
  184.         LCD_DisplayStringLine(Line2, (unsigned char *)text);
  185.         sprintf(text, "    PA7:%d%%", pa7_duty);
  186.         LCD_DisplayStringLine(Line4, (unsigned char *)text);
  187. }

  188. /* USER CODE END 4 */

  189. /**
  190.   * @brief  This function is executed in case of error occurrence.
  191.   * @retval None
  192.   */
  193. void Error_Handler(void)
  194. {
  195.   /* USER CODE BEGIN Error_Handler_Debug */
  196.   /* User can add his own implementation to report the HAL error return state */
  197.   __disable_irq();
  198.   while (1)
  199.   {
  200.   }
  201.   /* USER CODE END Error_Handler_Debug */
  202. }

  203. #ifdef  USE_FULL_ASSERT
  204. /**
  205.   * @brief  Reports the name of the source file and the source line number
  206.   *         where the assert_param error has occurred.
  207.   * @param  file: pointer to the source file name
  208.   * @param  line: assert_param error line source number
  209.   * @retval None
  210.   */
  211. void assert_failed(uint8_t *file, uint32_t line)
  212. {
  213.   /* USER CODE BEGIN 6 */
  214.   /* User can add his own implementation to report the file name and line number,
  215.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  216.   /* USER CODE END 6 */
  217. }
  218. #endif /* USE_FULL_ASSERT */

  219. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码

5.演示效果

6feb641b679c4a7cae645a15ee0c65c3.gif

————————————————
版权声明:火花页.


收藏 评论0 发布时间:2023-3-1 22:33

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版