相关初始化程序为: /******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { int len; u8 bufs[50]; #ifdef DEBUG debug(); #endif
/* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration();
/* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */
TIM_Configuration(); GPIO_Configuration();
if (BKP_ReadBackupRegister(BKP_DR1) != 0x5AA5) { /* Backup data register value is not correct or not yet programmed (when the first time the program is executed) */
//printf("
RTC not yet configured....");
/* RTC Configuration */ if(RTC_LSE_Configuration()) #if 1 { LedAlarm(HIGH_LEVEL); while(1); } else { LedFail(LOW_LEVEL); LedAlarm(LOW_LEVEL); LedRun(HIGH_LEVEL); } #else RTC_Configuration(); #endif
/* Adjust time by values entred by the user on the hyperterminal */ Time_Adjust(secondcount);
BKP_WriteBackupRegister(BKP_DR1, 0x5AA5); } else { /* Check if the Power On Reset flag is set */ if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET) { //printf("
Power On Reset occurred...."); } /* Check if the Pin Reset flag is set */ else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET) { //printf("
External Reset occurred...."); }
//printf("
No need to configure RTC...."); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro();
/* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); }
LcdInit(); InitKey();
/* Initialize the SPI FLASH driver */ SPI_FLASH_Init();
ADCInit(); SPI_ADC_Init(); DisplayMenuTest();
while (1) { len=ReadComm(1, bufs, 8);
if(len)//<100*1024) { //int i; //for(i = 0; i < 0x2000; i++); WriteComm(1, bufs, len);
//len+=len; } } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2);
/* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */ RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
/* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } }
/* Enable peripheral clocks --------------------------------------------------*/ /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable GPIOA and USART1 clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_TIM3|RCC_APB1Periph_SPI2|RCC_APB1Periph_TIM2|RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); }
#endif /******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures the nested vectored interrupt controller. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ #ifdef VECT_DFU NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); #else NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif #endif
/* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the RTC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Enable the DMA1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM2 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/******************************************************************************* * Function Name : RTC_Configuration * Description : Configures the RTC. * Input : None * Output : None * Return : None *******************************************************************************/
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 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();
/* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(40000);
/* 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); }
int RTC_LSE_Configuration(void) { u32 tick1,tick2;
/* 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 */ tick1 = 0; while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { LedFail(HIGH_LEVEL); Delayms(3000); LedFail(LOW_LEVEL); Delayms(3000); RCC_LSEConfig(RCC_LSE_ON); } if(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { xtal_32k = 1; //RCC_LSEConfig(RCC_LSE_OFF); //PrintStr(10, 10, "LSE work abnormal", 16); //Delayms(2000); 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_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();
return 0; }
|