• The last:【STM32Cube_15】 Use hardware I2C to read temperature and humidity sensor data (SHT30)

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

This paper records in detail how to use STM32CubeMX to configure the HARDWARE CRC peripheral check data of STM32L431RCT6, and uses THE SHT30 temperature and humidity sensor as an example to check whether the check can be correct.

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:

Configure CRC peripherals

First activate CRC:

Then configure the initial value for the CRC check:

Here we take SHT30 as an example, which has been given in its data manual, as shown in the figure:

Accordingly, the configuration of CRC peripherals is as follows:

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

STM32Cube_09 redirects printf function to serial port output.

Testing CRC

Add the following code to the main.c file:

/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
Copy the code

Then modify the main function:

int main(void)
{
    /* USER CODE BEGIN 1 */
	uint8_t dat[2] = {0xBE.0xEF};
	uint8_t crc = 0;

    /* USER CODE END 1 */
    HAL_Init();

    SystemClock_Config();

    MX_GPIO_Init();
    MX_CRC_Init();
    MX_USART1_UART_Init();
  
    /* USER CODE BEGIN 2 */
	printf("Test CRC check:\n");
	crc = HAL_CRC_Accumulate(&hcrc, (uint32_t*)dat, 2);
	printf("crc = %#x\n", crc);
    /* USER CODE END 2 */

    while (1) {}}Copy the code

The test results

The test results are as follows:

Now that we have learned how to use hardware CRC to validate SHT30 data, the next section will describe how to use hardware SPI to drive the LCD screen (ST7789).

  • The last:【STM32Cube_15】 Use hardware I2C to read temperature and humidity sensor data (SHT30)
  • Next up:【STM32Cube_17】 Using hardware SPI driver TFT-LCD (ST7789).

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