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

24F16KA102 and RTCC - used DS3231 instead.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

24F16KA102 and RTCC - used DS3231 instead.
PostPosted: Thu May 15, 2014 6:40 am     Reply with quote

Hi,

I'm having some trouble getting the RTCC on the 24F16KA102 to run.
Compiler version is PCWHD 4.135.
32K clock crystal and matching caps are connected at B4 and A4
The minimalist code is below. Heartbeat and debug message display fine. All I get back from reading the time is "00/00/2000 00:00:01".
My attempt to set the time isn't working and the clock is not running.

Am I perhaps missing something in the fuses?

Code:
#case
// 16KA102Test.c    Test components
//
// 2014.05.14 - Jurgen G Schmidt    www.jgscraft.com
//
// Compiler:      CCS PCWH 4.135
// Programmer:    PICkit2
//
//           PB1     MCLR/RA5 - 1    28 - Vdd +         
//           PB2          RA0 - 2    27 - Vss -         
//                        RA1 - 3    26 - RB15         HB LED
//                   PGD1/RB0 - 4    25 - RB14         
//                   PGC1/RB1 - 5    24 - RB13/SDO1    LCD     
//           DTS      RX1/RB2 - 6    23 - RB12         
//                        RB3 - 7    22 - RB11/SCK1    LCD
//                      - Vss - 8    21 - RB10/SDI1     
//                   OSCI/RA2 - 9    20 - RA6           
//                   OSCO/RA3 - 10   19 - RA7           
//          XTAL    SOSCI/RB4 - 11   18 - RB9/SDA1     
//          XTAL    SOSCO/RA4 - 12   17 - RB8/SCL1     
//                      + Vdd - 13   16 - RB7/TX1      DTS
//                        RB5 - 14   15 - RB6          JGSDBG
//                   
//-----------------------------------------------------------------------------
#include <24f16ka102.h>

#FUSES NOWDT          //No Watch Dog Timer
#FUSES NOPUT          //No Power Up Timer
#FUSES NOMCLR         //Engages weak pullup
#FUSES NOBROWNOUT     //No brownout reset

#FUSES ICSP1
#FUSES RTCCK_SOSC     // use SOSC as RTCC clock

#use delay(internal=32M)

// Serial debug data via SW. INVERT so can connect directly to legacy PC serial port
#use rs232( BAUD=9600, XMIT=PIN_B6, INVERT, STREAM=JGSDBG )
#use rs232( BAUD=2400, RCV=PIN_B2, XMIT=PIN_B7, STREAM=JGSDAT, ERRORS )

#define PIN_LED   PIN_B15               // heartbeat LED
#define PIN_DBG   PIN_B6                // SW Serial out

typedef unsigned int8   UINT8;
typedef unsigned int16  UINT16;

//-----------------------------------------------------------------------------

void main()
{
  rtc_time_t write_clock, read_clock;

  setup_adc_ports( NO_ANALOGS);         // ALWAYS, Always, always do this !!!
  delay_ms( 200 );                      // allow serial port to warm up :)
  fprintf(JGSDBG,"\r\n%s %s %s\r\n",__FILE__, __DATE__, __TIME__);

  setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 0x00); 

  write_clock.tm_year=0x14;
  write_clock.tm_mon=5;
  write_clock.tm_mday=0x15;
  write_clock.tm_wday=5;
  write_clock.tm_hour=0x12;
  write_clock.tm_min=0x15;
  write_clock.tm_sec=0;
 
  rtc_write(&write_clock);         //writes new clock setting to RTCC
 
  while( TRUE ){
    output_toggle( PIN_LED );
    rtc_read(&read_clock);        //reads clock value from RTCC
    fprintf(JGSDBG,"\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( 10000 );
  }
}
//-----------------------------------------------------------------------------
//------ end of 16KA102Test.c ------


Thanks for any help.
_________________
Jürgen
www.jgscraft.com


Last edited by jgschmidt on Sat May 17, 2014 8:56 am; edited 1 time in total
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri May 16, 2014 1:32 am     Reply with quote

Built-in rtcc functions of V4.135 are apparently faulty. At least the unlock function in setup_rtc() and rtc_write() uses a wrong NVMKEY address.

Manual unlock before calling the functions might work. Or use a newer PCD version, e.g. V4.140.
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

No luck.
PostPosted: Sat May 17, 2014 8:53 am     Reply with quote

After spending 2 days looking at the 24F reference, working with sample code from the XLP 16-bit kit and comparing CCS asm code with Microchip's, I've given up.

This is not enough reason to upgrade compiler versions yet - I just thought I'd try something new with a pretty well-established chip. I didn't get many hits on RTCC overall in the forum so maybe that's a hint not to use it. Sigh...

It's back to the trusty DS3231 and M41T81SMY6F.
_________________
Jürgen
www.jgscraft.com
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat May 17, 2014 12:21 pm     Reply with quote

The generated code of V4.135 is
Code:
....................   setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 0x00);   
044A:  MOV     #0,W1
044C:  MOV     #55,W2
044E:  MOV     W2,[W1]
0450:  MOV     #AA,W3
0452:  MOV     W3,[W1]
0454:  BSET.B  627.5
0456:  MOV     #742,W1
0458:  MOV     #46,W2
045A:  MOV     #57,W3
045C:  MOV.B   W2L,[W1]
045E:  MOV.B   W3L,[W1]
0460:  BSET.B  742.1
0462:  MOV     #8400,W4
0464:  MOV     W4,626
0466:  BSET.B  2FC.1
0468:  BCLR.B  627.5


The error is in the first instruction which should be
Quote:
044A: MOV #766,W1


Likewise in rtc_write().
temtronic



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

View user's profile Send private message

PostPosted: Sat May 17, 2014 2:11 pm     Reply with quote

There's a LOT to said about using 'tried and true' hardware and software !
The latest stuff doesn't necessarily be the old stuff !!
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat May 17, 2014 2:45 pm     Reply with quote

Yes, FvM, I noticed that. I tried to write my own C and ASM code to replace the CCS functions - no luck. Other forums also mention the possibility that the clock crystal is sensitive to board layout and so may not start on my proto board. Echoing T, I'm sticking with what is known to work.
_________________
Jürgen
www.jgscraft.com
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat May 17, 2014 3:19 pm     Reply with quote

Of course there can be other than software related problems. But not reading back the written RTC values refers to software issue.

As a workaround for the said built-in function bug, you can set RTCWREN manually before calling setup_rtc() or rtc_write()

Code:
#word NVMKEY = 0x766
#word RCFGCAL = 0x626
#bit RTCWREN = RCFGCAL.13
...
#asm
DISI #7
#endasm
NVMKEY = 0x55;
NVMKEY = 0xaa;
RTCWREN = 1;
setup_rtc(...);
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Sat May 17, 2014 8:10 pm     Reply with quote

Thanks for that - I'll try it out.
_________________
Jürgen
www.jgscraft.com
jgschmidt



Joined: 03 Dec 2008
Posts: 184
Location: Gresham, OR USA

View user's profile Send private message Send e-mail Visit poster's website

Some Progress...
PostPosted: Sat May 17, 2014 10:57 pm     Reply with quote

Thanks, that code worked - I can update the registers. And the clock is running. But I'm running into the hardware trouble mentioned by others...

The RTCC clock is running and when I check the SOSCO line with my scope it's right at 32767Hz. However, the clock is running about 2 to 3 times fast. (1 minute goes by in 25 seconds.) I don't have the necessary skills to resolve that. In any case, here's the currently running code for anyone else who wants to experiment:
Code:
#case
// 16KA102Test.c    Test components
//
//-----------------------------------------------------------------------------
#include <24f16ka102.h>

#FUSES NOOSCIO        // clock output in OSCO to verify clock settings with scope
#FUSES NOWDT          //No Watch Dog Timer
#FUSES NOPUT          //No Power Up Timer
#FUSES NOBROWNOUT     //No brownout reset
#FUSES ICSP1
#FUSES RTCCK_SOSC     // use SOSC as RTCC clock

#use delay(internal=32M)

// Serial debug data via SW. INVERT so can connect directly to legacy PC serial port
#use rs232( BAUD=9600, XMIT=PIN_B6, INVERT, STREAM=JGSDBG )

#define PIN_LED     PIN_A0             // heartbeat LED
#define PIN_DBG     PIN_B6              // SW Serial out

#word NVMKEY = 0x766
#word RCFGCAL = 0x626
#bit RTCWREN = RCFGCAL.13

//-----------------------------------------------------------------------------

void main()
{
  rtc_time_t write_clock, read_clock;

  setup_adc_ports( NO_ANALOGS);         // ALWAYS, Always, always do this !!!

  delay_ms( 200 );                      // allow serial port to warm up :)
  fprintf(JGSDBG,"\r\n%s %s %s\r\n",__FILE__, __DATE__, __TIME__);
  delay_ms( 200 );                      // allow serial port to warm up :)

  #asm
  DISI #7
  #endasm
  NVMKEY = 0x55;
  NVMKEY = 0xaa;
  RTCWREN = 1;
  setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 0);

  write_clock.tm_year=0x09;
  write_clock.tm_mon=0x08;
  write_clock.tm_mday=0x07;
  write_clock.tm_wday=0x05;
  write_clock.tm_hour=0x06;
  write_clock.tm_min=0x05;
  write_clock.tm_sec=4;
 
  #asm
  DISI #7
  #endasm
  NVMKEY = 0x55;
  NVMKEY = 0xaa;
  RTCWREN = 1;
  rtc_write(&write_clock);         //writes new clock setting to RTCC

  while( TRUE ){
    output_toggle( PIN_LED );
    rtc_read(&read_clock);        //reads clock value from RTCC
    fprintf(JGSDBG,"\r\n>>%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 );
  }
}
//------------------------------------------------------------------------------
//------ end of 16KA102Test.c ------

Cheers,
_________________
Jürgen
www.jgscraft.com
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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