PIC18F27J13, any tricks to RTC?

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

PIC18F27J13, any tricks to RTC?

Post by zener »

I am using the 18F27J13 again. This time trying out the PWM's (working) and the Real Time Clock (not working). I set the config register to enable it and enable the output (whatever that is) and then I read the HALFSEC bit to look for it transitioning. It does not appear to be working. I tried to set the oscillator to INTRC but the compiler said it doesn't recognize CONFIG3L. Strange. My 32KHz crystal does not appear to be oscillating.

User avatar
opossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am

Re: PIC18F27J13, any tricks to RTC?

Post by opossum »

Typically you just have to enable TMR1 to use external xtal, and enable the RTCC.

Code: Select all

	T1CONbits.T1OSCEN = 1;				// Enable 32.768 kHz osc
	INTCONbits.GIE = 0;					// Enable RTC writes (so clock can be set)
	EECON2 = 0x55;
	EECON2 = 0xAA;
	RTCCFGbits.RTCWREN = 1;
	RTCCFGbits.RTCEN = 1;				// Enable RTC

Code: Select all

void SetTime(void)
{
	const uint8_t year =   0x10;
	const uint8_t month =  0x11;
	const uint8_t day =    0x11;
	const uint8_t dow =    0x05;
	const uint8_t hour =   0x23;
	const uint8_t minute = 0x20;
	const uint8_t second = 0x00;

	RTCCFGbits.RTCPTR0 = 1;
	RTCCFGbits.RTCPTR1 = 1;
	RTCVALL = year;		// Year
	RTCVALH = 0;		//
	RTCVALL = day;		// Day
	RTCVALH = month;	// Month
	RTCVALL = hour;		// Hour
	RTCVALH = dow;		// Week
	RTCVALL = second;	// Second
	RTCVALH = minute;	// Minute
	
}

void ShowTime(void)
{
	uint8_t yr, mo, da, wk, hr, mn, se;

	RTCCFGbits.RTCPTR0 = 1;
	RTCCFGbits.RTCPTR1 = 1;
	yr = RTCVALL;
	da = RTCVALH;
	da = RTCVALL;
	mo = RTCVALH;
	hr = RTCVALL;
	wk = RTCVALH;
	se = RTCVALL;
	mn = RTCVALH;

	printf((const rom char far *)"%02X.%02X.%02X %02X:%02x.%02x", yr, mo, da, hr, mn, se);
}

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: PIC18F27J13, any tricks to RTC?

Post by zener »

Thank you for the fast reply. I do have some questions though. Clearing INTCON bit 7 (GIE) disables interrupts. I don't see what that has to do with enabling writes to the RTC. Also, I am a little confused about the writes to EECON2, although I now see they are necessary. Also it is necessary to write individually to the bits of RTCCFG as you show. I was using:

RTCCFG = 0xEC; // Turn on and configure Real Time Clock

But that didn't work, even with the writes to EECON2 before it. Only your special code works. I am not using the GIE clear for now though.

I was able to get the oscillator going with the T1OSCEN bit! That is not so clear in the data sheet I think. The amplitude is about .7 volts PP at the crystal. I am guessing that is normal. My clock is running about 5% slow. I am using 22pF caps. Do you think that is too big? I know there is a cal register.

Also I would like to understand your second code box much better since I will likely need all that once I get the clock actually going. It appears you just read and write succesively to the same place but it increments the data automatically?

Thanks

User avatar
opossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am

Re: PIC18F27J13, any tricks to RTC?

Post by opossum »

Read section 17.2.7 (Write Lock), section 17.2.8 (Register Mapping) and note 3 on page 239.

If interrupts are not disabled, then the unlock may be interrupted and fail.

6 to 12 pf is typical for 32 kHz xtal. Some micros (TMS430 for example) need no external caps for 32 kHz xtal.

Try this to make sure T1OSC is used for RTCC: #pragma config RTCOSC = T1OSCREF

Working PIC RTCC example: datalogger

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: PIC18F27J13, any tricks to RTC?

Post by zener »

oPossum wrote:Try this to make sure T1OSC is used for RTCC: #pragma config RTCOSC = T1OSCREF
That was what I needed! Was running from the internal 31.125 KHZ RC
Thanks! You have saved me a lot of time.

User avatar
zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: PIC18F27J13, any tricks to RTC?

Post by zener »

Oh great Possum,

Is there a way to modify just one calendar parameter, for example just minutes, if I wanted to adjust the time? Or do I have to read them all then write them all, including the changed one?

Thanks

User avatar
opossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am

Re: PIC18F27J13, any tricks to RTC?

Post by opossum »

Code: Select all

   RTCCFGbits.RTCPTR0 = 0; // Set pointer to seconds/minutes
   RTCCFGbits.RTCPTR1 = 0;
   RTCVALH = 0;  // Set minutes

Code: Select all

   uint8_t min;
   RTCCFGbits.RTCPTR0 = 0; // Set pointer to seconds/minutes
   RTCCFGbits.RTCPTR1 = 0;
   min = RTCVALH;  // Get minutes

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”