搜索
ST MCU Finder
安装免费手机应用,
寻找理想的ST MCU

[STM32F0] 抽奖中了一个F072开发板,分享使用其DAC

[复制链接]
177|5
 楼主 | 2020-10-19 19:59 | 显示全部楼层 |阅读模式
本帖最后由 wuxiubo 于 2020-10-19 20:00 编辑

上个礼拜有幸参加抽奖中了一块F072的开发板。就是下图:
966cadbad84042ba7d6dc9ea98f0964.jpg
3f4da3ab3143a3dacfb8d0142403269.jpg
板子上带有ST-LINK仿真器,不用外接仿真器十分方便。
1603108515(1).jpg
之后就是在CUBEMX上看其资源,发现居然还带了DAC功能,果断尝试了下。
效果如下:
38e32259ca60dc1d749b0aa030f6402.jpg
fb5f0430cd2cb1d1388a8801e83fbc8.jpg
使用生成代码,在加上一些其他代码就完成了正弦波的输出,然后用示波器测量下即可。
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2020 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 "dac.h"
  23. #include "dma.h"
  24. #include "tim.h"
  25. #include "gpio.h"

  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */

  28. /* USER CODE END Includes */

  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. const uint16_t Sine12bit[100]={
  32. 0x0800,0x0881,0x0901,0x0980,0x09FD,0x0A79,0x0AF2,0x0B68,0x0BDA,0x0C49,
  33. 0x0CB3,0x0D19,0x0D79,0x0DD4,0x0E29,0x0E78,0x0EC0,0x0F02,0x0F3C,0x0F6F,
  34. 0x0F9B,0x0FBF,0x0FDB,0x0FEF,0x0FFB,0x0FFF,0x0FFB,0x0FEF,0x0FDB,0x0FBF,
  35. 0x0F9B,0x0F6F,0x0F3C,0x0F02,0x0EC0,0x0E78,0x0E29,0x0DD4,0x0D79,0x0D19,
  36. 0x0CB3,0x0C49,0x0BDA,0x0B68,0x0AF2,0x0A79,0x09FD,0x0980,0x0901,0x0881,
  37. 0x0800,0x077F,0x06FF,0x0680,0x0603,0x0587,0x050E,0x0498,0x0426,0x03B7,
  38. 0x034D,0x02E7,0x0287,0x022C,0x01D7,0x0188,0x0140,0x00FE,0x00C4,0x0091,
  39. 0x0065,0x0041,0x0025,0x0011,0x0005,0x0001,0x0005,0x0011,0x0025,0x0041,
  40. 0x0065,0x0091,0x00C4,0x00FE,0x0140,0x0188,0x01D7,0x022C,0x0287,0x02E7,
  41. 0x034D,0x03B7,0x0426,0x0498,0x050E,0x0587,0x0603,0x0680,0x06FF,0x077F,
  42. };
  43. uint16_t DualSine12bit[100];
  44. /* USER CODE END PTD */

  45. /* Private define ------------------------------------------------------------*/
  46. /* USER CODE BEGIN PD */
  47. /* USER CODE END PD */

  48. /* Private macro -------------------------------------------------------------*/
  49. /* USER CODE BEGIN PM */

  50. /* USER CODE END PM */

  51. /* Private variables ---------------------------------------------------------*/

  52. /* USER CODE BEGIN PV */

  53. /* USER CODE END PV */

  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. /* USER CODE BEGIN PFP */

  57. /* USER CODE END PFP */

  58. /* Private user code ---------------------------------------------------------*/
  59. /* USER CODE BEGIN 0 */

  60. /* USER CODE END 0 */

  61. /**
  62.   * [url=home.php?mod=space&uid=247401]@brief[/url]  The application entry point.
  63.   * @retval int
  64.   */
  65. int main(void)
  66. {
  67.   /* USER CODE BEGIN 1 */
  68. uint32_t Idx;        //定义变量
  69.   /* USER CODE END 1 */

  70.   /* MCU Configuration--------------------------------------------------------*/

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

  73.   /* USER CODE BEGIN Init */

  74.   /* USER CODE END Init */

  75.   /* Configure the system clock */
  76.   SystemClock_Config();

  77.   /* USER CODE BEGIN SysInit */

  78.   /* USER CODE END SysInit */

  79.   /* Initialize all configured peripherals */
  80.   MX_GPIO_Init();
  81.   MX_DMA_Init();
  82.   MX_DAC_Init();
  83.   MX_TIM6_Init();
  84.   /* USER CODE BEGIN 2 */
  85. /*读入正弦波数据*/
  86. for (Idx = 0; Idx <100; Idx++)  
  87.         {  
  88.                 DualSine12bit[Idx] = (Sine12bit[Idx] << 16)+ (Sine12bit[Idx]);  
  89.         }
  90.         HAL_TIM_Base_Start(&htim6);
  91.     HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);
  92.   /* USER CODE END 2 */

  93.   /* Infinite loop */
  94.   /* USER CODE BEGIN WHILE */
  95.   while (1)
  96.   {
  97.     /* USER CODE END WHILE */

  98.     /* USER CODE BEGIN 3 */
  99.   }
  100.   /* USER CODE END 3 */
  101. }

  102. /**
  103.   * @brief System Clock Configuration
  104.   * @retval None
  105.   */
  106. void SystemClock_Config(void)
  107. {
  108.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  109.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  110.   /** Initializes the RCC Oscillators according to the specified parameters
  111.   * in the RCC_OscInitTypeDef structure.
  112.   */
  113.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
  114.   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  115.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  116.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  117.   {
  118.     Error_Handler();
  119.   }
  120.   /** Initializes the CPU, AHB and APB buses clocks
  121.   */
  122.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  123.                               |RCC_CLOCKTYPE_PCLK1;
  124.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI48;
  125.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  126.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  127.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  128.   {
  129.     Error_Handler();
  130.   }
  131. }

  132. /* USER CODE BEGIN 4 */

  133. /* USER CODE END 4 */

  134. /**
  135.   * @brief  This function is executed in case of error occurrence.
  136.   * @retval None
  137.   */
  138. void Error_Handler(void)
  139. {
  140.   /* USER CODE BEGIN Error_Handler_Debug */
  141.   /* User can add his own implementation to report the HAL error return state */

  142.   /* USER CODE END Error_Handler_Debug */
  143. }

  144. #ifdef  USE_FULL_ASSERT
  145. /**
  146.   * @brief  Reports the name of the source file and the source line number
  147.   *         where the assert_param error has occurred.
  148.   * @param  file: pointer to the source file name
  149.   * @param  line: assert_param error line source number
  150.   * @retval None
  151.   */
  152. void assert_failed(uint8_t *file, uint32_t line)
  153. {
  154.   /* USER CODE BEGIN 6 */
  155.   /* User can add his own implementation to report the file name and line number,
  156.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  157.   /* USER CODE END 6 */
  158. }
  159. #endif /* USE_FULL_ASSERT */

  160. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
添加的代码就几行,非常简单

使用特权

评论回复
| 2020-10-19 21:15 | 显示全部楼层
谢谢分享 自带DAC还是蛮方便的

使用特权

评论回复
| 2020-10-19 21:15 | 显示全部楼层
波形图上有寄生干扰呀

使用特权

评论回复
 楼主 | 2020-10-19 22:47 | 显示全部楼层
王栋春 发表于 2020-10-19 21:15
波形图上有寄生干扰呀

额,这个可能是他DAC的原因

使用特权

评论回复
| 2020-10-19 23:00 | 显示全部楼层
真棒,这个板子真给力。

使用特权

评论回复
 楼主 | 2020-10-20 10:50 | 显示全部楼层
wahahaheihei 发表于 2020-10-19 23:00
真棒,这个板子真给力。

嗯嗯,除了主频低点,外设还是很丰富的。

使用特权

评论回复
扫描二维码,随时随地手机跟帖
您需要登录后才可以回帖 登录 | 注册

本版积分规则

我要发帖 我要提问 投诉建议 申请版主

快速回复

您需要登录后才可以回帖
登录 | 注册
高级模式

论坛热帖

在线客服 快速回复 返回顶部 返回列表