cancel
Showing results for 
Search instead for 
Did you mean: 

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?

Akhan.2
Associate II

* 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
1 ACCEPTED SOLUTION

Accepted Solutions
prain
Senior III

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);

View solution in original post

14 REPLIES 14

Move the sprintf() inside the loop

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
prain
Senior III

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
Associate II

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

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

Akhan.2
Associate II

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

S.Ma
Principal

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

What do you think this line does?

HAL_GPIO_LockPin(GPIOA, GPIO_PIN_0 == 1);

-- pa

Piranha
Chief II

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