1. 程式人生 > >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. #define DA_OUT2_CHANNEL    DAC_Channel_2
  8. #define DA_OUT2_GRP        GPIOA
  9. #define DA_OUT2_INDEX      GPIO_Pin_5
  10. #define DA_OUT2_HIGH()     GPIO_SetBits(DA_OUT2_GRP, DA_OUT2_INDEX)
  11. #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. void DAC1Init(void);  
  4. void DAC1OutVoltage(float data);  
  5. void DAC2Init(void);  
  6. #endif /* _DAC_H_ */

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


from:http://blog.csdn.net/wqx521/article/details/69468673