【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波

一、平台
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第1张图片
二、配置
1、DAC的配置
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第2张图片【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第3张图片
输出缓冲Output Buffer建议关闭。DAC的输出缓存可以用来减小输出阻抗,无需外部运放即可直接驱动外部负载。但输出不能达到0(会出现底部失真),不推荐开启。具体参考官方手册,缓冲器打开时低端的DAC输出电压大概0.2V(不同型号可能不同,暂未考证)
在这里插入图片描述
2、DMA的配置
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第4张图片
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第5张图片
DMA的工作模式:Circular

3、TIM6的配置
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第6张图片
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第7张图片
定时器的配置决定了输出的正弦波的频率。
【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第8张图片
定时器时钟设为72M。
正弦波的频率=主频 /(TIM_Prescaler+1)/(TIM_Period+1)/ 点数
选100个点
f=72M/(5+1)/(5+1)/100=20kHz.

三、生成代码
需要添加的:
在mian.c中定义查表的数组,保存于静态内存。

const uint16_t Sine12bit[100]={
0x0800,0x0881,0x0901,0x0980,0x09FD,0x0A79,0x0AF2,0x0B68,0x0BDA,0x0C49,
0x0CB3,0x0D19,0x0D79,0x0DD4,0x0E29,0x0E78,0x0EC0,0x0F02,0x0F3C,0x0F6F,
0x0F9B,0x0FBF,0x0FDB,0x0FEF,0x0FFB,0x0FFF,0x0FFB,0x0FEF,0x0FDB,0x0FBF,
0x0F9B,0x0F6F,0x0F3C,0x0F02,0x0EC0,0x0E78,0x0E29,0x0DD4,0x0D79,0x0D19,
0x0CB3,0x0C49,0x0BDA,0x0B68,0x0AF2,0x0A79,0x09FD,0x0980,0x0901,0x0881,
0x0800,0x077F,0x06FF,0x0680,0x0603,0x0587,0x050E,0x0498,0x0426,0x03B7,
0x034D,0x02E7,0x0287,0x022C,0x01D7,0x0188,0x0140,0x00FE,0x00C4,0x0091,
0x0065,0x0041,0x0025,0x0011,0x0005,0x0001,0x0005,0x0011,0x0025,0x0041,
0x0065,0x0091,0x00C4,0x00FE,0x0140,0x0188,0x01D7,0x022C,0x0287,0x02E7,
0x034D,0x03B7,0x0426,0x0498,0x050E,0x0587,0x0603,0x0680,0x06FF,0x077F,
};
uint16_t DualSine12bit[100];

在main函数中读取数组并输出

int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t Idx;        //定义变量
  /* USER CODE END 1 */
  

  /* MCU Configuration--------------------------------------------------------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_DAC_Init();
  MX_TIM6_Init();
  /* USER CODE BEGIN 2 */
  /*读入正弦波数据*/
	 for (Idx = 0; Idx <100; Idx++)  
	{  
		DualSine12bit[Idx] = (Sine12bit[Idx] << 16)+ (Sine12bit[Idx]);  
	}
	HAL_TIM_Base_Start(&htim6);
    HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1,(uint32_t *)DualSine12bit,100,DAC_ALIGN_12B_R);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

四、示波器观察结果

【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波_第9张图片
工程链接:https://download.csdn.net/download/qq_21246169/12523781

你可能感兴趣的:(【HAL】stm32F103 DAC+DMA+TIM6查表法产生正弦波)