CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

My RTCC can't keep its time when VDD shutdown
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

My RTCC can't keep its time when VDD shutdown
PostPosted: Tue Sep 03, 2019 7:01 am     Reply with quote

Hi there,

I need some help for use the internal RTCC of a PIC16F19176. I’m using compiler version 5.088.

I wrote a simple code and using only the RTCC and RS232 for debugging.

Main.c:
Code:
#include <main.h>

void set_clock(rtc_time_t &date_time)
{
   date_time.tm_year=19;
   date_time.tm_mon=9;
   date_time.tm_mday=2;
   date_time.tm_wday=2;
   
   date_time.tm_hour=14;
   date_time.tm_min=48;
   date_time.tm_sec=0;
}


void main()
{
   rtc_time_t write_clock, read_clock;
   
   setup_rtc(RTC_ENABLE | RTC_CLOCK_SOSC | RTC_ALARM_DISABLE | RTC_CHIME_DISABLE,0);         //enables internal RTCC
   setup_spi(SPI_DISABLED);
   setup_wdt(WDT_OFF);
   
   fprintf(UART,"\r\nRestart = %LX\r\n",restart_cause());
   
   set_clock(write_clock);

   rtc_write(&write_clock);         //writes new clock setting to RTCC
     
   while(1)
   {
      rtc_read(&read_clock);        //reads clock value from RTCC
      printf("\r%02u/%02u/20%02u %02u:%02u:%02u",read_clock.tm_mday,read_clock.tm_mon,read_clock.tm_year,read_clock.tm_hour,read_clock.tm_min,read_clock.tm_sec);
      delay_ms(250);
   }
}



Main.h :
Code:
#include <16F19176.h>

#device ADC=12

#FUSES RSTOSC_SOSC              //On Power-up clock running from SOSC
#FUSES VBATEN                   //VBAT functionality is enabled
#FUSES CKS                      //Clock Switching Enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES MCLR                     //Master Clear pin enabled
#FUSES PUT_16MS                 //Power-Up Timer set to 16ms
#FUSES LPBOR                    //Low-Power Brownout reset is enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV24                   //Brownout reset at 2.4V
#FUSES ZCDDIS                   //Zero-cross detect circuit is disabled at POR
#FUSES PPS1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES WDTSW                    //Watch Dog Timer Postscale settable in software
#FUSES WDTWIN_SW                //Watchdog Window is settable in software
#FUSES WDTCLK_SW                //WDT clock source settable in software
#FUSES BBSIZ512                 //Boot block size 512 bytes
#FUSES LVP                      //Low Voltage Programming on B3(PIC16) or B5(PIC18)

#use delay(internal=8MHz)

#pin_select U1TX = PIN_C6
#pin_select U1RX = PIN_C7

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=UART)


Hardware:
- 3V coin cell (fully charged) connected to pin RA5/VBAT
- 32.768kHz quartz (Wurth 830003000) to pin RC0 and RC1 with 2x15pF capacitor to GND
- VDD = 5VDC

I program the microcontroller with this code and set the date and time to 02 / 09 / 2019 14:48:00. I run the program and receive this message from the RS232:
Restart = 0F36
02 / 09 / 2019 14:48:00

- 1st: 0x0F36 is not a restart cause
- 2nd: The date and time don't increment

Then I switch off my power supply (only VDD and NOT Vbat). I run the program and receive this message from the RS232:
Restart = 0B3C
02 / 09 / 2019 14:48:00

- 1st: 0x0B3C is not a restart cause either
- 2nd: The date and time don't increment

Next I comment the call to the functions set_clock(write_clock) and rtc_write(&write_clock). I program the microcontroller with this code (without switch off the power supply) . I run the program and receive this message from the RS232:
Restart = 0F36
02 / 09 / 2019 14:48:00

- 1st: 0x0F36 is not a restart cause
- 2nd: The date and time increment correctly without any drift on more than 20min

Then I switch off my power supply (only VDD and NOT Vbat). I run the program and receive this message from the RS232:
Restart = 0B3C
00 / 00 / 2000 00:00:00

- 1st: 0x0B3C is not a restart cause
- 2nd: The date and time increment correctly without any drift on more than 20min
- 3rd: The date and time don't keep the previous setting and restart from zero


- The RTCC don't start after programming if I use the rtc_write() function (my 32.768kHz quartz don't work)
- The RTCC keep the date and time even if I reprogram the microcontroller and run perfectly without rtc_write() function (my 32.768kHz quartz work perfectly)
- The RTCC don't keep the date and time set without power supply even if the external quartz run perfectly (I put my scope on it and tick perfectly with and without power supply).

I think I don’t use the right #FUSES setting or missing something stupid...!?

Can you help me please?


Last edited by BeeElectronic on Tue Sep 03, 2019 7:18 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 7:12 am     Reply with quote

while I don't use that PIC...
this...
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)

probably should be NOLVP....as 99% of all programmers don't use that..

..
this...
#FUSES RSTOSC_SOSC //On Power-up clock running from SOSC

says to me PIC will be running at 32KHz ??

other who use that PIC will reply, may take awhile....
Jay
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 7:31 am     Reply with quote

Hi temtronic,

I change the LVP fuse to NOLVP.

For me (I don't know if I'm right) it means before and during the power up the
PIC running from the SOSC at 32kHz until the end of the power-up (here 16ms, #FUSES PUT_16MS)

By default the setting is :"On power-up clock running from external oscillator" and the setting can be : run from HFINTRC and LFINTRC (with and without PLL).

I just try multiple configurations with this fuse but it doesn't work.
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 7:51 am     Reply with quote

If I want my SOSC to start I need to put the setup_rtc() after my rtc_write() function!

Code:
void main()
{
   int16 res = 0;
   rtc_time_t write_clock, read_clock;

   res = restart_cause();
   
   fprintf(UART,"\r\nRestart = %LX\r\n",res);

   //setup_rtc(RTC_ENABLE | RTC_CLOCK_SOSC | RTC_ALARM_DISABLE | RTC_CHIME_DISABLE, 0); // enables internal RTCC
         
   setup_spi(SPI_DISABLED);
   setup_wdt(WDT_OFF);

   set_clock(write_clock);

   rtc_write(&write_clock);         //writes new clock setting to RTCC
   
   setup_rtc(RTC_ENABLE | RTC_CLOCK_SOSC | RTC_ALARM_DISABLE | RTC_CHIME_DISABLE, 0); // enables internal RTCC


   while(1)
   {
      rtc_read(&read_clock);        //reads clock value from RTCC
      printf("\r%02u/%02u/20%02u %02u:%02u:%02u",read_clock.tm_mday,read_clock.tm_mon,read_clock.tm_year,read_clock.tm_hour,read_clock.tm_min,read_clock.tm_sec);
      delay_ms(250);
   }
}


It's weird...
gaugeguy



Joined: 05 Apr 2011
Posts: 288

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 8:20 am     Reply with quote

shouldn't set_clock(write_clock); be set_clock(&write_clock); instead?
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 8:29 am     Reply with quote

gaugeguy wrote:
shouldn't set_clock(write_clock); be set_clock(&write_clock); instead?


Nope...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 12:37 pm     Reply with quote

BeeElectronic wrote:

Next I comment the call to the functions set_clock(write_clock) and
rtc_write(&write_clock). I program the microcontroller with this code

Maybe the re-programming is affecting the RTCC.
Instead of re-programming, why not add this code near the start of
your program. It uses RS-232 communications to let you ask the
user to write a sample date/time to the RTCC or not:
Code:

printf("Do you want to write a sample date/time of\n\r");

while(TRUE)
  {
   c = getc();    // Wait for user to press a key
   c = toupper(c);  // Add #include <ctype.h> to use this function
   
   if(c == 'Y')
     {
      set_clock(write_clock);
      printf("\n\r");
      printf("New date/time written to RTCC.\n\r");
      break;
     }

   if(c == 'N')
      break; 
  }
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Sep 03, 2019 1:16 pm     Reply with quote

Get rid of the RSTOSC_SOSC fuse.

Problem is this overrides the secondary oscillator settings.
Thinking about it, this may well program the SOSC to use high power mode,
which won't be compatible with teh 32K crystal.
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Wed Sep 04, 2019 7:34 am     Reply with quote

PCM programmer:
I try you little code. Now my RTCC start working after a 'Y' with my custom date and time or 'N' with default date and time.

The only difference with my code was my reaction time. So I found that I need a delay between setup_rtc() and set_clock() / rtc_write() functions.
This delay need to be bigger than 2.5s (perhaps the time between SOSC and the internal clock switch and / or the SOSC quartz start-up... I don't know).

After a few seconds without VDD the RTCC keep correctly my custom date and time BUT NOT EVERY TIME ! Sometimes the RTCC don't keep the time and
restart from 00:00:00.

Ttelmah:
It's make any difference if I use the RSTOSC_SOSC or not...

Other ideas?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Sep 04, 2019 8:30 am     Reply with quote

OK.
There was a risk, since this is saying to use the secondary oscillator as the
master CPU oscillator on boot. Now if the compiler looks at your clock setting
and sees the '16MHz' there is might set the secondary oscillator to high power
mode, which would then overdrive a 32K crystal....
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Wed Sep 04, 2019 8:50 am     Reply with quote

I lost my time, during a switch off, only if my restart cause is:
0x0B3C
0x0B34

And never with this restart causes:
0x0F36
0x0F3C

For the PIC16F19176:
Code:
#define NORMAL_POWER_UP      0xF3C
#define BROWNOUT_RESTART     0xF3E
#define MCLR_FROM_SLEEP      0xE37
#define WDT_TIMEOUT          0xD2F
#define WDT_FROM_SLEEP       0xC3F
#define INTERRUPT_FROM_SLEEP 0xE3F
#define MCLR_FROM_RUN        0xF37
#define RESET_INSTRUCTION    0xF3B
#define STACK_OVERFLOW       0xFBF
#define STACK_UNDERFLOW      0xF7F
#define WDT_WINDOW_VIOLATION 0xF1F
#define MEMORY_VIOLATION     0x73F
#define VBAT_BROWNOUT_RESET  0xB3E


0x0F36 is when I just re-program the PIC (not in the list)
0x0F3C is a normal power up
0x0B3C and 0x0B34 mean 0xB3E a Vbat brownout reset?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 04, 2019 9:08 am     Reply with quote

This Microchip forum thread may be useful:
https://www.microchip.com/forums/m926122.aspx
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Thu Sep 05, 2019 1:45 am     Reply with quote

PCM programmer wrote:
This Microchip forum thread may be useful:
https://www.microchip.com/forums/m926122.aspx


I add this setup code:
Code:
   if(res == 0x0F3C)
   {
      setup_rtc(RTC_ENABLE | RTC_CLOCK_SOSC | RTC_ALARM_DISABLE | RTC_CHIME_DISABLE, 0); // enables internal RTCC
     
      fprintf(UART,"Setup OK\r\n");
   }


When a 0x0B3C or 0x0B34 restart cause occur I don't run the setup_rtc() function. This thing work for the reset of the date and time but
after the RTCC don't start ticking.

Conclusion:
If a 0x0B3C (probably mean a Vbat brownout reset, #define VBAT_BROWNOUT_RESET 0xB3E) the setup_rtc function reset the previous date
and time. But If I don't do the setup_rtc, the RTCC don't start even if the date and time is setup correctly and not reset.

I put my scope on the Vbat track on my PCB and setup the trigger with a falling edge and 2.9V trig level. My scope stop on every power on or off
with just a little interference but they are the same between a 0x0F3C and a 0x0B3C restart cause...
BeeElectronic



Joined: 19 Jul 2017
Posts: 27

View user's profile Send private message

PostPosted: Thu Sep 05, 2019 2:08 am     Reply with quote

In some case even if I don't run the setup_rtc function my RTCC restart at 00:00:00 in case of a 0x0B3C restart cause...

I migrate from a PIC16F1936 + DS1307 to this PIC16F19176 with internal RTCC, for more memory, but if this RTCC is not reliable
I thing I go back to a PIC16F19176 + DS1307.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Thu Sep 05, 2019 2:52 am     Reply with quote

Seriously there should not be any detectable ripple on the Vbat connection.
2 or 3mV at most.
How long is the connection?.
Is the battery ground connected to the same point as the processor ground?.
How it the battery connection physically made?.
Do you have any capacitor on the Vbat connection?.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group