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

Help ! 18f67k22 RTC
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

Help ! 18f67k22 RTC
PostPosted: Tue Nov 20, 2012 1:21 pm     Reply with quote

Hello everybody,

I have a pic 18f67k22, using the compiler 4128.
I'm not having success when using the internal clock RTC with the pic, because I have no time accuracy.

What occurs:

I put it to count 1 in 1 second and will accumulate, but by passing 1 minute, he told the truth 56 seconds.

I'm losing a lot of precision, because in 15 minutes already lost 1 minute. It is very wrong!

I tried changing the pic to see if the problem was in the pic and the other gave the same error, but not delayed 4 seconds and said yes 4 seconds.

Maybe my RTC needs a calibration or configuration, maybe FUSE, do not know for sure.

Could you help me?

If someone can post this code that is simple to help me. I think I am missing in configuration or calibration, not sure.

I am now without the code, was at work.

Hug everyone
temtronic



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

View user's profile Send private message

PostPosted: Tue Nov 20, 2012 2:08 pm     Reply with quote

Timing problems can occour due to board layout, wrong xtal selection, wrong cap values, changes in temperature, humidity etc. Even the Vcc ( 5 v vs 3 v) can affect the overall timing as well as current 'surges' from other sources.

Something to consider in the overall project. The PIC must have battery backup and some 'failsafe' code/hardware should primary power be lost.
Using an RTC chip like the DS1307 eliminates 99% of the headaches. Yes, costs a couple of dollars but then what is YOUR time worth?
Other benefits to a hardware RTC include an easy interrupt (say 1Hz) AND a few bytes of CMOS RAM (scratchpad) to store important data when main power fails (it will......) and a LOT less code for 'timekeeping', freeing up codespace for other functions.

hth
jay
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Tue Nov 20, 2012 2:16 pm     Reply with quote

temtronic wrote:
Timing problems can occour due to board layout, wrong xtal selection, wrong cap values, changes in temperature, humidity etc. Even the Vcc ( 5 v vs 3 v) can affect the overall timing as well as current 'surges' from other sources.

Something to consider in the overall project. The PIC must have battery backup and some 'failsafe' code/hardware should primary power be lost.
Using an RTC chip like the DS1307 eliminates 99% of the headaches. Yes, costs a couple of dollars but then what is YOUR time worth?
Other benefits to a hardware RTC include an easy interrupt (say 1Hz) AND a few bytes of CMOS RAM (scratchpad) to store important data when main power fails (it will......) and a LOT less code for 'timekeeping', freeing up codespace for other functions.

hth
jay




Thanks for the tips!
But I have to use the internal RTC in this project.
It makes all the difference for me, is something commercial pos. and have to reduce cost and size.
I understand it has several things that can interfere with the accuracy of the timer.
but how could I cntrolar these variables?
I believe that even with calibration or configuration problem, because the error is repeated forever.
I'll check these variables as external interference, voltage drop etc, but I have to feed it with 5v for reasons of design.
Could you help me?
a code simple calibration and configuration that can help me.
never done it as I could be missing this point.

Thanks to all
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 20, 2012 4:46 pm     Reply with quote

May seem like a silly question but:-

Are you using a crystal or the internal oscillator for cost reasons?

Mike
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Tue Nov 20, 2012 4:58 pm     Reply with quote

Mike Walne wrote:
May seem like a silly question but:-

Are you using a crystal or the internal oscillator for cost reasons?

Mike

The project may admit an external crystal case solves the problem.
But we can not use an external RTC, project requirement.
The clock for the RTC internal clock can be internal or external, but prefer the internal clock.
We try to use the clock external to try to resolve the problem, but could not make it work.
Ttelmah



Joined: 11 Mar 2010
Posts: 19278

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 3:00 am     Reply with quote

lucasromeiro wrote:
Mike Walne wrote:
May seem like a silly question but:-

Are you using a crystal or the internal oscillator for cost reasons?

Mike

The project may admit an external crystal case solves the problem.
But we can not use an external RTC, project requirement.
The clock for the RTC internal clock can be internal or external, but prefer the internal clock.
We try to use the clock external to try to resolve the problem, but could not make it work.


The internal LFINTOSC clock will _never_ be remotely 'accurate'....
It is only quoted as having +/-15% accuracy. It's timing also drifts with temperature.
You can adjust an offset value in the RTCC, telling it to add pulses every second, or skip pulses every second, to tune the timing to be 'close', but this needs to be done for every chip (the LFINTOSC, is _not_ supplied with a calibration value from the factory, that can be accessed via the fuses), and will still give a timing that will change be several minutes per day if the temperature shifts...

You access this with:
Code:

signed int8 RTCCAL;
#byte RTCCAL=(getenv("SFR:RTCCAL"))

//Then

RTCCAL=1 to 127 gives +ve adjustment
RTCCAL=-1 to -128 gives -ve adjustment


The adjustment is in added/subtracted RTC clocks/minute, in steps of four, from 0 to 508 positive, and 0 to 512 negative.
You can also access this in the setup_rtc function, by adding the value to the command, so:
Code:

setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 05);


Will give 20 pulses per minute added to the RTC count.

Realistically to get anything approaching 'accuracy', you must use the external oscillator.

This requires a watch crystal between SOSCO and SOSCI. Suitable loading capacitors on this. Enabling SOSC with:
setup_oscillator(OSC_SOSC_ON);
and selecting the RTC to run from the secondary oscillator (RTCOSC_T1 fuse).
I'm afraid the internal LFINTOSC, is not the tool to use if you want anything approaching an accurate RTCC....

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 3:13 am     Reply with quote

OK. So it wasn't a silly question (Should have been).

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19278

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 4:11 am     Reply with quote

No, it definately wasn't. Also though it is a 'read the data sheet' moment for the original poster. Smile

Best Wishes
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 4:44 am     Reply with quote

Ttelmah wrote:
lucasromeiro wrote:
Mike Walne wrote:
May seem like a silly question but:-

Are you using a crystal or the internal oscillator for cost reasons?

Mike

The project may admit an external crystal case solves the problem.
But we can not use an external RTC, project requirement.
The clock for the RTC internal clock can be internal or external, but prefer the internal clock.
We try to use the clock external to try to resolve the problem, but could not make it work.


The internal LFINTOSC clock will _never_ be remotely 'accurate'....
It is only quoted as having +/-15% accuracy. It's timing also drifts with temperature.
You can adjust an offset value in the RTCC, telling it to add pulses every second, or skip pulses every second, to tune the timing to be 'close', but this needs to be done for every chip (the LFINTOSC, is _not_ supplied with a calibration value from the factory, that can be accessed via the fuses), and will still give a timing that will change be several minutes per day if the temperature shifts...

You access this with:
Code:

signed int8 RTCCAL;
#byte RTCCAL=(getenv("SFR:RTCCAL"))

//Then

RTCCAL=1 to 127 gives +ve adjustment
RTCCAL=-1 to -128 gives -ve adjustment


The adjustment is in added/subtracted RTC clocks/minute, in steps of four, from 0 to 508 positive, and 0 to 512 negative.
You can also access this in the setup_rtc function, by adding the value to the command, so:
Code:

setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 05);


Will give 20 pulses per minute added to the RTC count.

Realistically to get anything approaching 'accuracy', you must use the external oscillator.

This requires a watch crystal between SOSCO and SOSCI. Suitable loading capacitors on this. Enabling SOSC with:
setup_oscillator(OSC_SOSC_ON);
and selecting the RTC to run from the secondary oscillator (RTCOSC_T1 fuse).
I'm afraid the internal LFINTOSC, is not the tool to use if you want anything approaching an accurate RTCC....

Best Wishes



Good morning Ttelmah,
Thanks for explanations.
Understand the question of accuracy in using the internal clock.
could you help me to configure the RTC to work with external clock?

Shall not know how to do this configuration.

Another thing: what should I use crystal for the RTC external?
What capacitors?

Thanks for everything, is helping me a lot!

Hugs
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 5:08 am     Reply with quote

The microchip data sheet has ALL the information you need for whichever oscillator you choose.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19278

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 8:00 am     Reply with quote

and of course the data sheet for whatever crystal you use. Key thing that is often 'missed', is that the two capacitors used in the crystal circuit, are each paralleled by the pin and track capacitances, and are then seen _in series_ as the crystal load.
I have already posted what is needed to use the external crystal.

Best Wishes
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 9:51 am     Reply with quote

Ttelmah wrote:
and of course the data sheet for whatever crystal you use. Key thing that is often 'missed', is that the two capacitors used in the crystal circuit, are each paralleled by the pin and track capacitances, and are then seen _in series_ as the crystal load.
I have already posted what is needed to use the external crystal.

Best Wishes

You refer to this configuration crystal?

http://destro.hostpo.net/images_projetos/uc_8051-basic-6.png

I just need to turn this way and configure the lines that you have posted?

thank you

Ttelmah



Joined: 11 Mar 2010
Posts: 19278

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 10:15 am     Reply with quote

The RTC, uses the _secondary oscillator_. On pins 29, and 30.

You need to identify a 32KHz watch crystal that meets the accuracy you require, then get _it's_ data sheet, and from the design of your board, calculate the track capacitance, add the PIC pin capacitance, and calculate C1, and C2, to give the loading your crystal requires.

It is _all_ down to reading data sheets, as Mike says. They also have application notes explaining how best to layout the tracks round the crystal pins, and things to avoid.
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

PostPosted: Wed Nov 21, 2012 12:45 pm     Reply with quote

Ttelmah wrote:
The RTC, uses the _secondary oscillator_. On pins 29, and 30.

You need to identify a 32KHz watch crystal that meets the accuracy you require, then get _it's_ data sheet, and from the design of your board, calculate the track capacitance, add the PIC pin capacitance, and calculate C1, and C2, to give the loading your crystal requires.

It is _all_ down to reading data sheets, as Mike says. They also have application notes explaining how best to layout the tracks round the crystal pins, and things to avoid.

hello,
i use this configuration, but dont work:

Code:

#FUSES RTCOSC_T1

setup_rtc(RTC_ENABLE|RTC_OUTPUT_SOURCE,0);           
//setup_oscillator(OSC_SOSC_ON);

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16); //Incremento em 210mS
  //setup_timer_1(T1_DISABLED); //Habilita o clock do rtcc
  // setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_ENABLE_SOSC); //Habilita o clock do rtcc
setup_timer_1(T1_ENABLE_SOSC);
setup_timer_2(T2_DIV_BY_4,249,5); //Incremento em 1.0mS
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
lucasromeiro



Joined: 27 Mar 2010
Posts: 167

View user's profile Send private message

CODE
PostPosted: Wed Nov 21, 2012 1:27 pm     Reply with quote

Code:

//Parâmetros Gerais
     setup_wdt(WDT_OFF);
     setup_ccp1(CCP_OFF);
     setup_low_volt_detect(FALSE);

     //Parâmetros dos Timers
     setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16); //Incremento em 210mS
     //setup_timer_1(T1_DISABLED); //Habilita o clock do rtcc
    setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_ENABLE_SOSC); //Habilita o clock do rtcc
    //setup_timer_1(T1_ENABLE_SOSC);
     setup_timer_2(T2_DIV_BY_4,249,5); //Incremento em 1.0mS
     setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
     setup_timer_4(T4_DISABLED,0,1);     
   
         
         
    //Parâmetros dos Display LCD
     lcd_init();
     
     //Parâmetros da Comunicação SPI
     setup_spi2(SPI_SS_DISABLED);
     
     //Parâmetros das Interrupções
     //enable_interrupts(INT_RTCC);   // Interrupção Serial
     //enable_interrupts(INT_TIMER0); // Incremento a cada 210 mS
     Parametro_Interrupcoes_RDA=0;
     Parametro_Interrupcoes_RDA2=0;
     enable_interrupts(INT_RDA);
     //enable_interrupts(INT_RDA2);
     enable_interrupts(GLOBAL);
     
     //Parâmetros das Portas Digitais     
     set_tris_b(0b10111100);
     set_tris_c(0b10111001);
     set_tris_d(0b10110100);
     set_tris_e(0b10000000);
     Make_All_Pins_Digital();
     
     //Parâmetros das Portas Analógicas
     setup_adc_ports(sAN0|VSS_VDD);
     setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
     set_adc_channel(0);
     delay_us(10);
     
     //Bloco de Flags das Interrupções
     Parametro_Interrupcoes_RDA=0;
     Parametro_Interrupcoes_RDA2=0;
     Parametro_Interrupcoes_TIMER0=0;
     
     //Bloco de LED das Interrupções
     output_low(LED_Alarme);
     output_low(LED_Serial);
     
     //Configuração do RTCC
   setup_rtc(RTC_ENABLE|RTC_OUTPUT_SOURCE,0);         //enables internal RTCC           
 
  //setup_oscillator(OSC_SOSC_ON);



#include <18F67K22.h>
#device adc=10


#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES VREGSLEEP                //Ultra low-power regulator is disabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES HSH                      //High speed Osc, high power 16MHz-25MHz
#FUSES NOPLLEN                  //4X HW PLL disabled, 4X PLL enabled in software
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV18                   //Brownout reset at 1.8V
#FUSES ZPBORM                   //Zero-Power BOR
#FUSES WDT_NOSLEEP              //Watch Dog Timer, disabled during SLEEP
#FUSES WDT1048576               //Watch Dog Timer uses 1:1048576 Postscale
#FUSES MSSPMSK7                 //MSSP uses 7 bit Masking mode
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES BBSIZ2K                  //2K words Boot Block size
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTC                   //Configuration registers not write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads



//LUCAS

//#FUSES RTCOSC_INT               //RTCC uses Internal 31KHz Oscillator as reference source
 
#FUSES SOSC_LOW   
#FUSES RTCOSC_T1

// FIM DE LUCAS
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, 4  Next
Page 1 of 4

 
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