OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3363|回复: 4

请问库函数版的RTC 怎么断电后还不是实时

[复制链接]

1

主题

4

帖子

0

精华

新手入门

积分
28
金钱
28
注册时间
2016-4-11
在线时间
6 小时
发表于 2016-4-11 23:05:34 | 显示全部楼层 |阅读模式
1金钱
下面贴代码#include "stm32f10x.h"
#include "stdio.h"
#include "time.h"
#define     PRINTF_ON  1

void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void RTC_Configuration(void);

void TimeShow(void);

void SetAlarm(struct tm t);
void SetCalendarTime(struct tm t);
void SetUnixTime(time_t);
struct tm  ConvUnixToCalendar(time_t t);
u32 ConvCalendarToUnix(struct tm t);
u32 GetUnixTime(void);


vu32  Display;

struct tm CurrentTime = {0,34,23,9,4,2016};
struct tm AlarmTime = {0,35,23,9,4,2016};


int main(void)
{
  RCC_Configuration();
  GPIO_Configuration();
    NVIC_Configuration();
    USART_Configuration();
    RTC_Configuration();
   
    SetCalendarTime(CurrentTime);
    SetAlarm(AlarmTime);
    while(1){ TimeShow(); }
}

void TimeShow(void)
{
    u32    Time = 0;
    if(Display)
    {
        Time = GetUnixTime();
        CurrentTime = ConvUnixToCalendar(Time);

        printf("\r\n Time : %d - %d - %d,%d : %d : %d \r\n",
            CurrentTime.tm_year,
            CurrentTime.tm_mon,
            CurrentTime.tm_mday,
            CurrentTime.tm_hour,
            CurrentTime.tm_min,
            CurrentTime.tm_sec);
        Display = 0;
    }
}

void SetCalendarTime(struct tm t)
{
    SetUnixTime(ConvCalendarToUnix(t));
}

void SetUnixTime(time_t t)
{
    RTC_WaitForLastTask();
    RTC_SetCounter((u32)t);
    RTC_WaitForLastTask();
}

void SetAlarm(struct tm t)
{
    RTC_WaitForLastTask();
    RTC_SetAlarm(ConvCalendarToUnix(t));
    RTC_WaitForLastTask();
}

u32 GetUnixTime(void)
{
    return (u32)RTC_GetCounter();
}

u32 ConvCalendarToUnix(struct tm t)
{
    t.tm_year -=1900;
    return mktime(&t);
}

struct tm  ConvUnixToCalendar(time_t t)
{
    struct tm *t_tm;
    t_tm = localtime(&t);
    t_tm->tm_year += 1900;
    return *t_tm;
}


void GPIO_Configuration(void)
{
      GPIO_InitTypeDef GPIO_InitStructure;                                                                                                                                                                                                                                                   
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;            
      GPIO_Init(GPIOA , &GPIO_InitStructure);

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;            
      GPIO_Init(GPIOA , &GPIO_InitStructure);
}

void RTC_Configuration(void)
{
    PWR_BackupAccessCmd(ENABLE);
    BKP_DeInit();
    RCC_LSEConfig(RCC_LSE_ON);
    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
    RCC_RTCCLKCmd(ENABLE);

    RTC_WaitForSynchro();
    RTC_WaitForLastTask();

    RTC_ITConfig(RTC_IT_SEC|RTC_IT_ALR,ENABLE);
    RTC_WaitForLastTask();

    RTC_SetPrescaler(32767);
    RTC_WaitForLastTask();
}


void RCC_Configuration(void)
{
    /* ???????? HSEStartUpStatus */
    ErrorStatus HSEStartUpStatus;

      /* ????????*/
      RCC_DeInit();
      /* ??HSE*/
      RCC_HSEConfig(RCC_HSE_ON);
      /* ??HSE?????*/
      HSEStartUpStatus = RCC_WaitForHSEStartUp();
    /* ??HSE??????,????if()?? */
      if(HSEStartUpStatus == SUCCESS)
      {
        /* ??HCLK(AHB)????SYSCLK 1?? */
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
        /* ??PCLK2???? HCLK(AHB) 1?? */
        RCC_PCLK2Config(RCC_HCLK_Div1);
        /* ??PCLK1???? HCLK(AHB) 2?? */
        RCC_PCLK1Config(RCC_HCLK_Div2);
        /* ??FLASH??????2 */
        FLASH_SetLatency(FLASH_Latency_2);
        /* ??FLASH???? */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
        /* ?????(PLL)????HSE 1??,????9,?PLL????? 8MHz * 9 = 72MHz */
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
        /* ??PLL */
        RCC_PLLCmd(ENABLE);
        /* ??PLL???? */
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
        /* ??SYSCLK????PLL */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        /* ??PLL??SYSCLK??? */
        while(RCC_GetSYSCLKSource() != 0x08);
      }
      /* ??APB2????GPIOA??*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
        
}


void USART_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;

    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;                                                                                                                                                      
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
    USART_ClockInit(USART1 , &USART_ClockInitStructure);

    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
    USART_Init(USART1,&USART_InitStructure);

     USART_Cmd(USART1,ENABLE);
}


void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

}
#include "stm32f10x_it.h"

#include "stdio.h"

extern vu32 Display;

void RTC_IRQHandler(void)
{
    if(RTC_GetFlagStatus(RTC_FLAG_ALR) != RESET){
        printf("\r\nIt's time to do sth.\r\n");        
    }else{
        Display =1 ;
   
    }

    RTC_ClearITPendingBit(RTC_IT_ALR|RTC_IT_SEC);
}


正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

1

主题

4

帖子

0

精华

新手入门

积分
28
金钱
28
注册时间
2016-4-11
在线时间
6 小时
 楼主| 发表于 2016-4-11 23:26:31 | 显示全部楼层
断电后重新上电还是按照初始设定的时刻走。这是为什么呢
回复

使用道具 举报

1

主题

4

帖子

0

精华

新手入门

积分
28
金钱
28
注册时间
2016-4-11
在线时间
6 小时
 楼主| 发表于 2016-4-12 13:31:51 | 显示全部楼层
请问哪里出现了问题
回复

使用道具 举报

70

主题

6768

帖子

0

精华

论坛大神

Rank: 7Rank: 7Rank: 7

积分
13151
金钱
13151
注册时间
2012-11-26
在线时间
3816 小时
发表于 2016-4-12 13:47:37 | 显示全部楼层
   RTC_Configuration();
     
   SetCalendarTime(CurrentTime);
    SetAlarm(AlarmTime);

不说啥了
学无止境
回复

使用道具 举报

1

主题

4

帖子

0

精华

新手入门

积分
28
金钱
28
注册时间
2016-4-11
在线时间
6 小时
 楼主| 发表于 2016-4-12 14:10:51 | 显示全部楼层
jermy_z 发表于 2016-4-12 13:47
RTC_Configuration();
     
   SetCalendarTime(CurrentTime);

请问是这个当前时间赋值的问题吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-10 19:40

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表