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
14 REPLIES 14

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

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.​

sorry sir this is no answer.

Akhan.2
Associate II

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

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?