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

STM32G431RBTx 基本模块 DAC

[复制链接]
STMCU小助手 发布时间:2023-3-1 20:48
前言
前面我们配置了ADC模块,本节我们学习配置DAC模块。

1.CubeMx的配置步骤
修改之前的ADC的ioc文件来生成工程。

6d23f81c1adf478f9d2dc519efb0939f.png

2.生成工程

141aa7495cc645bfb20f16fb98132a97.png

点击GENERATE CODE生成代码,再在bsp文件夹建立bdac.h和bdac.c,并加入工程中。

d3f75df69e904349b8fb6a022be6af69.png

7ac7def01da34d2487b930cac9e45025.png

至此工程就建立完毕了。


3.测试代码
bdac.h:
  1. #ifndef __BDAC_H__
  2. #define __BDAC_H__

  3. #include "main.h"

  4. void setDAC(DAC_HandleTypeDef *hdac, unsigned int Channel, float Vol);

  5. #endif
复制代码


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

  2. void setDAC(DAC_HandleTypeDef *hdac, unsigned int Channel, float Vol)
  3. {
  4.         unsigned int temp;
  5.         temp = (unsigned int)(Vol / 3.3f * 4096);
  6.         HAL_DAC_SetValue(hdac, Channel, DAC_ALIGN_12B_R, temp);
  7. }
复制代码

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 "dac.h"
  24. #include "tim.h"
  25. #include "gpio.h"

  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. #include "lcd.h"
  29. #include "badc.h"
  30. #include "stdio.h"
  31. #include "bdac.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. float Vol1 = 0;
  45. /* USER CODE END PV */

  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. /* USER CODE BEGIN PFP */

  49. /* USER CODE END PFP */

  50. /* Private user code ---------------------------------------------------------*/
  51. /* USER CODE BEGIN 0 */

  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.   /* USER CODE END 1 */

  61.   /* MCU Configuration--------------------------------------------------------*/

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

  64.   /* USER CODE BEGIN Init */

  65.   /* USER CODE END Init */

  66.   /* Configure the system clock */
  67.   SystemClock_Config();

  68.   /* USER CODE BEGIN SysInit */

  69.   /* USER CODE END SysInit */

  70.   /* Initialize all configured peripherals */
  71.   MX_GPIO_Init();
  72.   MX_TIM3_Init();
  73.   MX_TIM16_Init();
  74.   MX_TIM17_Init();
  75.   MX_ADC1_Init();
  76.   MX_ADC2_Init();
  77.   MX_DAC1_Init();
  78.   /* USER CODE BEGIN 2 */
  79.         LCD_Init();
  80.         LCD_Clear(Black);
  81.         LCD_SetBackColor(Black);
  82.         LCD_SetTextColor(White);
  83.         HAL_DAC_Start(&hdac1 , DAC_CHANNEL_1);
  84.         HAL_DAC_Start(&hdac1 , DAC_CHANNEL_2);

  85.         setDAC(&hdac1, DAC_CHANNEL_2, 3.0);
  86.   /* USER CODE END 2 */

  87.   /* Infinite loop */
  88.   /* USER CODE BEGIN WHILE */
  89.   while (1)
  90.   {
  91.     /* USER CODE END WHILE */

  92.     /* USER CODE BEGIN 3 */
  93.                 setDAC(&hdac1, DAC_CHANNEL_1, Vol1);
  94.                 char text[30];
  95.                 sprintf(text, "    V1:%.2f", getADC(&hadc1));
  96.                 LCD_DisplayStringLine(Line4, (unsigned char *)text);
  97.                 sprintf(text, "    V2:%.2f", getADC(&hadc2));
  98.                 LCD_DisplayStringLine(Line5, (unsigned char *)text);
  99.                 Vol1 += 0.1;
  100.                 if(Vol1 == 3.4)
  101.                 {
  102.                         Vol1 = 0;
  103.                 }
  104.                 HAL_Delay(500);
  105.   }
  106.   /* USER CODE END 3 */
  107. }

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

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

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

  157. /* USER CODE BEGIN 4 */

  158. /* USER CODE END 4 */

  159. /**
  160.   * @brief  This function is executed in case of error occurrence.
  161.   * @retval None
  162.   */
  163. void Error_Handler(void)
  164. {
  165.   /* USER CODE BEGIN Error_Handler_Debug */
  166.   /* User can add his own implementation to report the HAL error return state */
  167.   __disable_irq();
  168.   while (1)
  169.   {
  170.   }
  171.   /* USER CODE END Error_Handler_Debug */
  172. }

  173. #ifdef  USE_FULL_ASSERT
  174. /**
  175.   * @brief  Reports the name of the source file and the source line number
  176.   *         where the assert_param error has occurred.
  177.   * @param  file: pointer to the source file name
  178.   * @param  line: assert_param error line source number
  179.   * @retval None
  180.   */
  181. void assert_failed(uint8_t *file, uint32_t line)
  182. {
  183.   /* USER CODE BEGIN 6 */
  184.   /* User can add his own implementation to report the file name and line number,
  185.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  186.   /* USER CODE END 6 */
  187. }
  188. #endif /* USE_FULL_ASSERT */

  189. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

4.演示效果

7711860fb94f48dd893b63fcf197c6c5.gif

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


收藏 评论0 发布时间:2023-3-1 20:48

举报

0个回答

所属标签

相似分享

官网相关资源

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