• The last:STM32Cube_09 redirects printf function to serial port output

For a better reading experience, please go:Mculover666’s personal blog.

This article describes how to configure ADC peripherals of STM32L431RCT6 using STM32CubeMX, read the data of MQ-2 gas sensor and send it through serial port.

1. Preparation

Hardware preparation

  • Development board

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

  • MQ – 2 modules

    Mq-2 gas sensors are generally used in home and factory gas leak monitoring devices, suitable for the detection of liquefied gas, butane, propane, methane, alcohol, hydrogen, smoke, etc., as shown in figure:

The schematic diagram of the MQ-2 is as follows:

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:

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)
{
	uint16_t smoke_value = 0;

    HAL_Init();

    SystemClock_Config();

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

    while (1)
    {
        HAL_ADC_Start(&hadc1);	                // Start ADC single conversion
        HAL_ADC_PollForConversion(&hadc1, 50);	// Wait for the ADC conversion to complete
        smoke_value = HAL_ADC_GetValue(&hadc1); // Read ADC conversion data
        printf("smoke_value = %d\n", smoke_value);
        HAL_Delay(500); }}Copy the code

Now that we have learned how to read the VALUE of the MQ-2 sensor using an ADC, the next section describes how to flash the LED using a universal timer.

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