View previous topic :: View next topic |
Author |
Message |
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
RTCC |
Posted: Sun Nov 09, 2014 7:26 pm |
|
|
Hi everybody. I'm relatively new to PIC microcontroller and am having trouble with the internal RTCC on a PIC24Hj128GP502. I have the program EX_RTCC.c working on the pic but it's counting seconds too fast. I am using a 20MHz crystal with no PLL. Any thoughts would be greatly appreciated. I will post the code if you would like to see it. Thanks _________________ Liam Hanmore |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Sun Nov 09, 2014 7:43 pm |
|
|
Thank you for your fast reply!! I have test the speed it's running at in the while(true) loop by putting a pin high and low and measuring the high time on the scope. Which is 20MHz! I've also tried google with little info about the RTCC program and no info about this problem. _________________ Liam Hanmore |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
Posted: Sun Nov 09, 2014 8:14 pm |
|
|
I assume you using the external 32.768Khz xtal? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19588
|
|
Posted: Mon Nov 10, 2014 1:33 am |
|
|
The pin toggle speed in a loop has nothing to do with the RTCC.
However if the pin is toggling at 20MHz, then your chip _is_ using the PLL.
A processor running at 20Mhz, on this family, executes 10MIPS. A toggle loop takes about 5 instructions to control the pin each way, and loop round. So on a 20MHz master clock, I'd only expect the pin to toggle at about 2Mhz.
Your fuse settings are probably wrong.
Generate a _minimum_ program, showing your fuses, and the code you are using to access the RTCC (use the supplied library, and don't include this). Post this. |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Mon Nov 10, 2014 3:31 pm |
|
|
Ya I have a 32.768Khz xtal on the SOSCO and SOSC1 pins with 18pF load caps to ground. According to one of my friends he said to count the high time of the output_high() instruction on the scope and multiply by 4 to get the oscillator frequency. I did that and the high time was approx 5MHz therefore the frequency is 20MHz (i'm unsure if this is a proper calculation to use). As for the code to setup, run and read the RTCC it came from the example program EX_RTCC.c so i will leave out the two functions and only post what is needed.
Code: | #include <24hj128Gp502.h>
#fuses PR,HS,NOWDT, NOPROTECT
#use delay(crystal = 20M)
#use rs232(baud = 9600, xmit = PIN_b5, rcv = PIN_b6, bits=8)
#pin_select U1TX = PIN_b5 //Set up PPS so Tx on B5
#pin_select U1RX = PIN_b6 //Rx on B6
int8 get_number()
{
//from example
}
void set_clock(rtc_time_t &date_time)
{
//from example
}
void main()
{
set_tris_b(0x0200); //0B0000001000000000, rb6 I/P
rtc_time_t write_clock, read_clock;
setup_rtc(RTC_ENABLE,0); //enables internal RTCC
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
//printf("\r%02u/%02u/20%02u %02u:%02u:%02u",read_clock.tm_mon,read_clock.tm_mday,read_clock.tm_year,read_clock.tm_hour,read_clock.tm_min,read_clock.tm_sec);
delay_ms(250);
}
} |
_________________ Liam Hanmore |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
Posted: Mon Nov 10, 2014 9:25 pm |
|
|
This code works for me.
Code: |
/******************************************************************************
Unlock and Enable Secondary Osc
*******************************************************************************/
void ensecosc()
{
#word OSCCON = 0x742
//
#asm asis
// ;OSCCONL(Low byte) Unlock Sequence
mov #0x742, w1 //OSCCONL,
mov.b #0x02, w0
mov #0x46, w2
mov #0x57, w3
mov.b w2, [w1]
mov.b w3, [w1]
; Enable Sec Osc
mov.b w0, [w1]
#endasm
}
//
//-----------------------------------------------------------------------//
void main(void)
{
ensecosc();
//
rtc_time_t write_clock, read_clock;
setup_adc_ports( NO_ANALOGS);
printf("\r\n%s %s %s\r\n",__FILE__, __DATE__, __TIME__);
setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 0x00);
write_clock.tm_year=14;
write_clock.tm_mon=11;
write_clock.tm_mday=10;
write_clock.tm_wday=1;
write_clock.tm_hour=22;
write_clock.tm_min=30;
write_clock.tm_sec=0;
rtc_write(&write_clock); //writes new clock setting to RTCC
while( TRUE ){
rtc_read(&read_clock); //reads clock value from RTCC
printf("\r%02u/%02u/20%02u %02u:%02u:%02u",
read_clock.tm_mon,read_clock.tm_mday,read_clock.tm_year,read_clock.tm_hour,read_clock.tm_min,read_clock.tm_sec);
delay_ms( 1000 );
}
}
|
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Tue Nov 11, 2014 2:41 am |
|
|
Thanks dyeatman and Ttelmah for taking the time to help , I will give it a go after college and post the results. _________________ Liam Hanmore |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Tue Nov 11, 2014 9:24 am |
|
|
I tried the code above given by dyeatman and every second it outputs the time to the PC but every 3 seconds or so it skips a second. For example it updates as follows; 0, 1, 2, 3, 5, 6, 7, 8, 10... when I decrease the delay at the end it's similar to the program I used above. Thanks _________________ Liam Hanmore |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
Posted: Tue Nov 11, 2014 9:51 am |
|
|
The delay is there to slow down the screen updates and nothing more.
It has nothing to do with the RTCC. The reason it skips a second has to do
with the 1 second delay versus the RTCC time. Nothing is wrong.
In my case I checked the RTCC over several minutes and it was just slightly
off which means it needed to be calibrated using the calibration registers. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19588
|
|
Posted: Tue Nov 11, 2014 12:37 pm |
|
|
This is a classic example of 'beating'.
Do a web search on 'beating between two frequencies', and then understand why if the RTCC updates every second, and you look at it every second, you _will_ see occasional skips in the display.
Nothing wrong, just a 'fact of life'. |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Tue Nov 11, 2014 2:35 pm |
|
|
I know that dyeatman, if it wasn't there it would print to the PC screen as fast as the R232 would let it. Other than that the time is still incrementing to fast.
What PIC, fuses and external high frequency oscillator are you using. I have tried both programs in this thread and have got similar results so i don't know what to do!! _________________ Liam Hanmore |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
Posted: Tue Nov 11, 2014 2:52 pm |
|
|
Over a continuous 3 minutes how many seconds is it off?
As TTelmah said earlier the main clock speed has nothing to do with the
RTCC. The RTCC runs off its' own 32Khz xtal. Let's concentrate on that. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Tue Nov 11, 2014 2:57 pm |
|
|
For every two minutes the RTCC counts up, 2 minutes would have passed by on my stop watch!! _________________ Liam Hanmore |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
Posted: Tue Nov 11, 2014 3:02 pm |
|
|
I must be missing something here or mis-understanding what you are saying.
OK, one more time, exactly how many minutes/seconds does it take on your
stop watch when two minutes is counted by the RTCC? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Liam_85
Joined: 09 Nov 2014 Posts: 26 Location: Ireland
|
|
Posted: Tue Nov 11, 2014 3:21 pm |
|
|
Sorry if I confused you dyeatman i meant to say three minutes not two!!
Anyways,
Stop watch count = 40 sec
RTCC count = 1 min
Stop watch count = 1 min 20 sec
RTCC count = 2 min
Stop watch count = 2 min
RTCC count = 3 min
Therefore, the RTCC is counting 20 seconds per minute faster than it should be!! _________________ Liam Hanmore |
|
|
|