前面在DEBUG过程中一直认为是晶体问题,要求6P,结果试了很多个6P晶体均无法起振,反复很长时间,也按照要求去选择晶体,直到后来看了下参考DEMO的电路图,发现PC13引脚被上拉了,后来参照接法,就很快起振了(本人电路为下拉),但之前察看了很多个参考资料均未提及此条件,不知为何,还请斑竹确认下。
另在初始化时将侵入检测重新关闭一次,也可以起振,这点也未看到有提示,还请核实,是否如此。THKS!!!
playzwm,你的RTC的时钟初始化有问题
你的问题与PC13引脚被上拉无关。
RTC初始化程序
相关初始化程序为:
/*******************************************************************************
* 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;
}
程序中有点疑问
在运行这一行代码时:if (BKP_ReadBackupRegister(BKP_DR1) != 0x5AA5),你还没有对Backup区域提供时钟,同时你也没有使能可以访问Backup区域,你读取BKP_DR1的值正确否?
ST-你的意思是BAK区别必须先提供时钟?
必须先提供时钟才能访问?那如果才能Config这个区域呢?
是的
在参考手册中“Backup区域”第一小节就提到了。
楼主不会是没有看手册就写程序吧?
参考手册可是最最重要的手册。
BKP控制寄存器也需要先RTC起振之后才能设置???
老大,我怎么没找到这个阿,为何跟我的Datasheet不一样?
所以,要随时到网站上跟新参考手册和数据手册啊
http://www.st.com/mcu/modules.php?name=mcu&file=familiesdocs&FAM=110#Reference%20Manual
中文版是V7.0(2008-12),最新英文的是V8.0
你要看中文的,这个V7.0先凑合着看吧~~~
http://www.stmicroelectronics.com.cn/stonline/mcu/MCU_Pages.htm
手册是这么说的!?
PWR_CR的DBP为是取消后备区域的写保护!
但是,写不能写,读的还是可以的吧!就算是写保护了,还是可以读的。
是不是这样!?
晕,就这玩意搞了一个多月
如果说是这个原因,那为何把PC13接入上拉就可以起振了呢?关闭侵入检测也可以了呢?可否解释下。。。
对不起,我们无法对于没有按照手册要求操作所产生的后果
请您谅解。