Docs

Micro-controllers, wireless transmission and database

Head

STM32 to Nextion Display with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

STM32CubeIDE Settings

ADC1 - IN9 (tick)

Parameter Settings --> ADC Settings --> Continuous Conversion Mode (Enabled)

Enable USART1 asynchronous

Parameter Settings --> Basic Parameters --> Baud rate 9600

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
/* USER CODE BEGIN 0 */
uint16_t readValue;
uint16_t barValue;
uint16_t gaugeValue;
uint8_t endCommand[1];
void cTransmit(char *id, char *data)
{
  char buffer[40];
  endCommand[0] = 255; //Hex FF
  int length = sprintf(buffer, "%s=\"%s\"", id, data);
  HAL_UART_Transmit(&huart1, (uint8_t *)buffer, length, 1000);
  HAL_UART_Transmit(&huart1, endCommand, 1, 100);
  HAL_UART_Transmit(&huart1, endCommand, 1, 100);
  HAL_UART_Transmit(&huart1, endCommand, 1, 100);
}
void nTransmit(char *id, int data)
{
  char buffer[40];
  endCommand[0] = 255; //0xFF
  int length = sprintf(buffer, "%s=%d", id, data);
  HAL_UART_Transmit(&huart1, (uint8_t *)buffer, length, 1000);
  HAL_UART_Transmit(&huart1, endCommand, 1, 100);
  HAL_UART_Transmit(&huart1, endCommand, 1, 100);
  HAL_UART_Transmit(&huart1, endCommand, 1, 100);
}
/* USER CODE END 0 */
/* USER CODE BEGIN 2 */
HAL_ADC_Start(&hadc1);
/* USER CODE END 2 */
/* USER CODE BEGIN WHILE */
while (1)
{
  HAL_ADC_PollForConversion(&hadc1,1000);
  readValue = HAL_ADC_GetValue(&hadc1); //readValue is from 0-4095
  barValue = readValue/41; // barValue is from 0-100
  gaugeValue = readValue/23; // gaugeValue is from 0 to 180
  nTransmit("n0.val", barValue);
  nTransmit("j0.val", barValue);
  nTransmit("z0.val", gaugeValue);
  if(barValue < 33) { // barValue is from 0-100
    cTransmit("t0.txt","Low");
    nTransmit("t0.pco", 31); // 31 is for blue color
    nTransmit("z0.pco", 31);
  }
  else if (barValue < 66 ) {
    cTransmit("t0.txt","Medium");
    nTransmit("t0.pco", 1024); // 1024 is for green color
    nTransmit("z0.pco", 1024);
  }
  else {
    cTransmit("t0.txt","High");
    nTransmit("t0.pco", 63488); // 63488 is for red color
    nTransmit("z0.pco", 63488);
  }
  HAL_Delay(50);
  /* USER CODE END WHILE */
  /* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */