卓尔高考网

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

篇首语:本文由小编为大家整理,主要介绍了STM32 —— 多路DAC(输出电压和正弦波)相关的知识,希望对你有一定的参考价值。

[cpp]  view plain  copy  print ?
  1. //========================================DAC=========================================  
  2. #define DA_OUT1_CHANNEL    DAC_Channel_1  
  3. #define DA_OUT1_GRP        GPIOA  
  4. #define DA_OUT1_INDEX      GPIO_Pin_4  
  5. #define DA_OUT1_HIGH()     GPIO_SetBits(DA_OUT1_GRP, DA_OUT1_INDEX)  
  6. #define DA_OUT1_CONFIG()   GPIOConfig(DA_OUT1_GRP, DA_OUT1_INDEX, GPIO_Mode_AIN)  
  7.   
  8. #define DA_OUT2_CHANNEL    DAC_Channel_2  
  9. #define DA_OUT2_GRP        GPIOA  
  10. #define DA_OUT2_INDEX      GPIO_Pin_5  
  11. #define DA_OUT2_HIGH()     GPIO_SetBits(DA_OUT2_GRP, DA_OUT2_INDEX)  
  12. #define DA_OUT2_CONFIG()   GPIOConfig(DA_OUT2_GRP, DA_OUT2_INDEX, GPIO_Mode_AIN)  

[cpp]  view plain  copy  print ?
  1. #ifndef _DAC_H_  
  2. #define _DAC_H_  
  3.   
  4. void DAC1Init(void);  
  5. void DAC1OutVoltage(float data);  
  6. void DAC2Init(void);  
  7.   
  8. #endif /* _DAC_H_ */  

[cpp]  view plain  copy  print ?
  1. #include "dac.h"  
  2. //#include "target.h"  
  3. #include "type.h"  
  4.   
  5. #define DAC_DHR12RD_ADDRESS      0x40007420  
  6.   
  7. static const uint16_t sin[32] =  
  8.   
  9.   2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056,  
  10.   3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909,  
  11.   599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647  
  12.   
  13. ;  
  14. static uint32_t dual_sin[32];  
  15. static uint8_t index = 0;  
  16. // PA4  
  17. static void dac1_clk_init(void)  
  18.   
  19.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);  
  20.   
  21.   
  22. static void dac1_gpio_init(void)  
  23.   
  24.   DA_OUT1_CONFIG();  
  25.   DA_OUT1_HIGH();  
  26.   
  27.   
  28. static void dac1_mode_init(void)  
  29.   
  30.   DAC_InitTypeDef DAC_InitStructure;  
  31.   
  32.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;  
  33.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;  
  34.   DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits11_0;  
  35.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;  
  36.   DAC_Init(DA_OUT1_CHANNEL, &DAC_InitStructure);  
  37.   
  38.   DAC_Cmd(DA_OUT1_CHANNEL, ENABLE);  
  39.   
  40.   
  41. void DAC1Init(void)  
  42.   
  43.   dac1_clk_init();  
  44.   dac1_gpio_init();  
  45.   dac1_mode_init();  
  46.   
  47.   
  48. // DAC1OutVoltage(1.5);// 1.5V out  
  49. void DAC1OutVoltage(float data)  
  50.   
  51.   uint16_t value;  
  52.   
  53.   value = (uint16_t)((data / 3.3) * 4096);  
  54.   
  55.   DAC_SetChannel1Data(DAC_Align_12b_R, value);  
  56.   DAC_SoftwareTriggerCmd(DA_OUT1_CHANNEL, ENABLE);  
  57.   
  58.   
  59. static void dac2_clk_init(void)  
  60.   
  61.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);  
  62.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);  
  63.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);  
  64.   
  65.   
  66. static void dac2_gpio_init(void)  
  67.   
  68.   DA_OUT2_CONFIG();  
  69.   DA_OUT2_HIGH();  
  70.   
  71.   
  72. static void dac2_timer_init(void)  
  73.   
  74.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  
  75.   
  76.   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);  
  77.   TIM_TimeBaseStructure.TIM_Period = 0x19;  
  78.   TIM_TimeBaseStructure.TIM_Prescaler = 0x0;  
  79.   TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;  
  80.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  81.   TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);  
  82.   
  83.   /* TIM8 TRGO selection */  
  84.   TIM_SelectOutputTrigger(TIM8, TIM_TRGOSource_Update);  
  85.   
  86.   
  87. static void dac2_mode_init(void)  
  88.   
  89.   DAC_InitTypeDef DAC_InitStructure;  
  90.   
  91.   /* DAC channel2 Configuration */  
  92.   DAC_InitStructure.DAC_Trigger = DAC_Trigger_T8_TRGO;  
  93.   DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;  
  94.   DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;  
  95.   DAC_Init(DA_OUT2_CHANNEL, &DAC_InitStructure);  
  96.   
  97.   for(index = 0; index < 32; ++index)  
  98.     
  99.     dual_sin[index] = (sin[index] << 16) + sin[index];  
  100.     
  101.   
  102.   

您可能还会对下面的文章感兴趣: