这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » stm32 DAC

共1条 1/1 1 跳转至

stm32 DAC

工程师
2014-09-28 19:35:35     打赏
/*
DMA+TIM+DAC方式实现
*/

//DAC通道1的12位右对齐数据保持寄存器
#define DAC_DHR12R1_Address      0x40007408 
//生成一个360点的正弦表
u16 Sine_Const[360];

for(i = 0;i < 360;i++) 
{
  Sine_Const[i] = 2047+((sin(i*3.1415926/180))*2047); 
}

//相关时钟配置
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);

//定时器相关配置
void TIM_Configuration(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;   
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  
  TIM_TimeBaseStructure.TIM_Period = 0x5;           
  TIM_TimeBaseStructure.TIM_Prescaler = 0x00;        
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); 
  TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); 
  TIM_Cmd(TIM6, ENABLE); 
}

//DAC相关配置
void DAC_Configuration(void)
{
  DAC_InitTypeDef DAC_InitStructure;     
  DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; 
  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
  DAC_Init(DAC_Channel_1, &DAC_InitStructure); 
  DAC_Cmd(DAC_Channel_1, ENABLE
}

//DMA相关配置
void DMA_Configuration(void)
{
  DMA_DeInit(DMA2_Channel3); 
  DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R1_Address;  
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&Sine_Const;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  //外设作为数据传输的目的地 
  DMA_InitStructure.DMA_BufferSize = 360;   //dma缓存大小    
  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;  //工作在循环缓存模式,数据传输数目为0时,自动恢复配置初值 
  DMA_InitStructure.DMA_Priority = DMA_Priority_High; 
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  //通道未被设置成内存到内存模式,与循环模式相对 
  DMA_Init(DMA2_Channel3, &DMA_InitStructure); //将DMA_InitStructure中指定的参数初始化dma的通道寄存器 
  DMA_Cmd(DMA2_Channel3, ENABLE);  //使能通道 
  DAC_DMACmd(DAC_Channel_1, ENABLE); 

}

 


共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]