请写出STM32F103ZET6的12位DAC来生成并输出1KHz的正弦波的代码,为了降低MCU使用率,不要使用中断,最好采用DMA。
时间: 2023-03-31 17:01:33 浏览: 102
以下是生成并输出1KHz正弦波的代码,使用STM32F103ZET6的12位DAC,采用DMA方式:
#include "stm32f10x.h"
#define DAC_DHR12R1_Address x40007408
const uint16_t Sine12bit[32] = {
2048, 2447, 2831, 3185, 3498, 3768, 3989, 4157,
4269, 4321, 4311, 4239, 4107, 3919, 368, 3396,
3072, 2714, 2333, 1936, 153, 1124, 724, 337,
69, 7, 161, 530, 1101, 1771, 2535, 3375
};
void DAC_DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC_DHR12R1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 32;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
DMA_Cmd(DMA1_Channel3, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
int main(void)
{
DAC_DMA_Configuration();
while (1) {
// do something else
}
}
阅读全文