STM32 —— 多路DAC(输出电压和正弦波)
Posted imxiangzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32 —— 多路DAC(输出电压和正弦波)相关的知识,希望对你有一定的参考价值。
[cpp] view plain copy print ?- //========================================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)
[cpp] view plain copy print ?
- #ifndef _DAC_H_
- #define _DAC_H_
- void DAC1Init(void);
- void DAC1OutVoltage(float data);
- void DAC2Init(void);
- #endif /* _DAC_H_ */
[cpp] view plain copy print ?
- #include "dac.h"
- //#include "target.h"
- #include "type.h"
- #define DAC_DHR12RD_ADDRESS 0x40007420
- static 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;
- // PA4
- static 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 out
- void 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];
- STM32F103的DAC——实现音频输出
STM32CubeMX(07)定时器+ DAC+DMA输出阶梯波实验。