Skip to main content
Akhan.2
Associate
July 29, 2020
Solved

i am trying to display the content of "count " IN TIMER 2 IN THE WHILE LOOP ON LCD DISPLAY BUT I CAN SEE ONLY "00" CAN YOU PLEASE CHECK THE MISTAKE IN THE CODE BELOW?

  • July 29, 2020
  • 14 replies
  • 2809 views

* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "i2c-lcd.h"

#include "stm32f4xx_hal.h"

#include "stdio.h"

#include "string.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* 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 ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;

TIM_HandleTypeDef htim2;

/* USER CODE BEGIN PV */

  

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

static void MX_TIM2_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

   

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

uint32_t count=0;

char result[10] ;

 /* 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_I2C1_Init();

 MX_TIM2_Init();

lcd_init();

 /* USER CODE BEGIN 2 */

  HAL_TIM_Base_Start_IT(&htim2);

sprintf(result, "%02d", count);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  count = __HAL_TIM_GetCounter(&htim2);

   lcd_send_cmd (0x80);

   lcd_send_string(result);

  HAL_Delay(5000);

   lcd_put_cur(2,0);

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

  • - - - = ---------------------------------------------------ETC IN THE STM32CUBEMX
This topic has been closed for replies.
Best answer by prain

sprintf(result, "%02d", count); should be just before lcd_send_string(result);

like this:

count = __HAL_TIM_GetCounter(&htim2);

   lcd_send_cmd (0x80);

sprintf(result, "%02d", count);

   lcd_send_string(result);

  HAL_Delay(5000);

   lcd_put_cur(2,0);

14 replies

Tesla DeLorean
Guru
July 29, 2020

Move the sprintf() inside the loop

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
prain
prainBest answer
Visitor II
July 29, 2020

sprintf(result, "%02d", count); should be just before lcd_send_string(result);

like this:

count = __HAL_TIM_GetCounter(&htim2);

   lcd_send_cmd (0x80);

sprintf(result, "%02d", count);

   lcd_send_string(result);

  HAL_Delay(5000);

   lcd_put_cur(2,0);

Akhan.2
Akhan.2Author
Associate
July 29, 2020

Mr Prain---You are the Man ! I wish I could be a student of a person of your knowledge and great caliber. Thank you soooooooooo much.

Would you allow me to ask any question regarding stm32 in future. I will desperately wait for your reply.

from arif

---from canada

prain
Visitor II
July 30, 2020

I'm glad to know your problem solved. Feel free to ask questions.

Akhan.2
Akhan.2Author
Associate
July 31, 2020

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32f4xx_hal.h"

#include "stdio.h"

#include "string.h"

#include "i2c-lcd.h"

/* Private includes ---------- ------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* 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 ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;

RTC_TimeTypeDef sTime;

RTC_DateTypeDef sDate;

RTC_HandleTypeDef hrtc;

/* USER CODE BEGIN PV */

 char time[10];

 char date[10];

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

static void MX_RTC_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

void reset_time(void)

{ sTime.Hours = 0x0;

   sTime.Minutes = 0x0;

   sTime.Seconds = 0x0;

   HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);

 

  sDate.WeekDay = RTC_WEEKDAY_SUNDAY;

   sDate.Month = RTC_MONTH_JULY;

   sDate.Date = 0x31;

   sDate.Year = 0x20;

HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);

  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); 

}

  void set_time(void)

{ sTime.Hours = 0x15;

   sTime.Minutes = 0x20;

   sTime.Seconds = 0x30;

   HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);

 

  sDate.WeekDay = RTC_WEEKDAY_SUNDAY;

   sDate.Month = RTC_MONTH_JULY;

   sDate.Date = 0x31;

   sDate.Year = 0x20;

HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);

  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); 

}

void get_time(void)

{

   RTC_DateTypeDef gDate;

    RTC_TimeTypeDef gTime;

 /* Get the RTC current Time */

   HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);

 /* Get the RTC current Date */

   HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN);

 /* Display time Format: hh:mm:ss */

   sprintf((char*)time,"%02d:%02d:%02d",gTime.Hours, gTime.Minutes, gTime.Seconds);

 /* Display date Format: mm-dd-yy */

   sprintf((char*)date,"%02d-%02d-%2d",gDate.Date, gDate.Month, 2000 + gDate.Year); 

 

}

  

 void display_time (void)

{

  lcd_send_cmd (0x80);

   lcd_send_string (time);

   lcd_send_cmd (0xc0);

   lcd_send_string (date);

}

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* 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_I2C1_Init();

 MX_RTC_Init();

lcd_init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

    

   if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)== 0)

      { get_time();

 display_time();

     HAL_Delay(10);

     }

 else

{

HAL_GPIO_LockPin(GPIOA, GPIO_PIN_0 == 1);

    reset_time();

   display_time();

      HAL_Delay(10);

}

  

}

}

  /* USER CODE BEGIN 3 */

 /* USER CODE END 3 */

in the code above when i reset my Stm32f4 board i can see the time and date and when i press the user button ( blue ) the time is reset to 00:00;00 but than it starts increasing by it self so how can i hold it to 00:00:00 until I press ESCAPE key OR RESET key on the board because i just want to increase or decrease the time by the user so that it can be set to any value.

Pavel A.
July 31, 2020

What do you think this line does?

HAL_GPIO_LockPin(GPIOA, GPIO_PIN_0 == 1);

-- pa

Akhan.2
Akhan.2Author
Associate
August 1, 2020

Dear Mr Pavel

HAL_GPIO_LockPin(GPIOA, GPIO_PIN_0 == 1);

i use this line in my code to latch the value of PA0 which is a user button to 1 so that i can increase or decrease the value of seconds and the time manualy in this format (00:00:00) because when i press button which is at PA0 for a moment the time shown on my lcd becomes 00:00:00 but than it start increasing by itself from right side which is for second. can you please tell me how can i hold the value of the button on pa0 and how can i increase or decrease the time when i press the increase or decrease buttons on other 2 pins.

from arif

S.Ma
Principal
July 31, 2020

I think it is time to practice and learn. Learn fishing than ask for a fish...

Akhan.2
Akhan.2Author
Associate
August 1, 2020

please do not make fun. I just need an answer if you can

RMcCa
Senior II
August 1, 2020

Ok. Learn to read the reference manual and use the register interface directly. There is no royal road to geometry. Sometimes you need to study something and think for yourself.​

Piranha
Principal III
August 1, 2020
Akhan.2
Akhan.2Author
Associate
August 8, 2020

USER CODE BEGIN Header */

/**

 ******************************************************************************

 * @file      : main.c

 * @brief     : Main program body

 ******************************************************************************

 * @attention

 *

 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.

 * All rights reserved.</center></h2>

 *

 * This software component is licensed by ST under BSD 3-Clause license,

 * the "License"; You may not use this file except in compliance with the

 * License. You may obtain a copy of the License at:

 *            opensource.org/licenses/BSD-3-Clause

 *

 ******************************************************************************

 */

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stdio.h"

#include "i2c-lcd.h"

#include "string.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* 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

 ---------------------------------------------------------*/

RTC_TimeTypeDef sTime; 

RTC_DateTypeDef sDate ;

RTC_HandleTypeDef hrtc;

I2C_HandleTypeDef hi2c1;

/* USER CODE BEGIN PV */

 char time[10];

 char date[10];

int i =1;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

static void MX_RTC_Init(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

void increase_seconds(void)

{

  sTime.Hours = 0x00;

      sTime.Minutes = 0x00;

      sTime.Seconds = 0x00+i;

  HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);  

}

void reset_time(void)

{ sTime.Hours = 0x00;

   sTime.Minutes = 0x00;

   sTime.Seconds = 0x00;

   HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);

 

  sDate.WeekDay = RTC_WEEKDAY_SUNDAY;

   sDate.Month = RTC_MONTH_JULY;

   sDate.Date = 0x31;

   sDate.Year = 0x20;

HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);

  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); 

}

  void set_time(void)

{ sTime.Hours = 0x15;

   sTime.Minutes = 0x20;

   sTime.Seconds = 0x30;

   HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);

 

  sDate.WeekDay = RTC_WEEKDAY_SATURDAY;

   sDate.Month = RTC_MONTH_AUGUST;

   sDate.Date = 8;

   sDate.Year = 0x20;

HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);

  HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); 

}

void get_time(void)

{

   RTC_DateTypeDef gDate;

    RTC_TimeTypeDef gTime;

 /* Get the RTC current Time */

   HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);

 /* Get the RTC current Date */

   HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN);

 /* Display time Format: hh:mm:ss */

   sprintf((char*)time,"%02d:%02d:%02d",gTime.Hours, gTime.Minutes, gTime.Seconds);

 /* Display date Format: mm-dd-yy */

   sprintf((char*)date,"%02d-%02d-%2d",gDate.Date, gDate.Month, 2000 + gDate.Year); 

 

}

  

 void display_time (void)

{

  lcd_send_cmd (0x80);

   lcd_send_string (time);

   lcd_send_cmd (0xc0);

   lcd_send_string (date);

}

  

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* 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_I2C1_Init();

 MX_RTC_Init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

      

{

      if( HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0 )!=1)

         {

get_time();

         display_time();

}

       else

         {

break;

}

            

}

  

     // increase time button PA1//

        for(i=1; i<=79; i++)

          

{

increase_seconds();

             get_time();

           display_time();

HAL_Delay(1000);

  if(i >79) {

             break;

            }

          }

    get_time();

            display_time();

  

}

  

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  

 /* USER CODE END 3 */

IN in the above code seconds displayed like this 00:00:15 but after that it becomes 00:00:10 although it should be 00:00:16 and when it reaches 00:00:25 after that it becomes 00:00:20 and than when it reaches to 00:00:35 than after that it becomes 00:00:30. can anyone tell me the error in the code.

Piranha
Principal III
August 8, 2020

You have buffer overflow because you are using dangerous sprintf() where the safe snprintf() should be used.

Also you are too lazy to put your code in Code Snippet and even fix the title of the topic, which basically is showing disrespect to others, who are trying to help you. And stop whining and demanding the others to do the job for you for free.

The code you are posting is absolute nonsense which shows that you lack even the basics of microcontrollers, C language and programming in general. There is no small error - it's a general lack of understanding how it all works. Either you learn the basics or drop this whole programming thing as inappropriate for your abilities. But, if you want to learn, I already gave you a link, which is a great starting point for beginners. Have you started with it?