Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The so-called DAC function is to convert digital signals to analog signals. In general, ADC function is widely used, that is, to convert analog signals to digital signals, which is mostly used to read the value of the sensor. But sometimes you need to control some sensors, you need to use analog signal control, which needs to convert digital signals into analog signals.

A digital/analog converter module (DAC) is a 12-bit digital input, voltage output digital/analog converter. Or 12-bit mode, can also be used in conjunction with DMA controllers. When the DAC works in 12-bit mode, the data can be set to left-aligned or right-aligned. The DAC module has two output channels, each with a separate converter. In dual-DAC mode, the two channels can be converted independently or simultaneously and the output of the two channels can be updated synchronously. The DAC can be pin fed into the reference voltage V REF+ to obtain more accurate conversion results.

Main features of DAC

  • Two DAC converters: each converter corresponds to one output channel
  • 8 – or 12-bit monotonic output
  • Left-aligned or right-aligned data in 12-bit mode
  • Synchronous update function
  • Noise waveform generation
  • Triangular waveform generation
  • Dual DAC channels convert simultaneously or separately
  • Each channel has DMA capability
  • Externally triggered conversion
  • Input reference voltage V REF+

The following code to achieve analog signal output.

#include "dac.h"

void DAC1_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    DAC_InitTypeDef DAC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC , ENABLE);

    //PA4 is set to analog input. After the DAC channel is enabled, PA4 will be connected to the DAC analog output
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;								// Set to analog input
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    DAC_InitStructure.DAC_Trigger = DAC_Trigger_None; 							TEN1=0
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; 			// Do not use waveform generation
    DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0; 	// Set the mask and amplitude
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;				//DAC1 output cache is closed
    DAC_Init(DAC_Channel_1, &DAC_InitStructure); 								// Initialize DAC channel 1

    DAC_Cmd(DAC_Channel_1, ENABLE); 											/ / can make DAC1
    DAC_SetChannel1Data(DAC_Align_12b_R, 0); 									// The 12-bit right-aligned data format sets the DAC value
	
}
// Set the output voltage of channel 1
//vol:0--3300, representing 0-- 3.3V
void DAC1_Set_Vol(u16 vol)
{
    float temp = vol;
    temp /= 1000;
    temp = temp * 4096 / 3.3;

    DAC_SetChannel1Data(DAC_Align_12b_R, temp);
}
Copy the code

In the initialization, we should pay attention to a problem, STM32F103 MCU DAC output channel has two.

PA4 and PA5, but PA4 and PA5 are initialized as analog input functions when initializing pins. This is also explained in the official documentation.

PA4 and PA5 are automatically connected internally to the analog output pins. So when you initialize, even though you’re using output, you still have to set the pins to input.

After the pins are initialized, the DAC function is initialized, and neither trigger nor waveform generation is used. Directly controlled by software

Next, a function converts the output voltage value to the register setting value. The DAC is converted to 12 bits, so the maximum value is 2^12, which is 4096, and the supply voltage of STM32 MCU is 3.3V, so the voltage to be set divided by 3.3, and then multiplied by 4096 is the value of the DAC register to be set.

If you want to output the analog voltage, just call the DAC1_Set_Vol() function in the main function and pass in the voltage to be set.

int main(void)
{
    u16 adcx;
    float temp;
    u8 t = 0;
    u16 dacval = 0;
    u8 key;

    delay_init();
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    uart_init(115200);
    LED_Init();
    DAC1_Init();    
    while(1) { LED0 = ! LED0; DAC1_Set_Vol(1000);
        delay_ms(100); }}Copy the code

In the main function, set the voltage value of the DAC output to 1000, i.e., 1V. At this time through the multimeter can be in the STM32 MCU PA4 pin amount to 1V voltage value.