您好,欢迎访问上海现易电子元器件有限公司网站!
STM32F103有双DAC通道,利用DMA实现2个波形输出
#include "stm32f10x.h"
//DAC1,2初始化
void dac_init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; //开启端口A时钟
RCC->APB1ENR |= RCC_APB1ENR_DACEN; //开启DAC时钟
GPIOA->CRL &= 0xff00ffff; //PA4,PA5模拟输入
DAC->CR = DAC_CR_TEN1 | DAC_CR_TEN2; //开启触发使能才能设置触发模式
//开启DAC,关闭输出缓存,TIM6 TRGO触发,DMA使能
DAC->CR |= DAC_CR_EN1 | DAC_CR_BOFF1 | DAC_CR_DMAEN1 | \
DAC_CR_EN2 | DAC_CR_BOFF2 | DAC_CR_DMAEN2;
//必须关闭缓冲才能输出低于0.5V电压
}
//DMA2初始化
void dma2_init(unsigned int cpar, unsigned int cmar, unsigned int cndtr)
{
RCC->AHBENR |= RCC_AHBENR_DMA2EN; //开启DMA2时钟
DMA2_Channel3->CPAR = cpar; //外设地址
DMA2_Channel3->CMAR = cmar; //存储器地址
DMA2_Channel3->CNDTR = cndtr; //传输数据量
//通道开启,从存储器读,循环,外设不自增,存储器自增
//外设32位数据,存储器32位数据,优先级最高,非存储器到存储器
DMA2_Channel3->CCR = DMA_CCR3_EN | DMA_CCR3_DIR | DMA_CCR3_CIRC | \
DMA_CCR3_MINC | DMA_CCR3_PSIZE_1 | DMA_CCR3_MSIZE_1 | \
DMA_CCR3_PL_0 | DMA_CCR3_PL_1;
}
//TIM6初始化
void tim6_init(unsigned short arr, unsigned short psc)
{
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN; //开启TIM6时钟
TIM6->PSC = psc; //预分频器
TIM6->ARR = arr; //自动重装载
TIM6->CR2 &= 0xff8f;
TIM6->CR2 |= TIM_CR2_MMS_1; //更新事件用作TRGO
TIM6->CR1 &= 0xff70;
TIM6->CR1 |= TIM_CR1_CEN | TIM_CR1_ARPE; //使能计数器,非单脉冲模式,自动重装载
}
初始化RCC时钟为72MHz,正弦波1000点,2个波形,一个反相,一个幅度改变
for(i = 0; i < 1000; i++)
{
sw[i] = 4095 - sine_wave[i];
sw[i] <<= 16;
sw[i] += sine_wave[i] / 3;
}
dma2_init((unsigned int)&DAC->DHR12RD, (unsigned int)sw, 1000);
dac_init();
tim6_init(7200, 0);
ARR为7199,PSC为0,72M/7199/1000=10Hz波形频率,修改预分频和重装载数值可以改变波形频率
计算公式:f=fosc/n/(arr+1)/(psc+1)
f-波形频率
n-波形点数
arr-定时器计数,不能为0
psc-时钟分频系数
fosc-系统时钟,一般PLL后为72MHz
上海现易电子元器件有限公司 版权所有 未经授权禁止复制或镜像
CopyRight 2020-2025 www.xy777.net All rights reserved 沪ICP备2020031792号