stm32f407vet6DAC DMA正弦波代码
时间: 2023-04-06 12:00:50 浏览: 187
以下是一个简单的 stm32f407vet6 DAC DMA 正弦波代码示例:
```c
#include "stm32f4xx.h"
#define DAC_DHR12R1_ADDRESS 0x40007408
const uint16_t sin_wave[256] = {
2048, 2098, 2148, 2198, 2248, 2298, 2348, 2398,
2447, 2497, 2546, 2595, 2644, 2693, 2741, 2790,
2838, 2886, 2933, 2981, 3028, 3075, 3121, 3168,
3214, 3259, 3305, 3350, 3395, 3439, 3483, 3527,
3570, 3613, 3655, 3697, 3739, 3780, 3821, 3861,
3901, 3940, 3979, 4017, 4055, 4092, 4129, 4165,
4201, 4236, 4270, 4304, 4337, 4370, 4402, 4433,
4464, 4494, 4523, 4552, 4580, 4607, 4633, 4659,
4684, 4709, 4733, 4756, 4778, 4800, 4821, 4841,
4861, 4880, 4898, 4916, 4933, 4949, 4964, 4979,
4993, 5006, 5018, 5030, 5041, 5051, 5060, 5069,
5077, 5084, 5090, 5096, 5101, 5105, 5109, 5112,
5114, 5116, 5117, 5117, 5117, 5116, 5114, 5112,
5109, 5105, 5101, 5096, 5090, 5084, 5077, 5069,
5060, 5051, 5041, 5030, 5018, 5006, 4993, 4979,
4964, 4949, 4933, 4916, 4898, 4880, 4861, 4841,
4821, 4800, 4778, 4756, 4733, 4709, 4684, 4659,
4633, 4607, 4580, 4552, 4523, 4494, 4464, 4433,
4402, 4370, 4337, 4304, 4270, 4236, 4201, 4165,
4129, 4092, 4055, 4017, 3979, 3940, 3901, 3861,
3821, 3780, 3739, 3697, 3655, 3613, 3570, 3527,
3483, 3439, 3395, 3350, 3305, 3259, 3214, 3168,
3121, 3075, 3028, 2981, 2933, 2886, 2838, 2790,
2741, 2693, 2644, 2595, 2546, 2497, 2447, 2398,
2348, 2298, 2248, 2198, 2148, 2098, 2048, 1998,
1948, 1898, 1848, 1798, 1748, 1698, 1649, 1599,
1550, 1501, 1452, 1403, 1354, 1306, 1257, 1209,
1161, 1113, 1066, 1018, 972, 925, 879, 833,
787, 742, 697, 652, 608, 564, 520, 477,
434, 392, 350, 309, 268, 228, 189, 150,
112, 74, 37, 1, 0, 39, 78, 117,
156, 195, 234, 273, 312, 351, 390, 429,
468, 507, 546, 585, 624, 663, 702, 741,
780, 819, 858, 897, 936, 975, 1014, 1053,
1092, 1131, 1170, 1209, 1248, 1287, 1326, 1365,
1403, 1442, 1481, 1520, 1559, 1598, 1637, 1676,
1715, 1754, 1793, 1832, 1871, 1910, 1948, 1987
};
void DAC_DMA_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC_DHR12R1_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)sin_wave;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = 256;
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_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DMA_Cmd(DMA1_Stream5, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
int main(void)
{
DAC_DMA_Init();
while (1) {
}
}
```
这个代码使用 DMA 和 DAC 输出正弦波。在这个例子中,DAC 的输出引脚是 PA4,DMA 的通道是 7,DMA 的流是 5,DAC 的触发源是定时器 6 的触发输出。正弦波数据存储在 sin_wave 数组中,数组的长度是 256。在主循环中,什么也不做,因为 DMA 和 DAC 已经在后台运行了。
阅读全文