重学STM32---(六)DAC+DMA+TIM
这两天复习了DAC,DMA再加上把基本定时器TIM6和TIM7看了一下,打算写一个综合点的程序,,,就在网上找了一些关于DAC,DMA和定时器相关的程序,最终打算写了输出正弦波的程序。。。
由于没有示波器,也就不能显示出效果了,本来是打算用软件调试看效果的,但TIM6总是出问题,CNT不自增,可是我真的找不出TIM6有什么错误,,,后来在网上发现许多道友遇到同样的问题,有人说TIM6不支持软件调试,不知道是不是这么回事,,没办法,没示波器,软件调试又不行,自己本身也不想耽搁复习的进程,也就不纠结于此了
整体上应该是对的
程序:
#include "dac.h"
#include "stm32f10x.h"
void DAC1_Init()
{
RCC->APB2ENR |= 1 << 2; //
GPIOA->CRL &= 0xfff0ffff; //PA4模拟输入
DAC->CR &= 0x00000000; //清零
DAC->CR |= 1 << 12; //DMA使能
DAC->CR |= 0 << 6; //TIM6 TRGO触发
DAC->CR |= 1 << 2; //开启触发使能
DAC->CR |= 1 << 1; //关闭输出缓存
DAC->CR |= 1 << 0; //开启DAC1
}
#include "dma.h"
#include "stm32f10x.h"
//pscadr 外设地址
//memaddr存储器地址
//numbyte传输字节数
void DMA_Init(u32 pscadr,u16 memaddr,u16 numbyte)
{
RCC->AHBENR |= 1 << 0; //开启DMA1的时钟
DMA1_Channel1->CCR &= 0xFFFF0000;
DMA1_Channel1->CCR |= 3 << 12; //优先级最高
DMA1_Channel1->CCR |= 1 << 10; //存储器16位数据
DMA1_Channel1->CCR |= 1 << 8; //外设16位数据
DMA1_Channel1->CCR |= 1 << 7; //存储器自增
DMA1_Channel1->CCR |= 0 << 6; //外设不自增
DMA1_Channel1->CCR |= 1 << 5; //循环
DMA1_Channel1->CCR |= 1 << 4; //从存储器读
DMA1_Channel1->CNDTR |= numbyte ;
DMA1_Channel1->CMAR |= memaddr ;
DMA1_Channel1->CPAR |= pscadr ;
DMA1_Channel1->CCR |= 1 << 0; //通道开启
}
void TIM6_Init(u16 arr,u16 psc)
{
RCC->APB1ENR |= 1 << 4;
TIM6->CR1 &= 0xFF00;
TIM1->CR1 |= 1 << 7; //自动重装载
TIM6->CR1 |= 0 << 3; //非单脉冲模式
TIM6->CR2 |= 2 << 3; //更新事件用作TRGO
TIM6->PSC |= psc;
TIM6->ARR |= arr;
TIM6->CR1 |= 1 << 0;
}
#include "timer.h"
//#include "exti.h"
#include "dma.h"
#include "dac.h"
/* 波形数据 ---------------------------------------------------------*/
const unsigned short Sine12bit[32] = {
2448,2832,3186,3496,3751,3940,4057,4095,4057,3940,
3751,3496,3186,2832,2448,2048,1648,1264,910,600,345,
156,39,0,39,156,345,600,910,1264,1648,2048};
#define DAC_DHR12R1_ADDR DAC_BASE + 8 //DAC_DHR12R1寄存器地址
int main()
{
LED_Init ();
delay_init (72);
TIM6_Init(19,0);
DAC1_Init();
DMA_Init(DAC_DHR12R1_ADDR,(unsigned short)Sine12bit,32);
while(1);
}