Author Topic: STM32 SSD1306 number display problem  (Read 728 times)

0 Members and 1 Guest are viewing this topic.

Offline RemarkTopic starter

  • Contributor
  • Posts: 29
  • Country: lt
STM32 SSD1306 number display problem
« on: April 22, 2021, 09:46:18 am »
Hello to all. I am currently doing an environment parameter monitoring module, based on STM32F401RE microcontroller. The module measures the environment temperature, relative humidity and dust concentration in the air. The information is displayed on the oled screen. I can successfully display temperature and humidity, but when I try to display the calculated concentration of dust in the air, strange problems begin.

When the dust concentration is displayed on the OLED from 0 to 99ug/m3 - everything is fine. But when the dust concentration reaches the maximum measurable limit ~500ug/m3, on the OLED screen, I get completely different, lower values...

For example, if I print calculated dust values on the terminal and at the same time on the OLED screen, information on the terminal is correct, I can see for example 100-300 ug/m3, but on the OLED screen I see a completely different value, 15...12 ug/m3.

I add my code, main.c:

Code: [Select]
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <string.h>
#include <stdio.h>

#include "fonts.h"
#include "ssd1306.h"
#include "dwt_stm32_delay.h"


#define ADC_VREF      (float)3.3
#define ADC_AD_RESOLUTION       4096


/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_I2C1_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t Rh_byte1, Rh_byte2, Temp_byte1, Temp_byte2;
uint16_t sum, RH, TEMP;
int temp_low, temp_high, rh_low, rh_high;
char temp_char1[2], temp_char2, rh_char1[2], rh_char2;
uint8_t check = 0;
GPIO_InitTypeDef GPIO_InitStruct;



void set_gpio_output (void)
{
/*Configure GPIO pin output: PA2 */
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}



void set_gpio_input (void)
{
/*Configure GPIO pin input: PA2 */
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}



void DHT11_start (void)
{
DWT_Delay_Init();
set_gpio_output ();  // set the pin as output
HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, 0);   // pull the pin low
DWT_Delay_us (18000);   // wait for 18ms
HAL_GPIO_WritePin (GPIOA, GPIO_PIN_4, 1);   // pull the pin high
DWT_Delay_us (20);   // wait for 20us
set_gpio_input ();   // set as input
}



void check_response (void)
{
DWT_Delay_us (40);
if (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)))
{
DWT_Delay_us (80);
if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4))) check = 1;
else check = -1;
}
while ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)));   // wait for the pin to go low
//if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)));   // wait for the pin to go low
//return check;
}



uint8_t read_data (void)
{
uint8_t i,j;
for (j=0;j<8;j++)
{
while (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)));   // wait for the pin to go high
//if (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)));   // wait for the pin to go high
DWT_Delay_us (40);   // wait for 40 us
if (!(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)))   // if the pin is low
{
i&= ~(1<<(7-j));   // write 0
}
else i|= (1<<(7-j));  // if the pin is high, write 1
while ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)));  // wait for the pin to go low
//if ((HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_4)));  // wait for the pin to go low
}
return i;
}



// function for printing floats
void uprintf(char *str){

HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 100);
}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

uint32_t save_data;
char message[10];
char vltg[10];
char dust[10];
uint16_t voltage;
float fAdVoltage = 0;
float dustDensity = 0;
uint8_t dust_converted;


  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
  DWT_Delay_Init();
  SSD1306_Init();  // initialise the OLED display
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */


  // ADC and infrared LED preparation
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // turn on green on-board led
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); // turn on infrared led on the dust sensor
  DWT_Delay_us(280);

  HAL_ADC_Start(&hadc1);
  HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

  save_data = HAL_ADC_GetValue(&hadc1);

  DWT_Delay_us(40);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // turn off infrared LED
  DWT_Delay_us(9680);

  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Turn off green led

  // calculate voltage
  fAdVoltage = save_data * (ADC_VREF / ADC_AD_RESOLUTION) * 2; // multiply by 2 to get real voltage (because I use voltage divider)


  // calculate dust ug/m3
  dustDensity = (0.17 * fAdVoltage - 0.1) * 1000;  //multiplied by 1000 units and converted to ug/m3


  // change the data to be suitable for printing
  sprintf(message, "%hu\r\n", save_data);
  sprintf(vltg, "%.3f\r\n", fAdVoltage);
  sprintf(dust, "%.0f\r\n", dustDensity);


  // print data on terminal
  HAL_UART_Transmit(&huart2, (uint8_t*)vltg, strlen(vltg), HAL_MAX_DELAY);
  HAL_UART_Transmit(&huart2, (uint8_t*)dust, strlen(dust), HAL_MAX_DELAY);

// I tried to change to the right type suitable for OLED
  dust_converted = (uint8_t)dustDensity;


DHT11_start ();
check_response ();
Rh_byte1 = read_data ();
Rh_byte2 = read_data ();
Temp_byte1 = read_data ();
Temp_byte2 = read_data ();
sum = read_data();
///////////////////////////////////////////////


if (sum == (Rh_byte1+Rh_byte2+Temp_byte1+Temp_byte2))    // if the data is correct
{

SSD1306_GotoXY (0,0);
SSD1306_Puts("TEMP:- ",&Font_11x18, 1);
SSD1306_Putc((Temp_byte1/10)+48,&Font_11x18, 1);
SSD1306_Putc((Temp_byte1%10)+48,&Font_11x18, 1);
SSD1306_Puts("C",&Font_11x18, 1);
//display
SSD1306_GotoXY (0,24);
SSD1306_Puts("RH:- ",&Font_11x18, 1);
SSD1306_Putc((Rh_byte1/10)+48,&Font_11x18, 1);
SSD1306_Putc((Rh_byte1%10)+48,&Font_11x18, 1);
SSD1306_Puts("%",&Font_11x18, 1);

SSD1306_GotoXY (0,42);
SSD1306_Puts("Dust:- ",&Font_11x18, 1);
SSD1306_Putc((dust_converted/10)%10+48,&Font_11x18, 1);
SSD1306_Putc((dust_converted%10)+48,&Font_11x18, 1);

SSD1306_UpdateScreen();
HAL_Delay (1000);
//NVIC_SystemReset(); // worst case scenario
/*
SSD1306_GotoXY (0,0);
SSD1306_Puts("Test:- ",&Font_11x18, 1);

SSD1306_UpdateScreen();
HAL_Delay (1000);
*/

//green led on-board
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay (1000);
//DWT_Delay_us (40);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
//HAL_UART_Transmit(&huart2, "1", 10, 100);
HAL_Delay (1000);


}
}

  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_ADC1_Init(void)
{

  /* USER CODE BEGIN ADC1_Init 0 */

  /* USER CODE END ADC1_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC1_Init 1 */

  /* USER CODE END ADC1_Init 1 */
  /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
  sConfig.Channel = ADC_CHANNEL_11;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

/**
  * @brief I2C1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_I2C1_Init(void)
{

  /* USER CODE BEGIN I2C1_Init 0 */

  /* USER CODE END I2C1_Init 0 */

  /* USER CODE BEGIN I2C1_Init 1 */

  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 400000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */

  /* USER CODE END I2C1_Init 2 */

}

/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, LD2_Pin|GPIO_PIN_8, GPIO_PIN_RESET);

  /*Configure GPIO pin : B1_Pin */
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : PA4 */
  GPIO_InitStruct.Pin = GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : LD2_Pin PA8 */
  GPIO_InitStruct.Pin = LD2_Pin|GPIO_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



SSD1306_Puts function:

Code: [Select]
char SSD1306_Puts(char* str, FontDef_t* Font, SSD1306_COLOR_t color) {
/* Write characters */
while (*str) {
/* Write character by character */
if (SSD1306_Putc(*str, Font, color) != *str) {
/* Return error */
return *str;
}

/* Increase string pointer */
str++;
}

/* Everything OK, zero should be returned */
return *str;
}



SSD1306_Putc function:

Code: [Select]
char SSD1306_Putc(char ch, FontDef_t* Font, SSD1306_COLOR_t color) {
uint32_t i, b, j;

/* Check available space in LCD */
if (
SSD1306_WIDTH <= (SSD1306.CurrentX + Font->FontWidth) ||
SSD1306_HEIGHT <= (SSD1306.CurrentY + Font->FontHeight)
) {
/* Error */
return 0;
}

/* Go through font */
for (i = 0; i < Font->FontHeight; i++) {
b = Font->data[(ch - 32) * Font->FontHeight + i];
for (j = 0; j < Font->FontWidth; j++) {
if ((b << j) & 0x8000) {
SSD1306_DrawPixel(SSD1306.CurrentX + j, (SSD1306.CurrentY + i), (SSD1306_COLOR_t) color);
} else {
SSD1306_DrawPixel(SSD1306.CurrentX + j, (SSD1306.CurrentY + i), (SSD1306_COLOR_t)!color);
}
}
}

/* Increase pointer */
SSD1306.CurrentX += Font->FontWidth;

/* Return character written */
return ch;
}


Maybe someone can point me in the right direction?

Thanks for the answers.
 

Offline Renate

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: us
Re: STM32 SSD1306 number display problem
« Reply #1 on: April 22, 2021, 09:37:31 pm »
Code: (rich) [Select]
SSD1306_GotoXY (0,42);
SSD1306_Puts("Dust:- ",&Font_11x18, 1);
SSD1306_Putc((dust_converted/100)%10+'0',&Font_11x18, 1);
SSD1306_Putc((dust_converted/10)%10+'0',&Font_11x18, 1);
SSD1306_Putc((dust_converted%10)+'0',&Font_11x18, 1);
You're only converting two digits.
This kind of stuff should be in a routine anyway.
And I find '0' more soothing to the eye than 48.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf