GD32F303 DAC输出方波
时间: 2023-09-18 12:10:17 浏览: 206
GD32F307 DAC 输出波形
要在GD32F303上使用DAC输出方波,可以按照以下步骤进行:
1. 初始化DAC模块并设置为触发模式:在DAC初始化代码中,需要设置DAC模块为触发模式。触发模式下,DAC会按照指定的触发源来输出模拟信号。可以选择软件触发或者外部触发。在这里,我们选择软件触发。
```
DAC_InitPara.DAC_Trigger = DAC_TRIGGER_SOFTWARE;
DAC_InitPara.DAC_WaveGeneration = DAC_WAVE_GENERATION_NONE;
DAC_InitPara.DAC_OutputBuffer = DAC_OUTPUT_BUFFER_ENABLE;
DAC_Init(DAC1, &DAC_InitPara);
```
2. 设置DAC输出DMA:使用DMA可以让DAC在后台输出数据,而不需要CPU的干预。在这里,我们将DAC的数据寄存器与DMA通道相连。
```
DMA_InitPara.DMA_PeripheralBaseAddr = (uint32_t)(&DAC1->DAT);
DMA_InitPara.DMA_MemoryBaseAddr = (uint32_t)WaveTab;
DMA_InitPara.DMA_DIR = DMA_DIR_PERIPHERALDST;
DMA_InitPara.DMA_BufferSize = WAVE_TAB_SIZE;
DMA_InitPara.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
DMA_InitPara.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
DMA_InitPara.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD;
DMA_InitPara.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD;
DMA_InitPara.DMA_Mode = DMA_MODE_CIRCULAR;
DMA_InitPara.DMA_Priority = DMA_PRIORITY_HIGH;
DMA_InitPara.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
```
3. 填充DAC输出数据:在这里,我们将DAC的输出数据设置为一个方波。可以根据需要修改方波的频率和占空比。
```
for(i = 0; i < WAVE_TAB_SIZE; i++)
{
if(i < WAVE_TAB_SIZE / 2)
{
WaveTab[i] = 0;
}
else
{
WaveTab[i] = 0xFFF;
}
}
```
4. 启动DAC输出DMA:启动DMA后,DAC会自动输出数据。
```
DMA_Enable(DMA_CHANNEL_0, ENABLE);
DAC_Enable(DAC1, ENABLE);
```
完整的代码示例可以参考以下代码:
```
#include "gd32f30x.h"
#include "systick.h"
#define WAVE_TAB_SIZE 256
uint16_t WaveTab[WAVE_TAB_SIZE];
void DAC_Configuration(void)
{
DAC_InitPara DAC_InitPara;
DMA_InitPara DMA_InitPara;
uint16_t i;
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_DMA0, ENABLE);
RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_DAC, ENABLE);
DAC_InitPara.DAC_Trigger = DAC_TRIGGER_SOFTWARE;
DAC_InitPara.DAC_WaveGeneration = DAC_WAVE_GENERATION_NONE;
DAC_InitPara.DAC_OutputBuffer = DAC_OUTPUT_BUFFER_ENABLE;
DAC_Init(DAC1, &DAC_InitPara);
for(i = 0; i < WAVE_TAB_SIZE; i++)
{
if(i < WAVE_TAB_SIZE / 2)
{
WaveTab[i] = 0;
}
else
{
WaveTab[i] = 0xFFF;
}
}
DMA_InitPara.DMA_PeripheralBaseAddr = (uint32_t)(&DAC1->DAT);
DMA_InitPara.DMA_MemoryBaseAddr = (uint32_t)WaveTab;
DMA_InitPara.DMA_DIR = DMA_DIR_PERIPHERALDST;
DMA_InitPara.DMA_BufferSize = WAVE_TAB_SIZE;
DMA_InitPara.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE;
DMA_InitPara.DMA_MemoryInc = DMA_MEMORYINC_ENABLE;
DMA_InitPara.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD;
DMA_InitPara.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD;
DMA_InitPara.DMA_Mode = DMA_MODE_CIRCULAR;
DMA_InitPara.DMA_Priority = DMA_PRIORITY_HIGH;
DMA_InitPara.DMA_MTOM = DMA_MEMTOMEM_DISABLE;
DMA_Init(DMA_CHANNEL_0, &DMA_InitPara);
DMA_Enable(DMA_CHANNEL_0, ENABLE);
DAC_Enable(DAC1, ENABLE);
}
int main(void)
{
SysTick_Init();
DAC_Configuration();
while (1)
{
delay_1ms(10);
}
}
```
阅读全文