This article records how to configure ADC peripheral of STM32L431RCT6 with STM32CubeMX and read voltage value of DAC output pin in detail.

1. Preparation

Hardware preparation

  • Development board

    Firstly, we need to prepare a development board, here I prepare the development board of STM32L4 (BearPi) :

Software to prepare

  • Keil-mdk and the corresponding chip package need to be installed in order to compile and download the generated code;
  • Prepare a serial debugging assistant, the one I used hereSerial Port Utility;

Keil MDK and Serial Port Utility installation packages are available at the end of this article.

2. Generate MDK projects

Select the chip model

Open STM32CubeMX, open MCU selector:

Search for and select the chipSTM32L431RCT6:

Configuring the Clock Source

  • If you choose to use an external high speed clock (HSE), you need to configure RCC in the System Core;
  • If you use the default internal clock (HSI), you can skip this step.

Here I use the external clock:

Configure a serial port

Bear Pai developed onboard ST-link and virtual a serial port, the schematic diagram is as follows:

Here, I switch the switch to at-MCU mode to connect the PC serial port with USART1.

Next, configure USART1:

Configuration DAC

Determine the DAC output channel

View the schematic of the Bear PI E53 interface:

Configuration DAC

Select DAC1, open output channel 2, and keep the default configuration:

Configure the ADC

Knowledge cards – ADC

ADC (Analog to Digital Converter) converts continuously changing Analog signals into discrete Digital signals, which are then processed by Digital circuits. It is called Digital signal processing.

The STM32L431xx series has one ADC with ADC resolution up to 12 bits. Each ADC has up to 20 acquisition channels. The A/D conversion of these channels can be performed in single, continuous, scan or intermittent mode. ADC results can be left – or right-aligned and stored in 16-bit data registers.

The maximum conversion rate of THE STM32L431 ADC is 5.33Mhz, that is, the conversion time is 0.188us (at 12-bit resolution). The conversion time of the ADC is independent of the AHB bus clock frequency.

That’s the end of the knowledge card. Do you know anything about ADC?

Identify ADC channels

View the schematic of the Bear PI E53 interface:

Configure the ADC (Single conversion mode)

First chooseADC1, open channel 3:

Here we leave the default Settings for ADC:

Finally, set ADC conversion rules:

Leave the rest of the Settings as default.

Configuring the Clock Tree

The maximum main frequency of STM32L4 is 80M, so PLL is configured and finally enabledHCLK = 80MhzYou can:

Build project Settings

Code generation Settings

Finally set to generate a separate initialization file:

The generated code

Click on theGENERATE CODEMdk-v5 project can be generated:

3. Write, compile and download user code in MDK

Redirect the printf() function

Implement printf() based on the serial port sending function.

Write test code to read data

Modify the main function as follows:

int main(void)
{
    /* USER CODE BEGIN 1 */
    uint16_t i = 0;
    uint16_t adc_value = 0;
    float vol = 0.0;
    /* USER CODE END 1 */

    HAL_Init();
    SystemClock_Config();

    MX_GPIO_Init();
    MX_DAC1_Init();
    MX_USART1_UART_Init();
    MX_ADC1_Init();

    /* USER CODE BEGIN 2 */
    printf("DAC Test... \r\n");
    HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    for(i = 0; i < 4096; i++)
    {
        HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, i);
        HAL_Delay(2);
        if(i%1024= =0)
        {
            /* Use ADC sampling */
            HAL_ADC_Start(&hadc1);	                // Start ADC single conversion
            HAL_ADC_PollForConversion(&hadc1, 50);	// Wait for the ADC conversion to complete
            adc_value = HAL_ADC_GetValue(&hadc1); 	// Read ADC conversion data
            vol = ((double)adc_value/4096) *3.3;
            printf("adc_value = %d, vol = %.2fV.\n", adc_value, vol); }}printf("DAC test finish, test again! \r\n");
    }
    /* USER CODE END 3 */
    }
Copy the code

So far, we have learned how to read the voltage value of the DAC output pin using an ADC.

For more exciting articles and resources, please pay attention to my wechat official number: “McUlover666”.