单片机hal库dac输出两路正弦波
时间: 2023-07-06 15:40:11 浏览: 128
DAC输出正弦波
好的,下面是使用STM32 HAL库实现DAC输出两路正弦波的代码示例:
```c
#include "stm32f4xx_hal.h"
#include <math.h>
#define PI 3.1415926f
#define SAMPLE_RATE 10000 // 采样率
#define FREQ_A 500 // 正弦波A频率
#define FREQ_B 1000 // 正弦波B频率
DAC_HandleTypeDef hdac;
TIM_HandleTypeDef htim;
uint16_t waveTableA[SAMPLE_RATE / FREQ_A];
uint16_t waveTableB[SAMPLE_RATE / FREQ_B];
uint16_t waveIndexA = 0;
uint16_t waveIndexB = 0;
void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_DAC_Init(void);
void MX_TIM6_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DAC_Init();
MX_TIM6_Init();
HAL_TIM_Base_Start(&htim);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)waveTableA, sizeof(waveTableA) / sizeof(uint16_t), DAC_ALIGN_12B_R);
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_2, (uint32_t*)waveTableB, sizeof(waveTableB) / sizeof(uint16_t), DAC_ALIGN_12B_R);
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
}
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void MX_DAC_Init(void)
{
hdac.Instance = DAC;
HAL_DAC_Init(&hdac);
DAC_ChannelConfTypeDef sConfig;
sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1);
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_2);
}
void MX_TIM6_Init(void)
{
__HAL_RCC_TIM6_CLK_ENABLE();
htim.Instance = TIM6;
htim.Init.Prescaler = SystemCoreClock / SAMPLE_RATE - 1;
htim.Init.CounterMode = TIM_COUNTERMODE_UP;
htim.Init.Period = SAMPLE_RATE / FREQ_A - 1;
htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_Base_Init(&htim);
HAL_TIM_Base_Start_IT(&htim);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if (htim->Instance == TIM6)
{
waveTableA[waveIndexA++] = (uint16_t)(2048 * sinf(2 * PI * FREQ_A * waveIndexA / SAMPLE_RATE) + 2048);
waveTableB[waveIndexB++] = (uint16_t)(2048 * sinf(2 * PI * FREQ_B * waveIndexB / SAMPLE_RATE) + 2048);
}
}
```
这段代码使用了STM32 HAL库的API函数来配置DAC和定时器,并使用DMA传输正弦波数据到DAC数据寄存器中。其中,使用了STM32F4内置的数学库函数sinf()来生成正弦波数据,而不是使用查表法预先生成正弦波数据表。
阅读全文