Can't read RTCC registers in 18F87J93 ?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Can't read RTCC registers in 18F87J93 ?

Post by TonyR » Fri Aug 08, 2014 1:18 am

Good Morning All.

I need a RTCC and LCD display so chose 18F87J93 because its in the supported by SF list. I have a test program that compiles but I can't access the RTCC registers?

For example I expected to be able to read the "second" register.

Looking in the 18F87J93.bas I can't find any reference to the RTCC registers. Am I missing something?

Help appreciated'

Thanks,

Tony R.

Code: Select all

Device = 18F87J93                    
Clock = 4

Config OSC = INTOSC, RTCSOSC = INTOSCREF

Include "usart2.bas"  
Include "convert.bas"

OSCCON.4 = 0  ' set for 4MHZ clock from internal block so it will be same as crystal
OSCCON.5 = 1
OSCCON.6 = 1

USART2.SetBaudrate(br4800)

While true
USART2.Write(DecToStr(second), 13, 10)  ' <<<<<<<<<<<<<<<<<< SECOND is the name of an RTCC register but I get error here 
DelayMS(100)

Wend



TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: Can't read RTCC registers in 18F87J93 ?

Post by TonyR » Fri Aug 08, 2014 4:07 am

Ok forget that one, I discovered I can set a pointer and read the register RTCVALL and RTCVAH which theroretically contain seconds.

That works, I get values but they don't up date. I think I have tried every possible way of enabling and clocking the RTC but can't find a way that works?

Any advice appreciated.

Code: Select all

Device = 18F87J93                    
Clock = 8

Config OSC = INTOSC, RTCSOSC = INTOSCREF

Include "usart2.bas"  
Include "convert.bas"

Dim temp As Byte


OSCCON.4 = 1  ' set for 4MHZ clock from internal block so it will be same as crystal
OSCCON.5 = 1
OSCCON.6 = 1

OSCTUNE.7 = 0
 
RTCCFG.5 = 1  ' enable writing to RTCC registers by setting write enable  bit
RTCCFG.7 = 1  ' RTCC enabled
RTCCFG.5 = 0  ' disable writing to RTCC registers by resetting write enable  bit
RTCCFG.2 = 1  ' RTCC clock output enable  (dont know what this is ?)

USART2.SetBaudrate(br4800)

While true
  RTCCFG.0 = 0  ' set pointer to seconds, returns value that doesn't update
  RTCCFG.1 = 0
  USART2.Write(DecToStr(RTCVALL), 13, 10)       
  DelayMS(100)
Wend


bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Can't read RTCC registers in 18F87J93 ?

Post by bitfogav » Fri Aug 08, 2014 9:09 am

Hi..

Nathan put together a good module to work with the internal RTCC, have you checked this out?

http://www.sfcompiler.co.uk/wiki/pmwiki ... er.IntRTCC

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Re: Can't read RTCC registers in 18F87J93 ?

Post by RangerBob » Fri Aug 08, 2014 2:33 pm

Hi Tony,

The reason your Seconds field isn't updating is because you cannot just flip the WriteEnable bit (RTCCFG.5). You have to send an unlock sequence first (see 15.2.7 WRITE LOCK pg. 167 of the datasheet). You have to write $55, then $AA to EECON2 then the immediate next instruction can flip the RTCWREN bit. This is best done in raw assembler, and you should ensure that you are not interrupted during the sequence or else it fails. The result is returned in BCD format, and you need to read the register twice to ensure consistency in case you accidentally read it during a rollover.

The RTCC clock output enable " (dont know what this is ?) " allows you to turn on or off the RTCC output pin. This can be configured to be a 32kHz Clock output, a 1 Hz output, or an alarm match output.

The module I wrote works well and is tested in a production environment. The sample program may look complex, but that's only because it demonstrates pretty much every feature. At a basic level your program can be:

Code: Select all

Include "IntRTCC.bas"
Include "usart2.bas"
include "convert.bas"   ' For dec to str conversions

Dim Time As TTime      ' Create a structure to hold the time

USART2.SetBaudrate(br4800)

Time.Hour = 10         ' put in some default time to the Time structure
Time.Minute = 20
Time.Second = 30
IntRTCC.Write(Time)  ' write them into the RTCC registers

IntRTCC.RTCEnable(true)    ' start the RTCC running

While true
    IntRTCC.Read(Time)	    ' Read out

    USART2.Write(DecToStr(Time.Hour, 2),":",DecToStr(Time.Minute,2),":",DecToStr(Time.Second,2), " ", 13) ' Send via USART2
wend

I haven't tested on that particular device, but it all looks compatible. If you have any questions about it please feel free to ask.

Best Regards,

Rangerbob (Nathan)

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: Can't read RTCC registers in 18F87J93 ?

Post by TonyR » Fri Aug 08, 2014 7:52 pm

Thanks so much for advice bitfogav and Nathan.

It's 5.30am here, I got up early because I was going to go through your program Nathan line by line to find what I was doing wrong! One wonders why microchip wouldn't mention the unlock in the Rtcc section of the datasheet!

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: Can't read RTCC registers in 18F87J93 ?

Post by TonyR » Fri Aug 08, 2014 10:06 pm

Ok forget that one too ... again, it is!

Will try your library today and let you know how it goes, thanks Nathan,

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: Can't read RTCC registers in 18F87J93 ?

Post by TonyR » Sat Aug 09, 2014 2:06 am

It works, thanks heaps Nathan, so you can say your libraries mostly tested with 18F87J93.

Interestingly I got an ASM error the first time I compiled it - the assembler seems to have tried to assemble your comments // LOCK and // UNLOCK in the assembler part of the library. When I commented them out with ' instead of // it fixed it.

After that fix another error saying "ASM operator missing" appeared.

Installing the updated SF fixed both errors and now it works. Yay!

User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

Re: Can't read RTCC registers in 18F87J93 ?

Post by RangerBob » Mon Aug 11, 2014 8:21 am

Excellent, glad it was of some use!

Weird about the ASM comments bit. Never seen that. I'll change the WIKI from // to ' and move it out of that assembly section just to be on the safe side.

Rangerbob

Edit: Weirdly my source doesn't have those comments anyway. Perhaps I got carried away and added them to the wiki at some point without thinking about it. Doh! ;)

Post Reply