cancel
Showing results for 
Search instead for 
Did you mean: 

clock set problem

kemal
Associate II
Posted on February 06, 2015 at 17:47

hi all

mcu stm32f103 VBAT pin I have connected the to battery mcu or external I have connected the 3.2 volts I have set rtc_setcount hour =23:59:59 hour = 0 now works no problem no problem if you have or external 3.2 volts VBAT pin connected to the battery When I turn off again (external 3.2 volts) the problem is time value of hour 25 begins What is the problem? Thank you.

void RTC_setting(void)
{
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
// Check if the Power On Reset flag is set
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
// printf(''\r\n\n Power On Reset occurred....'');
}
// Check if the Pin Reset flag is set
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
// printf(''\r\n\n External Reset occurred....'');
}
//printf(''\r\n 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();
}
#ifdef RTCClockOutput_Enable
// Enable PWR and BKP clocks
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
// Allow access to BKP Domain
PWR_BackupAccessCmd(ENABLE);
// Disable the Tamper Pin
BKP_TamperPinCmd(DISABLE); // To output RTCCLK/64 on Tamper pin, the tamper
// Enable RTC Clock Output on Tamper Pin
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
// Clear reset flags
RCC_ClearFlag();
}
////////////////////////////////////////////////////////////////////////////
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
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 = (768 KHz)/(32767+1)
// Wait until last write operation on RTC registers has finished
RTC_WaitForLastTask();
}
/////////////////// stm32f103 ///////////////////////////////
int main(void)
{
Delay(0XFFFF);
char state; 
char state_1;
char rtcstep=1;
char rtcbit= 0; //1 set RTC_SetCounter set // 0 no set RTC_SetCounter 
uint32_t hour;
uint32_t minute;
uint32_t sec;
RTC_setting();
while(1)
{
////////////////// reading ////////////////////
if (rtcbit == 0) 
{
TimeDisplay = RTC_GetCounter();
hour = TimeDisplay / 3600;
minute = (TimeDisplay % 3600) / 60;
sec= (TimeDisplay % 3600) % 60;
///////////////////////////////////////////////////////////////////////////////////
//if(RTC_GetCounter() == 86399)//23:59:59 //RTC_SetCounter(((hour*60 + minute)*60 + sec)); 
//{
//PWR_BackupAccessCmd(ENABLE);
//RTC_WaitForLastTask();
//RTC_SetCounter(0x0);
//RTC_WaitForLastTask();
//PWR_BackupAccessCmd(DISABLE);
//}
 if (hour>23)
 {
 hour =0;
 } 

 hour _convert(hour,&hourBuffer[0],2);
print_hour (206,130,(unsigned char *)&hourBuffer[0],red,lacivet);
minute_convert(minute,&minuteBuffer[0],2);
print_minute (206,180,(unsigned char *)&minuteBuffer[0],redi,lacivet);
sec_convert(sec,&secBuffer[0],2);
print_sec (206,230,(unsigned char *)&secBuffer[0],red,lacivet);
}
///////////////////////////RTC_SetCounter set /// hour minute sec //////
state =GPIO_ReadInputDataBit(PORTA, butonup);
if(state==0) 
Delay(0XFFF);
{
rtcstep++ ; 
if (rtcbit ==1) 
{
if (rtcstep==1)
{
hour =hour+1;
if (hour==24)
{
hour =0;
}
hour_convert(hour,&hourBuffer[0],2);
print_hour (206,130,(unsigned char *)&hourBuffer[0],red,lacivet);
}
if (rtcstep==2)
{
minute=minute+1;
if (minute ==60) 
{
minute =0;
}
minute_convert(minute,&minuteBuffer[0],2);
print_minute (206,180,(unsigned char *)&dkkBuffer[0],red,lacivet);
}
if (rtcstep==3)
{
sec =sec+1;
if (sec ==60) 
{
sec =0;
}
sec_convert(sec,&snyBuffer[0],2);
print_sec (206,230,(unsigned char *)&secBuffer[0],red,lacivet);
}
//////////////////RTC_SetCounter // write //////////////////// 
if (rtcstep==4) 
{
PWR_BackupAccessCmd(ENABLE);
RTC_WaitForLastTask();
RTC_SetCounter(((hour*60 + minute)*60 + sec)); 
RTC_WaitForLastTask();
PWR_BackupAccessCmd(DISABLE);
rtcstep==1; 
rtcbit=0; ////// go reading
}
} 
///////////////////////Rtc_getcount delete///////////////////////
state_1 =GPIO_ReadInputDataBit(PORTA, enterbuton);
if(state_1==0)
Delay(0XFFF);
{
rtcbit ==1;
PWR_BackupAccessCmd(ENABLE);
TimeDisplay = RTC_GetCounter();
hour = 0;
minute = 0;
sec = 0;
/////////////////////RTC_SetCounter set ///////////////////// write ///////
RTC_SetCounter(((hour*60 + minute)*60 + sec)); 
hour_convert(hour,&hourBuffer[0],2);
print_hour (206,130,(unsigned char *)&hourBuffer[0],red,lacivet);
minute_convert(minute,&minuteBuffer[0],2);
print_minute (206,180,(unsigned char *)&minuteBuffer[0],red,lacivet);
sec_convert(sec,&secBuffer[0],2);
print_sec (206,230,(unsigned char *)&secBuffer[0],red,lacivet);
PWR_BackupAccessCmd(DISABLE);
state_1=1;
}
}

1 REPLY 1
kemal
Associate II
Posted on February 07, 2015 at 01:22

                                     

                     problem solved.

                     

Added

line

.

                 if (hour>23)

                    {

                         hour =0;

                    }