请选择 进入手机版 | 继续访问电脑版

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

STM32G491评测(2)DAC

[复制链接]
wenyangzeng 提问时间:2021-3-22 19:49 / 已解决
STM32G491评测(2)DAC
STM32G491内置4通道12位DAC。配合DMA可以高效产生各种波形,
本次评测试用DAC通道1配合DMA数据传输,以查表方式产生1KHZ正弦波。

在CubeMX中选择STM32G491芯片,配置系统时钟150MHZ.
1.png

使能DAC1
2.png


配置DAC1通道参数
3.png


配置DMA参数
4.png


使能TIM2
5.png

使能TIM6
6.png

生成MDK工程
要产生正弦波,需要在MX_DAC1_Init()函数末尾添加代码:
  1. if (HAL_DACEx_SelfCalibrate(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  2.   {
  3.     Error_Handler();
  4.   }
  5.         sDacConfig = sConfig;
复制代码

main()函数添加代码
  1. #include "main.h"
  2. #include "dac.h"
  3. #include "dma.h"
  4. #include "tim.h"
  5. #include "gpio.h"

  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */

  8. /* USER CODE END Includes */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* USER CODE BEGIN PTD */
  11. typedef enum
  12. {
  13.         DAC_WAVE_SAWTOOTH,
  14.         DAC_WAVE_SINE

  15.   
  16. } t_wavetype;
  17. /* USER CODE END PTD */

  18. /* Private define ------------------------------------------------------------*/
  19. /* USER CODE BEGIN PD */
  20. /* USER CODE END PD */

  21. /* Private macro -------------------------------------------------------------*/
  22. /* USER CODE BEGIN PM */

  23. /* USER CODE END PM */

  24. /* Private variables ---------------------------------------------------------*/

  25. /* USER CODE BEGIN PV */
  26. __IO uint8_t ubKeyPressed = RESET;
  27. __IO t_wavetype ubSelectedWavesForm = DAC_WAVE_SINE;
  28. /* USER CODE END PV */

  29. /* Private function prototypes -----------------------------------------------*/
  30. void SystemClock_Config(void);
  31. /* USER CODE BEGIN PFP */
  32. #define SAWTOOTH_NB_STEPS  60

  33. uint16_t sinewave[60] = {
  34. 0x07ff,0x08cb,0x0994,0x0a5a,0x0b18,0x0bce,0x0c79,0x0d18,0x0da8,0x0e29,0x0e98,0x0ef4,0x0f3e,0x0f72,0x0f92,0x0f9d,
  35. 0x0f92,0x0f72,0x0f3e,0x0ef4,0x0e98,0x0e29,0x0da8,0x0d18,0x0c79,0x0bce,0x0b18,0x0a5a,0x0994,0x08cb,0x07ff,0x0733,
  36. 0x066a,0x05a4,0x04e6,0x0430,0x0385,0x02e6,0x0256,0x01d5,0x0166,0x010a,0x00c0,0x008c,0x006c,0x0061,0x006c,0x008c,
  37. 0x00c0,0x010a,0x0166,0x01d5,0x0256,0x02e6,0x0385,0x0430,0x04e6,0x05a4,0x066a,0x0733};

  38. DAC_ChannelConfTypeDef sDacConfig = {0};
  39. static void DAC_ChangeWave(t_wavetype wave);

  40. /* USER CODE END PFP */

  41. /* Private user code ---------------------------------------------------------*/
  42. /* USER CODE BEGIN 0 */

  43. /* USER CODE END 0 */


  44. int main(void)
  45. {
  46.   /* USER CODE BEGIN 1 */

  47.   /* USER CODE END 1 */

  48.   /* MCU Configuration--------------------------------------------------------*/

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

  51.   /* USER CODE BEGIN Init */

  52.   /* USER CODE END Init */

  53.   /* Configure the system clock */
  54.   SystemClock_Config();

  55.   /* USER CODE BEGIN SysInit */

  56.   /* USER CODE END SysInit */

  57.   /* Initialize all configured peripherals */
  58.   MX_GPIO_Init();
  59.   MX_DMA_Init();
  60.   MX_DAC1_Init();
  61.   MX_TIM2_Init();
  62.   MX_TIM6_Init();
  63.   /* USER CODE BEGIN 2 */

  64.   if (HAL_TIM_Base_Start(&htim6) != HAL_OK)
  65.   {
  66.     Error_Handler();
  67.   }

  68.         ubSelectedWavesForm = DAC_WAVE_SINE;
  69.         DAC_ChangeWave(ubSelectedWavesForm);

  70.   while (1)
  71.   {

  72.     /* USER CODE END WHILE */

  73.     /* USER CODE BEGIN 3 */
  74.   }
  75.   /* USER CODE END 3 */
  76. }

  77. /**
  78.   * @brief System Clock Configuration
  79.   * @retval None
  80.   */
  81. void SystemClock_Config(void)
  82. {
  83.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  84.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  85.   /** Configure the main internal regulator output voltage
  86.   */
  87.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  88.   /** Initializes the RCC Oscillators according to the specified parameters
  89.   * in the RCC_OscInitTypeDef structure.
  90.   */
  91.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  92.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  93.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  94.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  95.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  96.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
  97.   RCC_OscInitStruct.PLL.PLLN = 75;
  98.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  99.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  100.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  101.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  102.   {
  103.     Error_Handler();
  104.   }
  105.   /** Initializes the CPU, AHB and APB buses clocks
  106.   */
  107.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  108.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  109.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  110.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  111.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  112.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  113.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  114.   {
  115.     Error_Handler();
  116.   }
  117. }

  118. /* USER CODE BEGIN 4 */
  119. static void DAC_ChangeWave(t_wavetype wave)
  120. {
  121.   uint32_t tmp;
  122.   
  123.   /* Suspend Time Base triggers */
  124.   if (HAL_TIM_Base_Stop(&htim2) != HAL_OK)
  125.   {
  126.     /* Counter enable error */
  127.     Error_Handler();
  128.   }
  129.   if (HAL_TIM_Base_Stop(&htim6) != HAL_OK)
  130.   {
  131.     /* Counter enable error */
  132.     Error_Handler();
  133.   }
  134.   
  135.   switch(wave)
  136.   {
  137.     case DAC_WAVE_SAWTOOTH:
  138.       if (HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_1) != HAL_OK)
  139.       {
  140.         /* DAC conversion start error */
  141.         Error_Handler();
  142.       }
  143.       break;
  144.     case DAC_WAVE_SINE:
  145.       if (HAL_DAC_Stop(&hdac1, DAC_CHANNEL_1) != HAL_OK)
  146.       {
  147.         /* DAC conversion start error */
  148.         Error_Handler();
  149.       }
  150.       break;
  151.     default:
  152.       Error_Handler();
  153.       break;
  154.   }

  155.   /* Re-configure DAC */
  156.   tmp = sDacConfig.DAC_Trigger;
  157.   sDacConfig.DAC_Trigger = sDacConfig.DAC_Trigger2;
  158.   sDacConfig.DAC_Trigger2 = tmp;
  159.   if (HAL_DAC_ConfigChannel(&hdac1, &sDacConfig, DAC_CHANNEL_1) != HAL_OK)
  160.   {
  161.     Error_Handler();
  162.   }
  163.   
  164.   /* Generate new wave */
  165.   switch(wave)
  166.   {
  167.     case DAC_WAVE_SAWTOOTH:
  168.       if (HAL_DACEx_SawtoothWaveGenerate(&hdac1, DAC_CHANNEL_1, DAC_SAWTOOTH_POLARITY_INCREMENT, 0, 0x10000/SAWTOOTH_NB_STEPS) != HAL_OK)
  169.       {
  170.         Error_Handler();
  171.       }
  172.       if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK)
  173.       {
  174.         /* DAC conversion start error */
  175.         Error_Handler();
  176.       }
  177.       break;
  178.     case DAC_WAVE_SINE:
  179.       if (HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1,
  180.                             (uint32_t *)sinewave,
  181.                             60,
  182.                             DAC_ALIGN_12B_R
  183.                            ) != HAL_OK)
  184.       {
  185.         /* DAC conversion start error */
  186.         Error_Handler();
  187.       }
  188.       break;
  189.     default:
  190.       Error_Handler();
  191.       break;
  192.   }
  193.   
  194.   /* Resume Time Base triggers */
  195.   if (HAL_TIM_Base_Start(&htim2) != HAL_OK)
  196.   {
  197.     /* Counter enable error */
  198.     Error_Handler();
  199.   }
  200.   if (HAL_TIM_Base_Start(&htim6) != HAL_OK)
  201.   {
  202.     /* Counter enable error */
  203.     Error_Handler();
  204.   }
  205. }
复制代码


运行结果
7.png
STM32G491DAC.rar (637 KB, 下载次数: 12)
收藏 评论0 发布时间:2021-3-22 19:49

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版