RTC Time of stm32f407

Source: Internet
Author: User
Tags printf sprintf
Rtc_inittypedef rtc_initstructure;
Rtc_timetypedef rtc_timestructure;
Rtc_datetypedef rtc_datastructure;


/****************************************************************************************
* Function Name: BSP_INITRTC
* Function function: Initialize RTC
* Formal Parameters: None
* Return value: None
*****************************************************************************************/


void Bsp_initrtc (void)
{
Rtc_inittypedef rtc_initstructure;

Rcc_apb1periphclockcmd (rcc_apb1periph_pwr,enable);
Pwr_backupaccesscmd (enable);/* Enable backup Register access */

if (Rtc_readbackupregister (rtc_bkp_dr0)! = 0xa0a0)
{
Rcc_lseconfig (rcc_lse_on);/* Enable LSE oscillator */
while (Rcc_getflagstatus (rcc_flag_lserdy) = = RESET)/* Wait ready */
{
}
Rcc_rtcclkconfig (Rcc_rtcclksource_lse);/* Select RTC Clock Source */
Rcc_rtcclkcmd (enable);/* Enable RTC clock */
Rtc_waitforsynchro ();/* Wait for RTC APB Register to synchronize */
/* Configure the RTC data Register and Divider */
Rtc_initstructure.rtc_asynchprediv = 0x7f;
Rtc_initstructure.rtc_synchprediv = 0xFF;
Rtc_initstructure.rtc_hourformat = rtc_hourformat_24;
Rtc_init (&rtc_initstructure);
/* Set Month day and week */
Rtc_datastructure.rtc_year = 0x15;
Rtc_datastructure.rtc_month = 0x12;
Rtc_datastructure.rtc_date = 0x06;
Rtc_datastructure.rtc_weekday = 0x07;
Rtc_setdate (rtc_format_bcd,&rtc_datastructure);
/* minutes and seconds and display format */
Rtc_timestructure.rtc_h12 = Rtc_h12_am;
Rtc_timestructure.rtc_hours = 0x21;
Rtc_timestructure.rtc_minutes = 0x30;
Rtc_timestructure.rtc_seconds = 0x00;
Rtc_settime (rtc_format_bcd,&rtc_timestructure);
/* Configure the backup register to indicate that a rtc*/has been set
Rtc_writebackupregister (RTC_BKP_DR0,0XA0A0);
}

}

/*
*********************************************************************************************************
* Function Name: main
* Function Description: C Program Entry
* Formal Parameters: None
* Return Value: Error code (no processing required)
*********************************************************************************************************
*/
int main (void)
{
Char buf[30];
/*
The boot file in the St firmware library has been executed with the Systeminit () function, the function in the SYSTEM_STM32F4XX.C file, the main function is
Configure the CPU system clock, internal flash access timing, configuration FSMC for external SRAM
*/
Bsp_init ();//Initialize RTC


/* Enter the main program loop body */
while (1)
{
Rtc_gettime (Rtc_format_bin, &rtc_timestructure);
Rtc_getdate (Rtc_format_bin, &rtc_datastructure);
sprintf (BUF,
"20%0.2d/%0.2d/%0.2d",
Rtc_datastructure.rtc_year,
Rtc_datastructure.rtc_month,
Rtc_datastructure.rtc_date);
printf ("%s", buf);//output time to serial port
printf ("\ r \ n");//newline

sprintf (BUF,
"%0.2d:%0.2d:%0.2d",
Rtc_timestructure.rtc_hours,
Rtc_timestructure.rtc_minutes,
Rtc_timestructure.rtc_seconds);
printf ("%s", buf);//output date to serial port
printf ("\ r \ n");//newline

Bsp_delayms (990);
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.