STM32 —— 多路DAC(输出电压和正弦波)

来源:互联网 发布:mac怎么用u盘重装系统 编辑:程序博客网 时间:2024/05/01 13:29
//========================================DAC=========================================#define DA_OUT1_CHANNEL    DAC_Channel_1#define DA_OUT1_GRP        GPIOA#define DA_OUT1_INDEX      GPIO_Pin_4#define DA_OUT1_HIGH()     GPIO_SetBits(DA_OUT1_GRP, DA_OUT1_INDEX)#define DA_OUT1_CONFIG()   GPIOConfig(DA_OUT1_GRP, DA_OUT1_INDEX, GPIO_Mode_AIN)#define DA_OUT2_CHANNEL    DAC_Channel_2#define DA_OUT2_GRP        GPIOA#define DA_OUT2_INDEX      GPIO_Pin_5#define DA_OUT2_HIGH()     GPIO_SetBits(DA_OUT2_GRP, DA_OUT2_INDEX)#define DA_OUT2_CONFIG()   GPIOConfig(DA_OUT2_GRP, DA_OUT2_INDEX, GPIO_Mode_AIN)

#ifndef _DAC_H_#define _DAC_H_void DAC1Init(void);void DAC1OutVoltage(float data);void DAC2Init(void);#endif /* _DAC_H_ */

#include "dac.h"//#include "target.h"#include "type.h"#define DAC_DHR12RD_ADDRESS      0x40007420static const uint16_t sin[32] ={  2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,  3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,  599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647};static uint32_t dual_sin[32];static uint8_t index = 0;// PA4static void dac1_clk_init(void){  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);}static void dac1_gpio_init(void){  DA_OUT1_CONFIG();  DA_OUT1_HIGH();}static void dac1_mode_init(void){  DAC_InitTypeDef DAC_InitStructure;  DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;  DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;  DAC_Init(DA_OUT1_CHANNEL, &DAC_InitStructure);  DAC_Cmd(DA_OUT1_CHANNEL, ENABLE);}void DAC1Init(void){  dac1_clk_init();  dac1_gpio_init();  dac1_mode_init();}// DAC1OutVoltage(1.5);// 1.5V outvoid DAC1OutVoltage(float data){  uint16_t value;  value = (uint16_t)((data / 3.3) * 4096);  DAC_SetChannel1Data(DAC_Align_12b_R, value);  DAC_SoftwareTriggerCmd(DA_OUT1_CHANNEL, ENABLE);}static void dac2_clk_init(void){  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);}static void dac2_gpio_init(void){  DA_OUT2_CONFIG();  DA_OUT2_HIGH();}static void dac2_timer_init(void){  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  TIM_TimeBaseStructure.TIM_Period = 0x19;  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);  /* TIM8 TRGO selection */  TIM_SelectOutputTrigger(TIM8, TIM_TRGOSource_Update);}static void dac2_mode_init(void){  DAC_InitTypeDef DAC_InitStructure;  /* DAC channel2 Configuration */  DAC_InitStructure.DAC_Trigger = DAC_Trigger_T8_TRGO;  DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;  DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;  DAC_Init(DA_OUT2_CHANNEL, &DAC_InitStructure);  for(index = 0; index < 32; ++index)  {    dual_sin[index] = (sin[index] << 16) + sin[index];  }}static void dac2_dma_init(void){  DMA_InitTypeDef DMA_InitStructure;  /* DMA2 channel4 configuration */  DMA_DeInit(DMA2_Channel4);  DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12RD_ADDRESS;  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&dual_sin;  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_Word;  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;  DMA_InitStructure.DMA_Priority = DMA_Priority_High;  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  DMA_Init(DMA2_Channel4, &DMA_InitStructure);}static void dac2_enable(void){  DMA_Cmd(DMA2_Channel4, ENABLE);  DAC_Cmd(DA_OUT2_CHANNEL, ENABLE);  DAC_DMACmd(DA_OUT2_CHANNEL, ENABLE);  TIM_Cmd(TIM8, ENABLE);}void DAC2Init(void){  dac2_clk_init();  dac2_gpio_init();  dac2_timer_init();  dac2_mode_init();  dac2_dma_init();  dac2_enable();}

0 0