View previous topic :: View next topic |
Author |
Message |
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
RTC in 18F67J94 with VBat loss the time |
Posted: Mon Apr 25, 2016 6:34 am |
|
|
Hi, I have a circuit with 18F67J94. Run with internal oscillator (8M) and secondary oscillator of 32768K.
I need the RTC run with a CR2032 battery in VBat when the external Vdd is lost.
My code is:
Code: |
#include <18F67J94.h>
#DEVICE ADC=10
#fuses FRC,SOSC_LOW,NOCLOCKOUT,NOIESO,NOIOL1WAY,PROTECT,VREGSLEEP_SW
#include <PIC18F67J94_registers.h>
#include "2464.c"
#use delay(clock=8000000)
void main()
{
setup_spi (SPI_SS_DISABLED);
setup_lcd (LCD_DISABLED);
setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_3 (T3_INTERNAL|T3_DIV_BY_8);
setup_timer_2 (T2_DISABLED, 0, 1);
setup_timer_1 (T1_EXTERNAL);
setup_oscillator (OSC_INTRC);
setup_adc (ADC_OFF);
setup_rtc(RTC_ENABLE | RTC_CLOCK_SOSC,0);
SETUP_CCP1(CCP_OFF );
.........
WHILE (true)
{
rtc_read(&read_clock); //reads clock value from RTCC
contadorseg=read_clock.tm_sec;
contadormin=read_clock.tm_min;
contadorhor=read_clock.tm_hour;
contadordia=read_clock.tm_wday;
...........
delay_ms(500);
}
}
|
The clock runs ok with external supply but the problem is when I lose the external supply, I lose the time. VBat don't run.
Where is the problem?? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Apr 25, 2016 11:28 am |
|
|
Schematic?
Mike |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Apr 25, 2016 12:45 pm |
|
|
Slow day here so I downloaded the datasheet.....seems all you have to do for 'hardware' is add battery to Vbat pin and gnd !!
I'm thinking a fuse has not been enabled or 'misenabled' by the compiler. One would have to dump listing, check appropriate fuse for correct bits being on.
I don't use that PIC, can't confirm/deny that CCS header has correct variables/fuses named/set right.
I did read though that the main PIC power HAS to be totally disconnected for Vbat to run the RTCC though....
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Mon Apr 25, 2016 2:08 pm |
|
|
Also:
setup_spi (SPI_SS_DISABLED);
Is wrong. The syntax to disable the spi is:
setup_spi(FALSE);
This is a classic from using the wizard, and not understanding it's options.... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Mon Apr 25, 2016 2:30 pm |
|
|
Even though Timer0 and the RTC don't appear to be related on this chip (at least at my first glance into the data sheet), you do appear to associate RTC values with Timer 0. I noticed you setup Timer 0 to run off the internal clock and the RTC to run off of an external 32k crystal.
Is the RTC supposed to run off an external crystal or the internal RC? You might also try the flashing LED test with Timer 0 and see if it comes out to the speed you anticipate. |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Wed Apr 27, 2016 3:08 am |
|
|
Thank to all.
The RTC is running fine when external supply is on. But when i loss external supply, the VBat battery does not seem to fulfill its function and i loss the time.
The battery is connected to VBat pin (pin 18) directly. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Wed Apr 27, 2016 3:29 am |
|
|
How do you set the value in the clock?.
You leave out this part of the code.
You do realise that the restart from Vbat, is seen as a reset, and so if you have code that initialises the clock, this will be executed.
You need to test 'restart_cause' near the start of your code, and only call the code to actually send values to the clock, if this is 'NORMAL_POWER_UP'. |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Wed Apr 27, 2016 4:54 am |
|
|
Thanks Ttelmah:
You are right. The first execution i set the clock, but no subsequent executions.
Only first execution:
Code: |
set_clock(write_clock);
rtc_write(&write_clock);
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Wed Apr 27, 2016 7:08 am |
|
|
Have you actually tested that this _is_ only being called when you expect?. It'd be the first thing I'd suspect, that this is incorrectly being called. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed Apr 27, 2016 7:34 am |
|
|
It's time (no pun) for you to post your 'test' program ! It should only be 20-30 lines of code, only testing the RTC .
Jay |
|
|
guy
Joined: 21 Oct 2005 Posts: 297
|
|
Posted: Wed Apr 27, 2016 10:47 am |
|
|
I would also look into the registers with the debugger, to see that they are set correctly by the compiler. With these type of peripherals there could easily be a bug in the compiler, esp. since Microchip keeps coming up with new chips and CCS has to keep up. The same goes to the fuses. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19516
|
|
Posted: Wed Apr 27, 2016 1:56 pm |
|
|
Yes.
My actual suspicion is that the restart code might not be correctly identifying that this is a restart with Vbat, and hence the initialisation code could be being called every time.
The actual operation of Vbat, doesn't seem to have any control registers associated for this use (it does for sleep operation, and there are a lot of controls for this, in the processor include file, but there doesn't seem to be an identifier for RESET_FROM_VBAT (or something very similar), which I'd expect if restart_cause was correctly handling this. If he is instead going 'DIY', you have to control the bit properly to handle this, hence the need to see what he is actually doing. |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Fri Jul 29, 2016 5:50 am |
|
|
Hi, I'm sorry but I have not seen before post.
I can see if the restart is from VBat with bit MCU_VBAT in RCON3.0
Then i use this code:
Code: |
rtc_time_t write_clock, read_clock;
setup_rtc(RTC_ENABLE | RTC_CLOCK_SOSC,0);
SRETEN=1;//pongo en marcha Regulador de Retencion, para VBAT
if (MCU_VBAT==1){
//Tiene la pila puesta, y entra aquĆ
MCU_VBAT=0;
}
else{
set_clock(write_clock);
rtc_write(&write_clock); //writes new clock setting to RTCC
}
WHILE (true)
{
rtc_read(&read_clock); //reads clock value from RTCC
contadorseg=read_clock.tm_sec;
contadormin=read_clock.tm_min;
contadorhor=read_clock.tm_hour;
contadordia=read_clock.tm_wday;
write_clock.tm_wday=contadordia;
rtc_write(&write_clock);
}
|
If the code restart with VBat=1, then contadorhor, contadormin,..... have invalid values, but if restart with VBat=0 then RTC run ok.
Any suggestions?? |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Fri Apr 30, 2021 9:15 am |
|
|
Hello,
I plan to use RTC with this PIC and with VBat, did you solve it ? What was the issue ?
Thanks for your help
Spilz |
|
|
|