示例#1
0
void RTC_Configuration(void)
{
	/* Enable PWR and BKP clocks */
  	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  	/* Allow access to BKP Domain */
  	PWR_BackupAccessCmd(ENABLE);

  	/* Reset Backup Domain */
  	BKP_DeInit();

#ifdef RTCClockSource_LSI
  /* Enable LSI */ 
  RCC_LSICmd(ENABLE);
  /* Wait till LSI is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {
  }

  /* Select LSI as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);  
#elif defined	RTCClockSource_LSE
  	/* Enable LSE */
  	RCC_LSEConfig(RCC_LSE_ON);
  	/* Wait till LSE is ready */
  	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  	{}

  	/* Select LSE as RTC Clock Source */
  	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#endif
  	/* Enable RTC Clock */
  	RCC_RTCCLKCmd(ENABLE);

  	/* Wait for RTC registers synchronization */
  	RTC_WaitForSynchro();

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();

  	/* Enable the RTC Second */
  	RTC_ITConfig(RTC_IT_SEC, ENABLE);

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();

  	/* Set RTC prescaler: set RTC period to 1sec */
      /* Set RTC prescaler: set RTC period to 1sec */
#ifdef RTCClockSource_LSI
  		RTC_SetPrescaler(31999); /* RTC period = RTCCLK/RTC_PR = (32.000 KHz)/(31999+1) */
#elif defined	RTCClockSource_LSE
  		RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
#endif

  //	RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  	/* Wait until last write operation on RTC registers has finished */
  	RTC_WaitForLastTask();
}
示例#2
0
// 通过直接计算校准RTC时钟(使用0.01ms周期的systick测量再修正RTC预分频系数)。
void RTC_Calibrate(void)
{
	unsigned int systick_sta;
	unsigned int rtctick_lmt;
	unsigned int prescaler 	= 0;	

	// 在使用systick测量RTC tick的周期长度之前,初始化systick
	NVIC_SetPriority(SysTick_IRQn, 0);	
	
	SysTick_Init();
	
	// 开始rtcalarm计数并使能rtcalarm中断
	SysTick_Enable();

	// 先设置默认的RTC预分频系数(rtc tike = ~1ms)
	RTC_SetPrescaler(DEF_RTC_PRESCALER);

	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);

	/* Wait for RTC registers synchronization */
	RTC_WaitForSynchro();

	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();

	// 测量RTC单周期长度持续~1秒
	rtctick_lmt = systick+1000/SYSTICK_PERIOD;

	// 记录测量前的systick值
	systick_sta = systick;

	// 测量1000个周期的rtc tick(~1s)
	while(systick < rtctick_lmt);

	prescaler = (DEF_RTC_PRESCALER * 1000)/((systick-systick_sta)/100);

	// printf("prescaler = %d.\r\n", prescaler);	

	// 重新设置RTC预分频系数
	RTC_SetPrescaler(prescaler);

	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);

	/* Wait for RTC registers synchronization */
	RTC_WaitForSynchro();

	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();

	// 校准完RTC时钟后,停止rtcalarm计数并禁止rtcalarm中断
	SysTick_Disable();

	// printf("RTC Clock calibrated.\r\n");
}
示例#3
0
static void rtcConfig(void)
{
    /* Enable PWR and BKP clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);

    /* Reset Backup Domain */
    BKP_DeInit();

    /* Enable the LSI OSC */
    RCC_LSICmd(ENABLE);

    /* Wait till LSI is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET);

    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

    /* Enable RTC Clock */
    RCC_RTCCLKCmd(ENABLE);

    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    /* Enable the RTC Second */
    RTC_ITConfig(RTC_IT_SEC, ENABLE);

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

#ifndef PRECISION_IN_MS
    /* Set RTC prescaler: set RTC period to 1 sec */
    RTC_SetPrescaler(40000); //LSI default is 40k HZ
#else
    /* Set RTC prescaler: set RTC period to 1ms */
    RTC_SetPrescaler(40); //LSI default is 40k HZ
#endif

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    /* To output second signal on Tamper pin, the tamper functionality
     * must be disabled (by default this functionality is disabled)
     */
    //BKP_TamperPinCmd(DISABLE);

    /* Enable the RTC Second Output on Tamper Pin */
    //BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
}
示例#4
0
文件: rtc.c 项目: yzfcer/wind-os
/*
************************************************************
*	函数名称:	RTC_Init
*
*	函数功能:	RTC初始化
*
*	入口参数:	无
*
*	返回参数:	无
*
*	说明:		
************************************************************
*/
_Bool RTC_Init(void)
{
	
#if(USE_EXT_RCC == 1)
	unsigned char errCount = 0;
#endif

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟   
	PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
	
	BKP_DeInit();	//复位备份区域
#if(USE_EXT_RCC == 1)
	RCC_LSEConfig(RCC_LSE_ON);	//设置外部低速晶振(LSE),使用外设低速晶振
	while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && errCount < 250)	//检查指定的RCC标志位设置与否,等待低速晶振就绪
	{
		errCount++;
		DelayMs(10);
	}
	if(errCount >= 250)
		return 1; //初始化时钟失败,晶振有问题
#endif
	
#if(USE_EXT_RCC == 1)
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); 		//设置RTC时钟(RTCCLK),选择LSE作为RTC时钟
#else
	RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);	//设置RTC时钟(HSE/128),选择HES作为RTC时钟
#endif
	RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
	RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
	RTC_WaitForSynchro(); //等待RTC寄存器同步
	
	RTC_ITConfig(RTC_IT_ALR, ENABLE); //使能RTC闹钟中断
	RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
	
	RTC_EnterConfigMode(); //允许配置
#if(USE_EXT_RCC == 1)
	RTC_SetPrescaler(32767); //设置RTC预分频的值
#else
	RTC_SetPrescaler(HSE_VALUE / 128 - 1); //设置RTC预分频的值
#endif
	RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
	
	RTC_SetCounter(0); //设置RTC计数器的值
	RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
	
	RTC_ExitConfigMode(); //退出配置模式
	
	RTC_NVIC_Init();
	
	return 0;

}
示例#5
0
void RTC_Configuration(void)
{
 	/* Enable PWR and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
 	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd(ENABLE);
 	/* Reset Backup Domain */
	BKP_DeInit();
 	// Select HSE/128 as RTC Clock Source
	RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);  
	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);
	/* Wait for RTC registers synchronization */
	RTC_WaitForSynchro();
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	/* Enable the RTC Second */  
//	RTC_ITConfig(RTC_IT_SEC, ENABLE);
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	/* Set RTC prescaler: set RTC period to 1sec  1ms */
	RTC_SetPrescaler(62); /*62499 RTC period = RTCCLK/RTC_PR = 8M / 128 = 62.5kHz ) -> (62499+1) */
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
}
示例#6
0
void RTC_Configuration(void)
{
	/* Reset Backup Domain */
	BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

}
示例#7
0
文件: rtc.c 项目: e-asphyx/tomatobox
void rtc_init() {
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

	if(BKP->DR1 != BKP_MAGIC) {
		PWR_BackupAccessCmd(ENABLE);                        /* Allow write access to BKP Domain */

		RCC_BackupResetCmd(ENABLE);                         /* Reset Backup Domain */
		RCC_BackupResetCmd(DISABLE);

		RCC_LSEConfig(RCC_LSE_ON);                          /* Enable LSE */
		while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Wait till LSE is ready */
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);             /* Select LSE as RTC Clock Source */
		RCC_RTCCLKCmd(ENABLE);                              /* Enable RTC Clock */

		RTC_WaitForSynchro();                               /* Wait for RTC registers synchronization */
		RTC_WaitForLastTask();
		RTC_SetPrescaler(32767);                            /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
		RTC_WaitForLastTask();

		BKP->DR1 = BKP_MAGIC;

		PWR_BackupAccessCmd(DISABLE);                       /* Protect backup registers */

		rtc_is_valid = 0;
	} else {
		/* Wait for RTC registers synchronization */
		RTC_WaitForSynchro();
	}
}
示例#8
0
文件: rtc.c 项目: pyjhhh/stm32_f1x
 /**
  * @file   RTC_Configuration
  * @brief  Configures the RTC.
  * @param  无
  * @retval 无
  */
static void RTC_Configuration(void)
{
  //启用PWR和BKP的时钟(from APB1)
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  //后备域解锁
  PWR_BackupAccessCmd(ENABLE);
  //备份寄存器模块复位
  BKP_DeInit();
  //外部32.768K时钟使能
  RCC_LSEConfig(RCC_LSE_ON);
  //等待稳定
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}
  //RTC时钟源配置成LSE(外部32.768K)
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  //RTC开启
  RCC_RTCCLKCmd(ENABLE);
  //开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器
  RTC_WaitForSynchro();
  //读写寄存器前,要确定上一个操作已经结束
  RTC_WaitForLastTask();
  //使能秒中断
  RTC_ITConfig(RTC_IT_SEC, ENABLE);
  //等待寄存器写入完成
  RTC_WaitForLastTask();
  //设置RTC分频器,使RTC时钟为1Hz
  //RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1)
  RTC_SetPrescaler(32767); 
  //等待写入完成
  RTC_WaitForLastTask();
}
示例#9
0
/**
  * @brief  Configures RTC clock source and prescaler.
  * @param  None
  * @retval None
  */
void RTC_Configuration(void)
{
    /* RTC clock source configuration ----------------------------------------*/
	/* Allow access to BKP Domain */
  	PWR_BackupAccessCmd(ENABLE);
	
    /* Reset Backup Domain */
    BKP_DeInit();
  
    /* Enable LSE OSC */
	RCC_LSICmd(ENABLE);
    /* Wait till LSE is ready */
	while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
    {
    }

    /* Select the RTC Clock Source */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);

    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);

    /* RTC configuration -----------------------------------------------------*/
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();

    /* Set the RTC time base to 1s */
    RTC_SetPrescaler(32767);  
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
	RTC_ITConfig(RTC_IT_ALR, ENABLE);
	RTC_WaitForLastTask();
  
}
示例#10
0
uint8_t RTC_AutoTest(void)
{

	uint8_t i = 0;
	/* Enable PWR and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	
	/* Allow access to BKP Domain */
	PWR_BackupAccessCmd(ENABLE);
	
	/* Reset Backup Domain */
	BKP_DeInit();
	
	/* Enable LSE */
	RCC_LSEConfig(RCC_LSE_ON);
	
	/* Wait till LSE is ready */
	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
	{	
		Delay_us(50000);
		i++;
		
		if(i>100) //5秒都还没起振,坏了
		{
			return 0;
		}
	}	

	/* Select LSE as RTC Clock Source */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
	
	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);
	
	/* Wait for RTC registers synchronization 
	 * 因为RTC时钟是低速的,内环时钟是高速的,所以要同步
	 */
	RTC_WaitForSynchro();
	
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	
	/* Enable the RTC Second */
	RTC_ITConfig(RTC_IT_SEC, ENABLE);
	
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	
	/* Set RTC prescaler: set RTC period to 1sec */
	RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) = 1HZ */
	
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	
	return 1;


}
示例#11
0
void rtc_timer_start(u32 alarmValue) 
{
    //enable BKP and PWR, Clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWR , ENABLE);
    
    // RTC clock source configuration 
    PWR_BackupAccessCmd(ENABLE);                      //Allow access to BKP Domain
    RCC_LSEConfig(RCC_LSE_ON);                        //Enable LSE OSC
    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET); //Wait till LSE is ready
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);           //Select the RTC Clock Source
    RCC_RTCCLKCmd(ENABLE);                            //enable RTC
    
    // RTC configuration 
    // Wait for RTC APB registers synchronisation 
    RTC_WaitForSynchro();
    
    RTC_SetPrescaler(0);                              //Set the RTC time base to 30.5us
    RTC_WaitForLastTask();                            //Wait until last write operation on RTC registers has finished

    //Set the RTC time counter to 0
    RTC_SetCounter(0);
    RTC_WaitForLastTask();

    // Set the RTC time alarm(the length of slot)
    RTC_SetAlarm(alarmValue);
    RTC_WaitForLastTask();
    
    //interrupt when reach alarm value
    RTC_ClearFlag(RTC_IT_ALR);
    RTC_ITConfig(RTC_IT_ALR, ENABLE);
   
    //Configures EXTI line 17 to generate an interrupt on rising edge(alarm interrupt to wakeup board)
    EXTI_ClearITPendingBit(EXTI_Line17);
    EXTI_InitTypeDef  EXTI_InitStructure; 
    EXTI_InitStructure.EXTI_Line    = EXTI_Line17;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt; 
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE; 
    EXTI_Init(&EXTI_InitStructure);
   
    //Configure RTC global interrupt:
    //Configure NVIC: Preemption Priority = 1 and Sub Priority = 1
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel                    = RTC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority         = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                 = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    //Configure RTC Alarm interrupt:
    //Configure NVIC: Preemption Priority = 0 and Sub Priority = 1
    NVIC_InitStructure.NVIC_IRQChannel                    = RTCAlarm_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority         = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                 = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
示例#12
0
文件: Calendar.c 项目: afxstar/Mplib
/*********************************************************************************************************//**
 * @brief Configures the RTC.
 * @retval None
 * @details RTC configuration as following:
 *  - S/W reset backup domain.
 *  - Configure RTC.
 *    - Select LSI as RTC Clock Source.
 *    - Enable the RTC Second interrupt.
 *    - RTC prescaler = 32768 to generate 1 second interrupt.
 *    - Enable clear RTC counter by match function.
 *
 ************************************************************************************************************/
void RTC_Configuration(void)
{
    /* Reset Backup Domain */
    PWRCU_DeInit();

    RTC_ClockSourceConfig(RTC_SRC_LSI);
    RTC_IntConfig(RTC_INT_CSEC, ENABLE);
    RTC_SetPrescaler(RTC_RPRE_32768);

    /* Restart counter when match event occured */
    RTC_CMPCLRCmd(ENABLE) ;
}
/*******************************************************************************
* Function Name  : RTC_Configuration
* Description    : Configures the RTC.
* Input          : None
* Output         : None
* Return         : 0 reday,-1 error.
*******************************************************************************/
int RTC_Configuration(void)
{
    u32 count=0x200000;

    /* Enable PWR and BKP clocks */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);

    /* Reset Backup Domain */
    BKP_DeInit();

    /* Enable LSE */
    RCC_LSEConfig(RCC_LSE_ON);
    /* Wait till LSE is ready */
    while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count) );
    if ( count == 0 )
    {
		rt_kprintf("RTC no init!");
        return -1;
    }

    /* Select LSE as RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

    /* Enable RTC Clock */
    RCC_RTCCLKCmd(ENABLE);

    /* Wait for RTC registers synchronization */
    RTC_WaitForSynchro();

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
	
	/* Enable the RTC Second */
	/* 使能RTC的秒中断 */	
	//RTC_ITConfig(RTC_IT_SEC, ENABLE);

	/* Wait until last write operation on RTC registers has finished */
	/* 等待上一次对RTC寄存器的写操作完成 */	
	//RTC_WaitForLastTask();

    /* Set RTC prescaler: set RTC period to 1sec */
    RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    return 0;
}
示例#14
0
/**
  * @brief  Configures the RTC.
  * @param  None
  * @retval None
  */
void RTC_Configuration(void) {
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Reset Backup Domain */
  BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}
示例#15
0
文件: rtc.c 项目: vcheung/STM32
/*
 * 函数名:RTC_Configuration
 * 描述  :配置RTC
 * 输入  :无
 * 输出  :无
 * 调用  :外部调用
 */
void RTC_Configuration(void)
{
	/* Enable PWR(电源控制) and BKP clocks */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	
	/* Allow access to BKP Domain 
	 * 允许进入后备区域进行写操作,因为后备寄存器中放的是重要的数据,默认是不允许往里面写入值的 */
	PWR_BackupAccessCmd(ENABLE);
	
	/* Reset Backup Domain 
	 *将后背寄存器的寄存器值设为默认值 */
	BKP_DeInit();
	
	/* Enable LSE 
	 *打开外部低速晶振,RTC可以选择的时钟源是外部和内部低速晶振及外部高速晶振,这里我们选择外部低速晶振32768HZ */
	RCC_LSEConfig(RCC_LSE_ON);
	/* Wait till LSE is ready 
	 *等待外部低速晶振准备就序 */
	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
	{}
	
	/* Select LSE as RTC Clock Source 
	 * 选择外部低速晶振为RTC的时钟源 */
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
	
	/* Enable RTC Clock */
	RCC_RTCCLKCmd(ENABLE);
	
	/* Wait for RTC registers synchronization
	 * 等待RTC寄存器与RTC的APB时钟同步 */
	RTC_WaitForSynchro();
	
	/* Wait until last write operation on RTC registers has finished
	 * 等待上次对RTC寄存器配置完成 */
	RTC_WaitForLastTask();
	
	/* Enable the RTC Second
	 * 使能RTC中断_秒中断 */
	RTC_ITConfig(RTC_IT_SEC, ENABLE);
	
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
	
	/* Set RTC prescaler: set RTC period to 1sec
	 * 设置RTC的预分频值,因为外部低速晶振是32768,所以选择RTC计数器计数频率
	 * = RTCCLK/RTC_PR =(32.768 KHz)/(32767+1) */
	RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
	
	/* Wait until last write operation on RTC registers has finished */
	RTC_WaitForLastTask();
}
示例#16
0
void RTC_STOP_WakeUp(void)
{
    /* Enable PWR and BKP clocks */
    /* PWR时钟(电源控制)与BKP时钟(RTC后备寄存器)使能 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);

    /* RTC clock source configuration ----------------------------------------*/
    /* Reset Backup Domain */
    BKP_DeInit();

    /* Enable LSE OSC */
    RCC_LSEConfig(RCC_LSE_ON);
    /* Wait till LSE is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
    {
    }

    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);

    /* RTC configuration -----------------------------------------------------*/
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();

    /* Set the RTC time base to 1s */
    RTC_SetPrescaler(32767);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    /* Enable the RTC Alarm interrupt */
    RTC_ITConfig(RTC_IT_ALR, ENABLE);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();

    RTC_ClearFlag(RTC_FLAG_SEC);
    while(RTC_GetFlagStatus(RTC_FLAG_SEC) == RESET);

    /* Alarm in 5 second */
    RTC_SetAlarm(RTC_GetCounter()+ 5);
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
}
示例#17
0
void RTC_Configuration(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
	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, ENABLE);
	RTC_WaitForLastTask();
	RTC_SetPrescaler(32767); 
	RTC_WaitForLastTask();	
}
示例#18
0
void timer_start()
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,
			       ENABLE);
	PWR_BackupAccessCmd(ENABLE);

	RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
	RCC_RTCCLKCmd(ENABLE);
	RTC_WaitForLastTask();
	RTC_WaitForSynchro();
	RTC_WaitForLastTask();
	RTC_SetPrescaler(0);	// counting at 8e6 / 128
	RTC_WaitForLastTask();
	RTC_SetCounter(0);
	RTC_WaitForLastTask();
}
示例#19
0
/*******************************************************************************
* 函 数 名:	
* 功    能:	
* 参    数:
* 返    回:	
*******************************************************************************/
uint8_t RTC_Init(uint32_t pre_value, uint32_t alarm_value, uint32_t count_value)
{	
	uint8_t count;
    NVIC_InitTypeDef NVIC_InitStructure;
	//RTC全局中断
	NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;		
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;	
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		
	NVIC_Init(&NVIC_InitStructure);		

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);	
	PWR_BackupAccessCmd(ENABLE);
	//设置外部低速晶振(LSE),使用外设低速晶振
	RCC_LSEConfig(RCC_LSE_ON);	
	//检查指定的RCC标志位设置与否,等待低速晶振就绪
	while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)	
	{
		count++;
		Delayms(10);
	}
	if(count>=250)
		return 1;
	//设置RTC时钟(RTCCLK),选择LSE作为RTC时钟   
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);		 
	RCC_RTCCLKCmd(ENABLE);	
	//等待最近一次对RTC寄存器的读操作完成
	RTC_WaitForSynchro();
	//等待最近一次对RTC寄存器的写操作完成
	RTC_WaitForLastTask();	
	//设置报警时间
	RTC_SetAlarm(alarm_value);
	RTC_WaitForLastTask();	
	//配置中断
	RTC_ITConfig( RTC_IT_SEC|RTC_IT_ALR|RTC_IT_OW, ENABLE);	
	RTC_WaitForLastTask();	
	//设置RTC预分频的值
	RTC_SetPrescaler(pre_value); 
	RTC_WaitForLastTask();	
	//清除RCC的复位标志位
	RCC_ClearFlag();
	
 	RTC_WaitForLastTask();	
	RTC_SetCounter(count_value);	
	RTC_WaitForLastTask();	
	return 0;
}
/**
  * @brief  Configures RTC clock source and prescaler.
  * @param  None
  * @retval None
  */
void RTC_Configuration(void)
{
  /* Check if the StandBy flag is set */
  if(PWR_GetFlagStatus(PWR_FLAG_SB) != RESET)
  {/* System resumed from STANDBY mode */

    /* Turn on LD4 */
    STM32vldiscovery_LEDOn(LED4);

    /* Clear StandBy flag */
    PWR_ClearFlag(PWR_FLAG_SB);

    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();
    /* No need to configure the RTC as the RTC configuration(clock source, enable,
       prescaler,...) is kept after wake-up from STANDBY */
  }
  else
  {/* StandBy flag is not set */

    /* RTC clock source configuration ----------------------------------------*/
    /* Reset Backup Domain */
    BKP_DeInit();
  
    /* Enable LSE OSC */
    RCC_LSEConfig(RCC_LSE_ON);
    /* Wait till LSE is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
    {
    }

    /* Select the RTC Clock Source */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

    /* Enable the RTC Clock */
    RCC_RTCCLKCmd(ENABLE);

    /* RTC configuration -----------------------------------------------------*/
    /* Wait for RTC APB registers synchronisation */
    RTC_WaitForSynchro();

    /* Set the RTC time base to 1s */
    RTC_SetPrescaler(32767);  
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
  }
}
示例#21
0
uint08 RTC_Init(void)
{
    //检查是不是第一次配置时钟
    uint08 temp=0;

    if ( BKP_ReadBackupRegister(BKP_DR1) != 0x5050 )      //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);    //使能PWR和BKP外设时钟   
        PWR_BackupAccessCmd(ENABLE);    //使能后备寄存器访问 
        BKP_DeInit();   //复位备份区域 	
        RCC_LSEConfig(RCC_LSE_ON);  //设置外部低速晶振(LSE),使用外设低速晶振
        while ( RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && temp< 252) //检查指定的RCC标志位设置与否,等待低速晶振就绪
        {
            temp++;
            //delay_ms(10);
            //OSTimeDlyHMSM(0, 0, 0, 10); 
        }
        if ( temp>=250 )
        {
          return 1;//初始化时钟失败,晶振有问题	 
        }
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);     //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟    
        RCC_RTCCLKCmd(ENABLE);  //使能RTC时钟  
        RTC_WaitForLastTask();  //等待最近一次对RTC寄存器的写操作完成
        RTC_WaitForSynchro();       //等待RTC寄存器同步  
        RTC_ITConfig(RTC_IT_SEC, ENABLE);       //使能RTC秒中断
        RTC_WaitForLastTask();  //等待最近一次对RTC寄存器的写操作完成
        RTC_EnterConfigMode();/// 允许配置	
        RTC_SetPrescaler(32767); //设置RTC预分频的值
        RTC_WaitForLastTask();  //等待最近一次对RTC寄存器的写操作完成
        RTC_Set(2009,12,2,10,0,55);  //设置时间	
        RTC_ExitConfigMode(); //退出配置模式  
        BKP_WriteBackupRegister(BKP_DR1, 0X5050);   //向指定的后备寄存器中写入用户程序数据
    }
    else//系统继续计时
    {

        RTC_WaitForSynchro();   //等待最近一次对RTC寄存器的写操作完成
        RTC_ITConfig(RTC_IT_SEC, ENABLE);   //使能RTC秒中断
        RTC_WaitForLastTask();  //等待最近一次对RTC寄存器的写操作完成
    }
    RTC_NVIC_Config();//RCT中断分组设置		    				     
    RTC_Get();//更新时间	
    return 0; //ok

}                           
示例#22
0
文件: rtc.c 项目: Farewellly/STM32
void RTCInit(void) {
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);  

	// Allow access to BKP Domain
	PWR_BackupAccessCmd(ENABLE);

	// Reset Backup Domain
	BKP_DeInit();

	// Enable the LSE OSC
//	RCC_LSEConfig(RCC_LSE_ON);

	// Disable the LSI OSC
	RCC_LSICmd(ENABLE);

	// Select the RTC Clock Source
	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //RCC_RTCCLKSource_LSE);

	// Enable the RTC Clock
	RCC_RTCCLKCmd(ENABLE);

	// Wait for RTC registers synchronization
	RTC_WaitForSynchro();

	// Wait until last write operation on RTC registers has finished
	RTC_WaitForLastTask();

	// Enable the RTC overflow interrupt
	RTC_ITConfig(RTC_IT_SEC, ENABLE);

	//Set 32768 prescaler - for one second interupt
	RTC_SetPrescaler(0x7FFF);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

	NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

}
示例#23
0
文件: rtc.c 项目: 9zigen/stm32
// Init RTC
void RTC_Init(void) {
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP,ENABLE); // Enable power control and backup domain
	PWR_BackupAccessCmd(ENABLE); // Enable BKP and RTC registers

	// Init and enable RTC if it is not enabled
	if ((RCC->BDCR & RCC_BDCR_RTCEN) != RCC_BDCR_RTCEN) {
		RCC_LSEConfig(RCC_LSE_ON); // Turn on LSE oscillator
		while(!RCC_GetFlagStatus(RCC_FLAG_LSERDY)) {} // Wait till LSE is ready
		while((RCC->BDCR & RCC_BDCR_LSEON) != RCC_BDCR_LSEON) {}
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Set LSE as clock source
		RCC_RTCCLKCmd(ENABLE); // Enable RTC clock
		RTC_WaitForSynchro();
		RTC_WaitForLastTask();
		RTC_SetPrescaler(32768); // Set prescaler --> 1Hz with 32.768KHz quartz on demoboard
		RTC_WaitForLastTask();
	}
	RTC_ITConfig(RTC_IT_SEC,ENABLE); // Enable RTC IRQ
	RTC_WaitForLastTask();
}
示例#24
0
文件: rtc.c 项目: pl4nkton/drquad32
void rtc_init(void)
{
    RCC_APB1PeriphClockCmd(
        RCC_APB1Periph_PWR |
        RCC_APB1Periph_BKP,
        ENABLE
    );

    PWR_BackupAccessCmd(ENABLE);
    // BKP_DeInit();

    RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
    RCC_RTCCLKCmd(ENABLE);

    RTC_WaitForSynchro();
    RTC_WaitForLastTask();

    RTC_SetPrescaler(RTC_PRESCALER-1);
    RTC_WaitForLastTask();
}
示例#25
0
u8 RTC_Init(void)
	{
	//检查是不是第一次配置时钟
	u8 temp=0;
	RTC_NVIC_Config();
	//if(BKP->DR1!=0X5050)//第一次配置
	if (BKP_ReadBackupRegister(BKP_DR1) != 0x5050)		//从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎
		{	 			
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);	//使能PWR和BKP外设时钟   
		PWR_BackupAccessCmd(ENABLE);	//使能RTC和后备寄存器访问 
		BKP_DeInit();	//将外设BKP的全部寄存器重设为缺省值 	
		RCC_LSEConfig(RCC_LSE_ON);	//设置外部低速晶振(LSE),使用外设低速晶振
		while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)	//检查指定的RCC标志位设置与否,等待低速晶振就绪
			{
			temp++;
			delay_ms(10);
			}
		if(temp>=250)return 1;//初始化时钟失败,晶振有问题	    
		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);		//设置RTC时钟(RTCCLK),选择LSE作为RTC时钟    
		RCC_RTCCLKCmd(ENABLE);	//使能RTC时钟  
		RTC_WaitForSynchro();		//等待最近一次对RTC寄存器的写操作完成
		RTC_WaitForLastTask();	//等待最近一次对RTC寄存器的写操作完成
		RTC_ITConfig(RTC_IT_SEC, ENABLE);		//使能RTC秒中断
		RTC_WaitForLastTask();	//等待最近一次对RTC寄存器的写操作完成
		RTC_SetPrescaler(32767); //设置RTC预分频的值
		RTC_WaitForLastTask();	//等待最近一次对RTC寄存器的写操作完成
		RTC_Set(2009,12,2,10,0,55);  //设置时间	  
		BKP_WriteBackupRegister(BKP_DR1, 0X5050);	//向指定的后备寄存器中写入用户程序数据
		}
	else//系统继续计时
		{

		RTC_WaitForSynchro();	//等待最近一次对RTC寄存器的写操作完成
		RTC_ITConfig(RTC_IT_SEC, ENABLE);	//使能RTC秒中断
		RTC_WaitForLastTask();	//等待最近一次对RTC寄存器的写操作完成
		}		    				     
	RTC_Get();//更新时间	
	RCC_ClearFlag();	//清除RCC的复位标志位
	return 0; //ok
	}
示例#26
0
void RTC_Init()
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  PWR_BackupAccessCmd(ENABLE);
  
  //采用HSE/128作为时钟频率
  RCC_HSEConfig(RCC_HSE_ON);
  RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  RCC_RTCCLKCmd(ENABLE);
  
  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();
  
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  
  /* RTC period = RTCCLK/RTC_PR = (8MHz/128)/(62499+1) = 1  (HSE)*/
  RTC_SetPrescaler(62499);
  
  /* 等待上一次对RTC寄存器的写操作完成 */
  RTC_WaitForLastTask();
}
示例#27
0
int main(void)
{
	uint8_t system_state=0, i2c_resets=0, si446x_resets=0;//used to track button press functionality and any errors
	uint8_t sensors=0;
	uint32_t repetition_counter=0;			//Used to detect any I2C lockup
	uint8_t L3GD20_Data_Buffer_old[8];		//Used to test for noise in the gyro data (indicating that it is working)
	uint8_t UplinkFlags=0,CutFlags=0;
	uint16_t UplinkBytes=0;				//Counters and flags for telemetry
	uint32_t last_telemetry=0,cutofftime=0,indtest=0,badgyro=0,permission_time=0,countdown_time=0,last_cuttest=0;
	uint16_t sentence_counter=0;
	uint8_t silab;
	//Cutdown config stuff here, atm uses hardcoded polygon defined in polygon.h
	static const int32_t Geofence[UK_GEOFENCE_POINTS*2]=UK_GEOFENCE;
	RTC_t RTC_time;
        _REENT_INIT_PTR(&my_reent);
        _impure_ptr = &my_reent;
	SystemInit();					//Sets up the clk
	setup_gpio();					//Initialised pins, and detects boot source
	DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE);	//Watchdog stopped during JTAG halt
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);/* Enable PWR and BKP clocks */
	PWR_BackupAccessCmd(ENABLE);/* Allow access to BKP Domain */
	uint16_t shutdown_lock=BKP_ReadBackupRegister(BKP_DR3);	//Holds the shutdown lock setting
	uint16_t reset_counter=BKP_ReadBackupRegister(BKP_DR2); //The number of consecutive failed reboot cycles
	PWR_BackupAccessCmd(DISABLE);
	if(RCC->CSR&RCC_CSR_IWDGRSTF && shutdown_lock!=SHUTDOWNLOCK_MAGIC) {//Watchdog reset, turn off
		RCC->CSR|=RCC_CSR_RMVF;			//Reset the reset flags
		shutdown();
	}
	if(USB_SOURCE==bootsource) {
		RCC->CFGR &= ~(uint32_t)RCC_CFGR_PPRE1_DIV16;
		RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV4;//Swap the ABP1 bus to run at 12mhz rather than 4 if we booted from USB, makes USB fast enough
	}
	SysTick_Configuration();			//Start up system timer at 100Hz for uSD card functionality
	Watchdog_Config(WATCHDOG_TIMEOUT);		//Set the watchdog
	Watchdog_Reset();				//Reset watchdog as soon as possible incase it is still running at power on
	rtc_init();					//Real time clock initialise - (keeps time unchanged if set)
	Usarts_Init();
	ISR_Config();
	rprintfInit(__usart_send_char);			//Printf over the bluetooth
	if(USB_SOURCE==bootsource) {
		Set_System();				//This actually just inits the storage layer
		Set_USBClock();
		USB_Interrupts_Config();
		USB_Init();
		uint32_t nojack=0x000FFFFF;		//Countdown timer - a few hundered ms of 0v on jack detect forces a shutdown
		while (bDeviceState != CONFIGURED) {	//Wait for USB config - timeout causes shutdown
			if((Millis>10000 && bDeviceState == UNCONNECTED)|| !nojack)	//No USB cable - shutdown (Charger pin will be set to open drain, cant be disabled without usb)
				shutdown();
			if(GET_VBUS_STATE)		//Jack detect resets the countdown
				nojack=0x0FFFFF;
			nojack--;
			Watchdog_Reset();		//Reset watchdog here, if we are stalled here the Millis timeout should catch us
		}
		PWR_BackupAccessCmd(ENABLE);		/* Allow access to BKP Domain */
		BKP_WriteBackupRegister(BKP_DR3,0x0000);//Wipe the shutdown lock setting
		PWR_BackupAccessCmd(DISABLE);
		while(1) {
			if(!(Millis%1000) && bDeviceState == SUSPENDED) {
				Delay(100);
				if(!GET_VBUS_STATE)
					shutdown();
			}
			Watchdog_Reset();
			__WFI();			//Sleep mode
		}
	}
        if(!GET_PWR_STATE && !(CoreDebug->DHCSR&0x00000001) && shutdown_lock!=SHUTDOWNLOCK_MAGIC) {//Check here to make sure the power button is still pressed, if not, sleep if no debug and not in always on flight mode
                shutdown();                             //This means a glitch on the supply line, or a power glitch results in sleep
        }
	// check to see if battery has enough charge to start
	ADC_Configuration();				//We leave this a bit later to allow stabilisation
	{
	uint32_t t=Millis;
	while(Millis<(t+100)){__WFI();}			//Sensor+inst amplifier takes about 100ms to stabilise after power on
	}
	if(Battery_Voltage<BATTERY_STARTUP_LIMIT) {	//We will have to turn off
		if(reset_counter<10)
			shutdown();
	}
	Watchdog_Reset();				//Card Init can take a second or two
	// system has passed battery level check and so file can be opened
	{//Context
	uint8_t silabs_header[5]={};
	uint8_t* silabs_header_=NULL;			//Pointer to the array (if all goes ok)
	if((f_err_code = f_mount(0, &FATFS_Obj)))Usart_Send_Str((char*)"FatFs mount error\r\n");//This should only error if internal error
	else {						//FATFS initialised ok, try init the card, this also sets up the SPI1
		if(!(f_err_code=f_open(&FATFS_logfile,(const TCHAR*)"time.txt",FA_OPEN_EXISTING|FA_READ|FA_WRITE))){//Try to open time file get system time
			if(!f_stat((const TCHAR *)"time.txt",&FATFS_info)) {//Get file info
				if(FATFS_info.fsize<5) {	//Empty file
					RTC_time.year=(FATFS_info.fdate>>9)+1980;//populate the time struct (FAT start==1980, RTC.year==0)
					RTC_time.month=(FATFS_info.fdate>>5)&0x000F;
					RTC_time.mday=FATFS_info.fdate&0x001F;
					RTC_time.hour=(FATFS_info.ftime>>11)&0x001F;
					RTC_time.min=(FATFS_info.ftime>>5)&0x003F;
					RTC_time.sec=(FATFS_info.ftime<<1)&0x003E;
					rtc_settime(&RTC_time);
					rprintfInit(__fat_print_char);//printf to the open file
					printf("RTC set to %d/%d/%d %d:%d:%d\n",RTC_time.mday,RTC_time.month,RTC_time.year,\
					RTC_time.hour,RTC_time.min,RTC_time.sec);
				}				
			}
			f_close(&FATFS_logfile);	//Close the time.txt file
		}
		// load settings if file exists
		Watchdog_Reset();			//Card Init can take a second or two
		if(!f_open(&FATFS_logfile,(const TCHAR *)"settings.dat",FA_OPEN_EXISTING | FA_READ)) {
			UINT br;
			int8_t rtc_correction;
			f_read(&FATFS_logfile, (void*)(&rtc_correction),sizeof(rtc_correction),&br);
			//Use the setting to apply correction to the RTC
                        if(br && (rtc_correction<30) && (rtc_correction>-92) && rtc_correction ) {
                                PWR_BackupAccessCmd(ENABLE);/* Allow access to BKP Domain */
                                uint16_t tweaked_prescale = (0x0001<<15)-2;/* Try to run the RTC slightly too fast so it can be corrected either way */
                                RTC_WaitForSynchro();   /* Wait for RTC registers synchronization */
                                if( RTC->PRLL != tweaked_prescale ) {/*Note that there is a 0.5ppm offset here (correction 0==0.5ppm slow)*/
                                        RTC_SetPrescaler(tweaked_prescale); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767-2+1) */
                                        RTC_WaitForLastTask();
                                }
                                BKP_SetRTCCalibrationValue((uint8_t) ((int16_t)31-(21*(int16_t)rtc_correction)/(int16_t)20) );
                                BKP_RTCOutputConfig(BKP_RTCOutputSource_None);/* Ensure any output is disabled here */
                                /* Lock access to BKP Domain */
                                PWR_BackupAccessCmd(DISABLE);
                        }
                        else if(br && ((uint8_t)rtc_correction==0x91) ) {/* 0x91 magic flag sets the RTC clock output on */
                                PWR_BackupAccessCmd(ENABLE);/* Allow access to BKP Domain */
                                BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);/* Output a 512Hz reference clock on the TAMPER pin*/
                                PWR_BackupAccessCmd(DISABLE);
                        }
			if(br) {
				f_read(&FATFS_logfile, (void*)(&shutdown_lock),sizeof(shutdown_lock),&br);/*This needs to be set with the same magic flag value*/
				if(br==2) {
                                	PWR_BackupAccessCmd(ENABLE);/* Allow access to BKP Domain */
					BKP_WriteBackupRegister(BKP_DR3,shutdown_lock);//Wipe the shutdown lock setting
                               		PWR_BackupAccessCmd(DISABLE);
				}
			}
			if(br==2) {			//Read was successful, next try to read 5 bytes of packet header
				f_read(&FATFS_logfile, (void*)(silabs_header),5,&br);
				if(br!=5)
					silabs_header_=silabs_header;
			}
			f_close(&FATFS_logfile);	//Close the settings.dat file
		}
#ifndef SINGLE_LOGFILE
		rtc_gettime(&RTC_time);			//Get the RTC time and put a timestamp on the start of the file
		rprintfInit(__str_print_char);		//Print to the string
		printf("%02d-%02d-%02dT%02d-%02d-%02d-%s.csv",RTC_time.year,RTC_time.month,RTC_time.mday,RTC_time.hour,RTC_time.min,RTC_time.sec,"Log");//Timestamp name
		rprintfInit(__usart_send_char);		//Printf over the bluetooth
#endif
		Watchdog_Reset();			//Card Init can take a second or two
		if((f_err_code=f_open(&FATFS_logfile,(TCHAR*)LOGFILE_NAME,FA_CREATE_ALWAYS | FA_WRITE))) {//Present
			Delay(10000);
			if((f_err_code=f_open(&FATFS_logfile,(TCHAR*)LOGFILE_NAME,FA_CREATE_ALWAYS | FA_WRITE))) {//Try again
				printf("FatFs drive error %d\r\n",f_err_code);
				if(f_err_code==FR_DISK_ERR || f_err_code==FR_NOT_READY)
					Usart_Send_Str((char*)"No uSD card inserted?\r\n");
			}
		}
		else {
			Watchdog_Reset();		//Card Init can take a second or two
			print_string[strlen(print_string)-4]=0x00;//Wipe the .csv off the string
			strcat(print_string,"_gyro.wav");
			if((f_err_code=f_open(&FATFS_wavfile_gyro,(TCHAR*)LOGFILE_NAME,FA_CREATE_ALWAYS | FA_WRITE))) {//Present
				printf("FatFs drive error %d\r\n",f_err_code);
				if(f_err_code==FR_DISK_ERR || f_err_code==FR_NOT_READY)
					Usart_Send_Str((char*)"No uSD card inserted?\r\n");
			}
			else {					//We have a mounted card
				f_err_code=f_lseek(&FATFS_logfile, PRE_SIZE);// Pre-allocate clusters
				if (f_err_code || f_tell(&FATFS_logfile) != PRE_SIZE)// Check if the file size has been increased correctly
					Usart_Send_Str((char*)"Pre-Allocation error\r\n");
				else {
					if((f_err_code=f_lseek(&FATFS_logfile, 0)))//Seek back to start of file to start writing
						Usart_Send_Str((char*)"Seek error\r\n");
					else
						rprintfInit(__str_print_char);//Printf to the logfile
				}
				if(f_err_code)
					f_close(&FATFS_logfile);//Close the already opened file on error
				else
					file_opened=1;		//So we know to close the file properly on shutdown
				if(file_opened==1) {
					Watchdog_Reset();	//Card Init can take a second or two
					if (f_err_code || f_tell(&FATFS_wavfile_gyro) != PRE_SIZE)// Check if the file size has been increased correctly
						Usart_Send_Str((char*)"Pre-Allocation error\r\n");
					else {
						if((f_err_code=f_lseek(&FATFS_logfile, 0)))//Seek back to start of file to start writing
							Usart_Send_Str((char*)"Seek error\r\n");
						else
							rprintfInit(__str_print_char);//Printf to the logfile
					}
					if(f_err_code)
						f_close(&FATFS_wavfile_gyro);//Close the already opened file on error
					else
						file_opened|=2;	//So we know to close the file properly on shutdown
				}
			}
		}
	}
	f_err_code|=write_wave_header(&FATFS_wavfile_gyro, 4, 100, 16);//4 channels, last channel is for the current rpm
	Watchdog_Reset();				//Card Init can take a second or two
	//Setup and test the silabs radio
	silab=si446x_setup(silabs_header_);
	if(silab!=0x44) {				//Should return the device code
		print_string[0]=0x00;
		printf("Silabs: %02x\n",silab);
		f_puts("Silabs detect error, got:",&FATFS_logfile);
		f_puts(print_string,&FATFS_logfile);
		shutdown_filesystem(ERR, file_opened);//So we log that something went wrong in the logfile
		shutdown();
	}
	}//Context
//extern void flash_write_time_volitage(u8 addr_index,uint16_t volitage,u8 year_H,u8 year_L,u8 month_H,u8 month_L,u8 day_H,u8 day_L,u8 hh_H,u8 hh_L,u8 mm_H,u8 mm_L,u8 ss_H,u8 ss_L ) ;
void operating_12864_system (void)    //按键操作和界面切换
{ 
	 u16 u16_WaitForOscSource,startv=0;
    
	if(rcc_flag==0)
	{
		if(k1==key_ok)
		{
			k1=key_none;
			rcc_flag=1;
			DebugMenu(1);
		}
		if(k1==key_cancel)
		{
			k1=key_none;
			flag_i=1;
			voltage=0;
      Alarm_flag_1=0;
      BUZZER_off;
      Fault_led_off;
      Relay_off;
// 			buzzer_tick_flag=1;
		}
	}
	if(rcc_flag==1)
	{
// 		Delay_ms(1000);
	// 		flag_system=1;
		if(flag_debug==0)
		{
			flag_debug=1;
		}
			switch (k1)
			{
				case key_down:
							k1=key_none;
						 flag_debug++; 
						 if(flag_debug>4)
							{
								flag_debug=1;
							}	
							DebugMenu(flag_debug);
             //	Delay_ms(20);
              						
				break;		
				case key_up:
						 k1=key_none;
						 flag_debug--;
						 if(flag_debug==0)
							{
								flag_debug=4;
							}
							DebugMenu(flag_debug);
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						 rcc_flag=0;
							flag_debug=1;
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							k1=key_none;
							
							if(flag_debug==1)
							{
								rcc_flag=21;
								flag_debug=1;
								SecondSet(flag_debug);
							}
							if(flag_debug==2)
							{
								rcc_flag=22;
								flag_debug=1;
								SecondSystemDebug(flag_debug);
							}
							if(flag_debug==3)
							{
								rcc_flag=23;
                fault_num=flash_num<0?0:flash_num;
//                 if(flash_num<0)
//                 {
//                   fault_num=0;
//                 }
//                 else{
//                   fault_num=flash_num;
//                 }
								flash_read_time_volitage(start_addr+fault_num*14);
								Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L);
//                 flash_read_time_volitage(start_addr+14*fault_num);
//                 Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L);
                
							}
							if(flag_debug==4)
							{
								rcc_flag=24;
								Second_clear_fault_records();
							}
				break;
			}
		}
		if(rcc_flag==21)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
							k1=key_none;
						 flag_debug++; 
						 if(flag_debug>2)
							{
								flag_debug=1;
							}	
							SecondSet(flag_debug);
             //	Delay_ms(20);
              						
				break;		
				case key_up:
						 k1=key_none;
						 flag_debug--;
						 if(flag_debug==0)
							{
								flag_debug=2;
							}
							SecondSet(flag_debug);
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						flag_debug=0;
						 rcc_flag=1;
						DebugMenu(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							k1=key_none;
							
					if(flag_debug==1)
					{
							rcc_flag=31;
							ThirdSystemSetting(1);
					}
					if(flag_debug==2)
					{
             flag_debug=1;
							rcc_flag=32;
							ThirdBaudRateSetting(flag_debug,Device_Add,Band_Rate,Data_bits,Stop_bits);
					}
							
				break;
			}	
		}
		if(rcc_flag==22)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
							k1=key_none;
						 flag_debug++; 
						 if(flag_debug>4)
							{
								flag_debug=1;
							}	
							SecondSystemDebug(flag_debug);
             //	Delay_ms(20);
              						
				break;		
				case key_up:
						 k1=key_none;
						 flag_debug--;
						 if(flag_debug==0)
							{
								flag_debug=4;
							}
							SecondSystemDebug(flag_debug);
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						flag_debug=1;
						 rcc_flag=1;
				   //  Delay_ms(20);
				     k1=key_none;
				     DebugMenu(1);
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							k1=key_none;
						if(flag_debug==1)
						{
							flag_debug=1;
							rcc_flag=33;
							ThirdChange();
						}
						if(flag_debug==2)
						{
							flag_debug=1;
							rcc_flag=34;
							ThirdCorrect();
						}
						if(flag_debug==3)
						{
							flag_debug=1;
							rcc_flag=35;
							ThirdCheck();
						}
						if(flag_debug==4)
						{
							flag_debug=1;
							rcc_flag=36;
							ThirdReguration(1,1,1,1);
						}
							
				break;
			}	
		}
		if(rcc_flag==23)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down: 
            fault_num++;
            if(fault_num>=50)
            {
              fault_num=0;
            }
            flash_read_time_volitage(start_addr+14*fault_num);
						Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L);
//                 fault_num=flash_num; 
            
            
            k1=key_none;
				break;		
				case key_up:
            fault_num--;
            if(fault_num<0)
            {
              fault_num=49;
            }
					 flash_read_time_volitage(start_addr+14*fault_num);
					 Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L);
//                 fault_num=flash_num; 
            
            
            k1=key_none;    							
				break;
				case key_cancel:
						 rcc_flag=1;
				   //  Delay_ms(20);
							flag_debug=1;
				     k1=key_none;
						DebugMenu(1);
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							k1=key_none;
						//	rcc_flag=2;
							
							
				break;
			}	
		}
		if(rcc_flag==24)
		{
			//flag_debug=1;
				switch (k1)
			{
				
				case key_cancel:
						 rcc_flag=1;
							flag_debug=1;
				   //  Delay_ms(20);
				     k1=key_none;
						DebugMenu(1);
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							k1=key_none;
//               fi++;
							rcc_flag=2;
                //flash_cache[300]=0;
//  							for(fi=0;fi<255;fi++)
//               {
//                 flash_cache[fi]=0;
//               }

//               Delay_ms(100);
//               flash_write_time_volitage(0,0,0,0,0,0,0,0,0,0,0,0,0,0 ) ;
//               Delay_ms(500);
//               flash_write(0xffff,flash_num_addr);
							
				break;
			}	
		}
		if(rcc_flag==31)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
						 k1=key_none;
						 flag_debug++; 
						 if(flag_debug>2)
							{
								flag_debug=1;
							}	
							ThirdSystemSetting(flag_debug);
             //	Delay_ms(20);
              						
				break;		
				case key_up:
						 k1=key_none;
						 flag_debug--;
						 if(flag_debug==0)
							{
								flag_debug=2;
							}
							ThirdSystemSetting(flag_debug);
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						 	flag_debug=1;
						 rcc_flag=21;
						 SecondSet(1);
				   //  Delay_ms(20);
				     k1=key_none;

				timer.sec=ss_H*10+ss_L;
				timer.min=mm_H*10+mm_L;
				timer.hour=hh_H*10+hh_L;
				timer.w_date=day_H*10+day_L;
				timer.w_month=month_H*10+month_L;
				timer.w_year=(year_H*10+year_L)+2000;
				
				RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
 
      /* Allow access to BKP Domain */
      PWR_BackupAccessCmd(ENABLE);
 
      /* Reset Backup Domain */
       BKP_DeInit();
 
      /* Enable LSE */
      RCC_LSEConfig(RCC_LSE_ON);
       for(u16_WaitForOscSource=0;u16_WaitForOscSource<5000;u16_WaitForOscSource++)
           {
           }
      /* Wait till LSE is ready */
      while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
 
      /* Select LSE as RTC Clock Source */
      RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
 
      /* Enable RTC Clock */
      RCC_RTCCLKCmd(ENABLE);
 
      /* Wait for RTC registers synchronization */
      RTC_WaitForSynchro();
 
      /* Wait until last write operation on RTC registers has finished */
      RTC_WaitForLastTask();
 
      /* Enable the RTC Second */
      RTC_ITConfig(RTC_IT_SEC, ENABLE);
 
      /* Wait until last write operation on RTC registers has finished */
       RTC_WaitForLastTask();
 
      /* Set RTC prescaler: set RTC period to 1sec */
      RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
 
      /* Wait until last write operation on RTC registers has finished */
      RTC_WaitForLastTask();
        BKP_WriteBackupRegister(BKP_DR1, 0x5A5A);
        RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec);
				break;
				case key_ok:
							k1=key_none;
							
					if(flag_debug==1)
					{
						  flag_debug=1;
							rcc_flag=41;
							FourStart(start_v);
					}
					if(flag_debug==2)
					{
						  flag_debug=1;
							rcc_flag=42;
							FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);	
					}
							
				break;
			}	
		}
		if(rcc_flag==32)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
						 k1=key_none;
						 flag_debug++; 
						 if(flag_debug>4)
							{
								flag_debug=1;
							}	
							ThirdBaudRateSetting(flag_debug,Device_Add,Band_Rate,Data_bits,Stop_bits);
             //	Delay_ms(20);
              						
				break;		
				case key_up:
						 k1=key_none;
						 flag_debug--;
						 if(flag_debug==0)
							{
								flag_debug=4;
							}
							ThirdBaudRateSetting(flag_debug,Device_Add,Band_Rate,Data_bits,Stop_bits);
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						 flag_debug=1;
						 rcc_flag=21;
						 SecondSet(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
						 k1=key_none;	
					if(flag_debug==1)
					{
// 						  flag_debug=1;
							rcc_flag=43;	
					} 
          else if(flag_debug==2)
					{
// 						  flag_debug=1;
							rcc_flag=44;
					}
           else if(flag_debug==3)
					{
// 						  flag_debug=1;
							rcc_flag=45;
					}
          else if(flag_debug==4)
					{
// 						  flag_debug=1;
							rcc_flag=46;
					}
							
				break;
			}	
		}
		if(rcc_flag==33)
		{
			//flag_debug=1;
				switch (k1)
			{
				
				case key_cancel:
						 	flag_debug=1;
						 rcc_flag=22;
						 SecondSystemDebug(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							
							
				break;
			}	
		}
		if(rcc_flag==34)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
						
              						
				break;		
				case key_up:
						
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						 	flag_debug=1;
						 rcc_flag=22;
						 SecondSystemDebug(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							
							
				break;
			}	
		}
		if(rcc_flag==35)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
						
              						
				break;		
				case key_up:
						
           //   Delay_ms(20);
              							
				break;
				case key_cancel:
						 	flag_debug=1;
						 rcc_flag=22;
						 SecondSystemDebug(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
							
							
				break;
			}	
		}
		if(rcc_flag==36)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
						 k1=key_none;
						 flag_debug++; 
						 if(flag_debug>3)
							{
								flag_debug=1;
							}	
							ThirdReguration(flag_debug,flag_Led,flag_Relay,flag_Buzzer);							
				break;		
				case key_up:
						 k1=key_none;
						 flag_debug--;
						 if(flag_debug==0)
							{
								flag_debug=3;
							}
							ThirdReguration(flag_debug,flag_Led,flag_Relay,flag_Buzzer);								
				break;
				case key_cancel:
						 	flag_debug=1;
						 rcc_flag=22;
						 SecondSystemDebug(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
					   k1=key_none;	
						 if(flag_debug==1)              
					  {					  
							++flag_Led;
							if(flag_Led<1)
							{
								flag_Led=2;
              }
							else if(flag_Led>2)
							{
								flag_Led=1;
              }
						ThirdReguration(1,flag_Led,flag_Relay,flag_Buzzer);	
					  }
						if(flag_debug==2)
						{
								++flag_Relay;
							if(flag_Relay<1)
							{
								flag_Relay=2;
              }
							else if(flag_Relay>2)
							{
								flag_Relay=1;
              }
						ThirdReguration(2,flag_Led,flag_Relay,flag_Buzzer);
						}
						if(flag_debug==3)
						{
								++flag_Buzzer;
							if(flag_Buzzer<1)
							{
								flag_Buzzer=2;
              }
							else if(flag_Buzzer>2)
							{
								flag_Buzzer=1;
              }
						ThirdReguration(3,flag_Led,flag_Relay,flag_Buzzer);
						}							
				break;
			}	
		}
    if(rcc_flag==41)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
							 
              if(start_v<=0)
							{
								start_v=0;
							}
				      start_v-=5;
							 FourStart(start_v);
             //	Delay_ms(20);
				      k1=key_none;
              						
				break;		
				case key_up:
							start_v+=5;
							 FourStart(start_v);
           //   Delay_ms(20);
				     k1=key_none;
              							
				break;
				case key_cancel:
              startv=start_v;
						 flag_debug=1;
						 rcc_flag=31;
						 ThirdSystemSetting(1);
				     k1=key_none;
            // flash_write_start_v(start_v);
            flash_write(startv,start_v_addr);
				break;
				case key_ok:
							
							
				break;
			}	
		}
		if(rcc_flag==42)
		{
			//flag_debug=1;
				switch (k1)
			{
				case key_down:
              						
				break;		
				case key_up:
						
              							
				break;
				case key_cancel:
						 	flag_debug=1;
						 rcc_flag=31;
						 ThirdSystemSetting(1);
				   //  Delay_ms(20);
				     k1=key_none;
							//DebugMenu(flag_debug);
				break;
				case key_ok:
					   ++flagTime;
				     if(flagTime > 12)
				     {
				      flagTime = 1;
				     }
              k1=key_none;						 
				break;
			}	
		}
		/*设置时间*/
		       if(flagTime==1)
					 {
						 switch (k1)
			      {
				     case key_down:
              	  --ss_L;
					    	if(ss_L<0)
					        {
					        	ss_L = 9;
					    	  }	
                  FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);	
                  k1=key_none;									
				     break;		
				     case key_up:
						      ++ss_L;
					    	if(ss_L>9)
					        {
					        	ss_L = 0;
					    	  }
                  FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
                  k1=key_none;									
				     break;
            }
					}
					 else if(flagTime==2)
					 {
            	switch (k1)
			      {
				     case key_down:
                  --ss_H;
					    	if(ss_H<0)
					        {
					        	ss_H = 6;
					    	  }	
                  FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);	
                  k1=key_none;										
				     break;		
				     case key_up:
						      ++ss_H;
					    	if(ss_H>6)
					        {
					        	ss_H = 0;
					    	  }
									FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              		 k1=key_none;				
				     break;
            }
           }
					 else if(flagTime==3)
					 {
            	switch (k1)
			      {
				     case key_down:
							    --mm_L;
					    	  if(mm_L<0)
					        {
					        	mm_L = 9;
					    	  }
									FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              		k1=key_none;					
				     break;		
				     case key_up:
						      ++mm_L;
					    	  if(mm_L>9)
					        {
					        	mm_L = 0;
					    	  }
									FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;							
				     break;
            }
           }
					 else if(flagTime==4)
					 {
            	switch (k1)
			      {
				     case key_down:
							    --mm_H;
					    	  if(mm_H<0)
					        {
					        	mm_H = 6;
					    	  }
									FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              		k1=key_none;					
				     break;		
				     case key_up:
						      ++mm_H;
					    	  if(mm_H>6)
					        {
					        	mm_H = 0;
					    	  }
									FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;				
				     break;
            }
           }
				   else if(flagTime==5)
					 {
            	switch (k1)
			      {
				     case key_down:
							    --hh_L;
					    	  if(hh_L<0)
					         {
					        	 hh_L = 9;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;			
				     break;		
				     case key_up:
						      ++hh_L;
					    	  if(hh_L>9)
					         {
					        	 hh_L = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;						
				     break;
            }
           }
					 else if(flagTime==6)
					 {
            	switch (k1)
			      {
				     case key_down:
              		--hh_H;
					    	  if(hh_H<0)
					         {
					        	 hh_H = 2;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;			
				     break;		
				     case key_up:
						      ++hh_H;
					    	  if(hh_H>2)
					         {
					        	 hh_H = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;
              							
				     break;
            }
           }
					 else if(flagTime==7)
					 {
            	switch (k1)
			      {
				     case key_down:
              			--day_L;
					    	  if(day_L<0)
					         {
					        	 day_L = 9;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;			
				     break;		
				     case key_up:
						      ++day_L;
					    	  if(day_L>9)
					         {
					        	 day_L = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;
              							
				     break;
            }
           }
					 else if(flagTime==8)
					 {
            	switch (k1)
			      {
				     case key_down:
              		--day_H;
					    	  if(day_H<0)
					         {
					        	 day_H = 3;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;			
				     break;		
				     case key_up:
						      ++day_H;
					    	  if(day_H>3)
					         {
					        	 day_H = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;
              							
				     break;
            }
           }
					 else if(flagTime==9)
					 {
            	switch (k1)
			      {
				     case key_down:
              		--month_L;
					    	  if(month_L<0)
					         {
					        	 month_L = 9;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;				
				     break;		
				     case key_up:
						      ++month_L;
					    	  if(month_L>9)
					         {
					        	 month_L = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;					
				     break;
            }
           }
					 else if(flagTime==10)
					 {
            	switch (k1)
			      {
				     case key_down:
              		--month_H;
					    	  if(month_H<0)
					         {
					        	 month_H = 1;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;				
				     break;		
				     case key_up:
						      ++month_H;
					    	  if(month_H>1)
					         {
					        	 month_H = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;
              							
				     break;
            }
           }
					 
					 else if(flagTime==11)
					 {
            	switch (k1)
			      {
				     case key_down:
							    --year_L;
					    	  if(year_L<0)
					         {
					        	 year_L = 9;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;	
              						
				     break;		
				     case key_up:
						       ++year_L;
					    	  if(year_L>9)
					         {
					        	 year_L = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;
              							
				     break;
            }
           }
					 else if(flagTime==12)
					 {
            	switch (k1)
			      {
				     case key_down:
              		--year_H;
					    	  if(year_H<0)
					         {
					        	 year_H = 9;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;				
				     break;		
				     case key_up:
						       ++year_H;
					    	  if(year_H>9)
					         {
					        	 year_H = 0;
					    	   }
              		FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L);
              	  k1=key_none;
              							
				     break;
            }
           }
					if(rcc_flag==43)                 //设置装置号
					{
						switch (k1)
			      {
				     case key_down: 
                  --Device_Add;
					    	if(Device_Add<1)
					        {
					        	Device_Add = 255;
					    	  }	
             ThirdBaudRateSetting(1,Device_Add,Band_Rate,Data_bits,Stop_bits);
                  k1=key_none;							
				     break;		
				     case key_up:
						      ++Device_Add;
					    	  if(Device_Add>255)
					         {
					        	Device_Add = 1;
					    	   }	
             ThirdBaudRateSetting(1,Device_Add,Band_Rate,Data_bits,Stop_bits);	
                   k1=key_none;                     
				     break;		 
						case key_cancel:                                //设置装置后返回
									 flag_debug=1;
									 rcc_flag=21;
									 SecondSet(1);
                 //  flash_write_device_b(Device_Add);	
                  flash_write(Device_Add,device_id_addr);
									 k1=key_none;
				    break;
            }
          }
					
					if(rcc_flag==44)                 //设置波特率
					{
						switch (k1)
			      {
				     case key_down: 
						     k1=key_none;
                  --flag_debug;
					    	if(flag_debug<1)
					        {
					        	flag_debug = 4;
					    	  }	
									switch(flag_debug)
									{
										case 1:
											    Band_Rate=4800;
										break;
										case 2:
											    Band_Rate=9600;
										break;
										case 3:
											    Band_Rate=19200;
										break;
										case 4:
											    Band_Rate=115200;
										break;
                  }
             ThirdBaudRateSetting(2,Device_Add,Band_Rate,Data_bits,Stop_bits);									
				     break;		
				     case key_up:
							     k1=key_none;
						      k1=key_none;
                  ++flag_debug;
					    	if(flag_debug>4)
					        {
					        	flag_debug = 0;
					    	  }	
									switch(flag_debug)
									{
										case 1:
											    Band_Rate=4800;
										break;
										case 2:
											    Band_Rate=9600;
										break;
										case 3:
											    Band_Rate=19200;
										break;
										case 4:
											    Band_Rate=115200;
										break;
                  }
             ThirdBaudRateSetting(2,Device_Add,Band_Rate,Data_bits,Stop_bits);								
				     break;		 
						case key_cancel:                                //设置装置后返回
									 flag_debug=1;
									 rcc_flag=21;
									 SecondSet(1);
									 k1=key_none;
                   uart_init(Band_Rate);
                   RS485_Init(Band_Rate);       //波特率设置  485
                  // flash_write_Band_Rate(Band_Rate);
                  flash_write(Band_Rate,Band_Rate_addr);
				    break;
            }
          }
          
          if(rcc_flag==45)                                 //设置数据位
					{
						switch (k1)
			      {
				     case key_down: 
                  --Data_bits;
					    	if(Data_bits<5)
					        {
					        	Data_bits = 8;
					    	  }	
             ThirdBaudRateSetting(3,Device_Add,Band_Rate,Data_bits,Stop_bits);
                  k1=key_none;							
				     break;		
				     case key_up:
						      ++Data_bits;
					    	  if(Data_bits>9)
					         {
					        	Data_bits = 5;
					    	   }	
             ThirdBaudRateSetting(3,Device_Add,Band_Rate,Data_bits,Stop_bits);	
                   k1=key_none;                     
				     break;		 
						case key_cancel:                                //设置停止位
									 flag_debug=1;
									 rcc_flag=21;
									 SecondSet(1);
//                    flash_write_device_b(Device_Add);	
//                  flash_write_databits(Data_bits);
                  flash_write(Data_bits,databits_addr);
									 k1=key_none;
				    break;
            }
          }
          
					if(rcc_flag==46)                                 //设置停止位
					{
						switch (k1)
			      {
				     case key_down: 
                  --Stop_bits;
					    	if(Stop_bits<1)
					        {
					        	Stop_bits = 9;
					    	  }	
             ThirdBaudRateSetting(4,Device_Add,Band_Rate,Data_bits,Stop_bits);
                  k1=key_none;							
				     break;		
				     case key_up:
						      ++Stop_bits;
					    	  if(Stop_bits>10)
					         {
					        	Stop_bits = 1;
					    	   }	
             ThirdBaudRateSetting(4,Device_Add,Band_Rate,Data_bits,Stop_bits);	
                   k1=key_none;                     
				     break;		 
						case key_cancel:                                //设置停止位
									 flag_debug=1;
									 rcc_flag=21;
									 SecondSet(1);
//                   flash_write_stopbits(Stop_bits);
                   flash_write(Stop_bits,stopbits_addr);
									 k1=key_none;
				    break;
            }
          }

}
示例#29
0
/**
  * @brief  Configures the RTC.
  * @param  None
  * @retval None
  */
void RTC_Configuration(void) {
  NVIC_InitTypeDef NVIC_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;
  
  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* 2 bits for Preemption Priority and 2 bits for Sub Priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0xFF;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Configure EXTI Line17 (RTC Alarm)to generate an interrupt on rising edge */
  EXTI_ClearITPendingBit(EXTI_Line17);
  EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_Init(&EXTI_InitStructure);
  
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Reset Backup Domain */
  BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  
  /* Alarm in 3 second */
  //RTC_SetAlarm(3);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  
  /* Enable the RTC Second, RTC Alarm interrupt */
  RTC_ITConfig(RTC_IT_SEC || RTC_IT_ALR, ENABLE);

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}
示例#30
0
/**
 * @brief  COnfiguration of RTC Registers, Selection and Enabling of 
 *   RTC clock
 * @param  None
 * @retval : None
 */
void RTC_Configuration()
{
	uint16_t WaitForOscSource;

	/*Allow access to Backup Registers*/
	PWR_BackupAccessCmd(ENABLE);

	BKP_RTCOutputConfig(BKP_RTCOutputSource_None);

	if(READ_BKP_CONFIGURATION() != CONFIGURATION_DONE)
	{
		/*Enables the clock to Backup and power interface peripherals    */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR, ENABLE);

		/* Backup Domain Reset */
		BKP_DeInit();

		set_time(&s_DateStructVar);	 
		set_alarm(&s_AlarmDateStructVar);

		//EE_Format();

		/*Enable 32.768 kHz external oscillator */
		RCC_LSEConfig(RCC_LSE_ON);
		for(WaitForOscSource = 0; WaitForOscSource < 5000; WaitForOscSource++);   

		RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

		/* RTC Enabled */
		RCC_RTCCLKCmd(ENABLE);
		RTC_WaitForLastTask();

		/*Wait for RTC registers synchronisation */
		RTC_WaitForSynchro();
		RTC_WaitForLastTask();

		/* Setting RTC Interrupts-Seconds interrupt enabled */
		/* Enable the RTC Second */
		RTC_ITConfig(RTC_IT_SEC , ENABLE);
		RTC_WaitForLastTask();

		/* Set RTC prescaler: set RTC period to 1 sec */
		RTC_SetPrescaler(32765); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
		/* Prescaler is set to 32766 instead of 32768 to compensate for
			lower as well as higher frequencies*/
		RTC_WaitForLastTask();

		WRITE_BKP_CONFIGURATION(CONFIGURATION_DONE);
	}
	else
	{
		/* PWR and BKP clocks selection */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
		for(WaitForOscSource = 0; WaitForOscSource < 5000; WaitForOscSource++);
		RTC_WaitForLastTask();

		/* Enable the RTC Alarm */
		RTC_ITConfig(RTC_IT_SEC, ENABLE);
		RTC_WaitForLastTask();
	}

	/* Check if how many days are elapsed in power down/Low Power Mode-
		Updates Date that many Times*/
	CheckForDaysElapsed();
}