cancel
Showing results for 
Search instead for 
Did you mean: 

Seconds in RTC is jumping

Ramya Rajendran
Associate
Posted on December 11, 2017 at 13:51

Hello, I am developing a motor controller using STM32F407, I wrote the code for RTC and uploaded it. I found the time jumps seconds (2 or 3 seconds each time). RTC is crucial in my project as I have to control the motor and the valve. My code is given below, Can anyone tell me a solution.? Thanks in advance.

//////////////////////////// CODE BEGINS///////////////////////////////

#include 'main.h'

#include 'stm32f4xx_hal.h'

/* USER CODE BEGIN Includes */

#include 'stm32f4_discovery_lcd.h'

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

RTC_HandleTypeDef hrtc;

SRAM_HandleTypeDef hsram1;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

uint8_t text[50];

/* USER CODE END PV */

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

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_RTC_Init(void);

static void MX_FSMC_Init(void);

/* USER CODE BEGIN PFP */

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

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)

{

  /* USER CODE BEGIN 1 */

RTC_TimeTypeDef currentTime;

RTC_DateTypeDef currentDate;

char time[16] = {0};

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

  MX_FSMC_Init();

  /* USER CODE BEGIN 2 */

HAL_RTC_Init(&hrtc);

STM32f4_Discovery_LCD_Init();

LCD_SetColors(LCD_COLOR_YELLOW, LCD_COLOR_BLACK);

LCD_SetFont(&Font16x24);

LCD_SetColors(LCD_COLOR_YELLOW, LCD_COLOR_BLACK);

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

HAL_RTC_GetDate(&hrtc, &currentDate,  RTC_FORMAT_BIN);

HAL_RTC_GetTime(&hrtc, &currentTime,  RTC_FORMAT_BIN);

sprintf(time, '%d:%d:%d:%d \n', currentTime.Hours, currentTime.Minutes, currentTime.Seconds, currentTime.SubSeconds);

LCD_SetFont(&Font16x24);

LCD_SetColors(LCD_COLOR_YELLOW, LCD_COLOR_BLACK);

sprintf((char*)text,'Time = %s',time);

LCD_DisplayString(10,10,(uint8_t *) text);

}

  /* USER CODE END 3 */

}

//////////////////////////// CODE ENDS///////////////////////////////

1 REPLY 1
Posted on December 12, 2017 at 09:27

Read first time then date.

Read the Reading the calendar  When BYPSHAD control bit is cleared in the RTC_CR register chapter in RM and the documentation to those two Cube functions.

JW