cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f4 RTC Alarm with LCD

haythem
Associate II
Posted on June 19, 2014 at 17:40

HI, i want to configure the RTC alarm with LCD, to goal is to display the time in a LCD. I set the LCD and RTC but I do not know commant display the time on the LCD

This is my code to configure the RTC alarm

  1. void RTC_Config(void)
  2. {
  3. RTC_TimeTypeDef RTC_TimeStructure;
  4. RTC_InitTypeDef RTC_InitStructure;
  5.   /* Enable the PWR clock */
  6.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  7.   /* Allow access to RTC */
  8.   //PWR_BackupAccessCmd(ENABLE);
  9.   /* Reset RTC Domain */
  10.   RCC_BackupResetCmd(ENABLE);
  11.   RCC_BackupResetCmd(DISABLE);
  12.   /* Enable the LSE OSC */
  13.   RCC_LSEConfig(RCC_LSE_ON);
  14.   /* Wait till LSE is ready */  
  15.   while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  16.   {
  17.   }
  18.   /* Select the RTC Clock Source */
  19.   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  20.   /* Define the RCC with hours */
  21.   RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
  22.   RTC_InitStructure.RTC_SynchPrediv  = 0xFF;
  23.   RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
  24.   RTC_Init(&RTC_InitStructure);
  25.   
  26.   /* Define time to 00h 00mn 00s AM */
  27.   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
  28.   RTC_TimeStructure.RTC_Hours   = 0x00;  //0 h
  29.   RTC_TimeStructure.RTC_Minutes = 0x00;  //0 Minutes
  30.   RTC_TimeStructure.RTC_Seconds = 0x00;  // Secands
  31.   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
  32.   
  33. }
  34. void RTC_AlarmConfig(void)
  35. {
  36.   EXTI_InitTypeDef EXTI_InitStructure;
  37.   RTC_AlarmTypeDef RTC_AlarmStructure;
  38.   NVIC_InitTypeDef NVIC_InitStructure;
  39.   
  40.   /* EXTI configuration */
  41.   EXTI_ClearITPendingBit(EXTI_Line17);
  42.   EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  43.   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  44.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  45.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  46.   EXTI_Init(&EXTI_InitStructure);
  47.   
  48.   /* Enable the RTC Alarm Interrupt */
  49.   NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
  50.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  51.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  52.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  53.   NVIC_Init(&NVIC_InitStructure);
  54.  
  55.   /* Set the alarmA Masks */
  56.   RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All;
  57.   RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
  58.   
  59.   /* Set AlarmA subseconds and enable SubSec Alarm : generate 8 interripts per Second */
  60.   RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5);
  61.   /* Enable AlarmA interrupt */
  62.   RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  63. }

13 REPLIES 13
Posted on June 19, 2014 at 17:52

Perhaps you can use

http://www.cplusplus.com/reference/cstdio/sprintf/

to compose a string in the format you want, and then print the string to the LCD.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on June 19, 2014 at 18:16

Thank you , i have a problem with ''

PWR_BackupAccessCmd(ENABLE);''  than keil give me this erreur ''.\TIM_PWM_Output\TIM_PWM_Output.axf: Error: L6218E: Undefined symbol PWR_BackupAccessCmd (referred from main.o).'' i use the stm32f4  discovery library

Posted on June 19, 2014 at 18:57

Make sure you have the library file stm32f4xx_pwr.c in your project

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on June 21, 2014 at 19:21

Thank you for your support, now I think that I have successfully configured the RTC Alarm. Here is my setup code, but my problem is that I can not display the seconds, minutes and hours on the LCD. Else can you help me to resolve this problem?

  1. void RTC_Config(void)
  2. {
  3. RTC_TimeTypeDef RTC_TimeStructure;
  4. RTC_InitTypeDef RTC_InitStructure;
  5.   /* Enable the PWR clock */
  6.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  7.   /* Allow access to RTC */
  8.   PWR_BackupAccessCmd(ENABLE);
  9.   /* Reset RTC Domain */
  10.   RCC_BackupResetCmd(ENABLE);
  11.   RCC_BackupResetCmd(DISABLE);
  12.   /* Enable the LSE OSC */
  13.   RCC_LSEConfig(RCC_LSE_ON);
  14.   /* Wait till LSE is ready */  
  15.  /* while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  16.   {
  17.   }*/
  18.   /* Select the RTC Clock Source */
  19.   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  20.   /* Define the RCC with hours */
  21.   RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
  22.   RTC_InitStructure.RTC_SynchPrediv  = 0xFF;
  23.   RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
  24.   RTC_Init(&RTC_InitStructure);
  25.   
  26.   /* Define time to 00h 00mn 00s AM */
  27.   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
  28.   RTC_TimeStructure.RTC_Hours   = 0x00;  //0 h
  29.   RTC_TimeStructure.RTC_Minutes = 0x00;  //0 Minutes
  30.   RTC_TimeStructure.RTC_Seconds = 0x00;  // Secands
  31.   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
  32.   
  33. }
  34. void time_config(void)
  35. {
  36. RTC_AlarmTypeDef RTC_AlarmStructure;
  37. RTC_AlarmCmd(RTC_Alarm_A, DISABLE);   /* disable before setting or cann't write */
  38. /* set alarm time 8:30:0 everyday */
  39. RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_H12_AM;
  40. RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = 0x08;
  41. RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = 0x30;
  42. RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = 0x0;
  43. RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; // Nonspecific
  44. RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  45. RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay; // Everyday 
  46. RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
  47. /* Enable Alarm */
  48. RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  49. RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
  50. RTC_ClearFlag(RTC_FLAG_ALRAF);
  51. }
  52. void RTC_AlarmConfig(void)
  53. {
  54.   EXTI_InitTypeDef EXTI_InitStructure;
  55.   RTC_AlarmTypeDef RTC_AlarmStructure;
  56.   NVIC_InitTypeDef NVIC_InitStructure;
  57.   
  58.   /* EXTI configuration */
  59.   EXTI_ClearITPendingBit(EXTI_Line17);
  60.   EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  61.   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  62.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  63.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  64.   EXTI_Init(&EXTI_InitStructure);
  65.   
  66.   /* Enable the RTC Alarm Interrupt */
  67.   NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
  68.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  69.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  70.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  71.   NVIC_Init(&NVIC_InitStructure);
  72.  
  73.   /* Set the alarmA Masks */
  74.   RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All;
  75.   RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);
  76.   
  77.   /* Set AlarmA subseconds and enable SubSec Alarm : generate 8 interripts per Second */
  78.   RTC_AlarmSubSecondConfig(RTC_Alarm_A, 0xFF, RTC_AlarmSubSecondMask_SS14_5);
  79.   /* Enable AlarmA interrupt */
  80.   RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  81. }

Posted on June 21, 2014 at 22:37

And what Board and LCD are we talking about? Are there no demos/examples for your board using the LCD? STemWin, etc?

STM32F4xx_DSP_StdPeriph_Lib_V1.0.0\Project\STM32F4xx_StdPeriph_Examples\RTC\RTC_StopWatch\main.c

/* Get the Curent time */
RTC_GetTime(RTC_Format_BCD, &RTC_TimeStruct);
/* Display the curent time and the sub second on the LCD */
RTC_Time_display(Line2, Black , RTC_Get_Time(Secondfraction , &RTC_TimeStruct));

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on June 22, 2014 at 00:49

I work with keil ARM bord, i was connected an LCD 16*2 to my stm32f4

Posted on June 22, 2014 at 01:43

Ok, and can you output characters and strings to your display?

And can you use sprintf() to create a string to send to the display from the time structure read from the RTC?
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on June 22, 2014 at 03:57

To write in LCD i use this function, but that is no work with RTC

  1. void LCD_Print(char* str){ // Print a null terminated string.
  2. struct TASK Task;
  3. int strPtr;
  4. strPtr = 0;
  5. //LCD_WaitNotBusy(); // Wait until we are not busy first
  6. while(*(str + strPtr) != 0){
  7. Task.Command = WriteData;
  8. Task.Data = WriteData | *(str + strPtr);
  9. Task.Iter = 0;
  10. LCD_PushTask(Task);
  11. strPtr++;
  12. }
  13. return;
  14. }

Posted on June 22, 2014 at 04:21

Wouldn't something like this suffice?

{
char String[16];
RTC_TimeTypeDef RTC_TimeStruct;
/* Get the Curent time */
RTC_GetTime(RTC_Format_BCD, &RTC_TimeStruct);
sprintf(String,''%02X:%02X:%02X'',
RTC_TimeStruct.RTC_Hours,
RTC_TimeStruct.RTC_Minutes,
RTC_TimeStruct.RTC_Seconds);
LCD_Print(String);
}

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