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 CCS Technical Support

Use of RTC in PIC24 using PCD compiler
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Globellit



Joined: 11 Mar 2008
Posts: 6
Location: Sweden

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

PostPosted: Wed Jun 04, 2008 6:48 am     Reply with quote

Hi Ron,

I have also start to go ower to C30. I could get the RTCC to work with some asm code but I given up, as You told to many bugs in the compiler.
I know it will work with the C30 so I concentrate me to finnish the scematics for my Iridium application.

regards, robert
drdelphi



Joined: 22 Apr 2007
Posts: 22
Location: Romania

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Feb 15, 2009 4:00 am     Reply with quote

Thanks ronbreukers for the SOSC starting routine in asm.

I didn't manage to use any of setup_rtc, rtc_write or rtc_read, but this worked :

Code:

#word RCFGCAL = 0x0626
#word RTCVAL = 0x0624

[...]

setup_timer1(T1_EXTERNAL_RTC); // don't know if it is really necessary

// start secondary oscillator
#ASM
MOV #0x742,W1 //OSCCON L address
MOV.B #0x02,W0 //the SOSCEN bit is set
MOV #0x46,W2 //UNLOCK SEQUENCE 1
MOV #0x57,W3 //UNLOCK SEQUENCE 2
MOV.B W2,[W1] //Write the
MOV.B W3,[W1] //unlock sequence
MOV.B W0,[W1] //set the SOSCEN bit

// unlock sequence for setting RTCWREN bit in RCFGCAL register
push w7
push w8
disi #5
mov #0x55, w7
mov w7, 0x766
mov #0xAA, w8
mov w8, 0x766
bset 0x626, #13
pop w8
pop w7
#ENDASM

bit_set(RCFGCAL, 15); // enable RTC
bit_set(RCFGCAL, 8); // simulating
bit_set(RCFGCAL, 9); // rtc_write
RTCVAL = 0x0009; // year 2009
RTCVAL = 0x1231; // month 12, day 31
RTCVAL = 0x0623; // day of the week 6, hour 23
RTCVAL = 0x5950; // minute 59, second 50
bit_clear(RCFGCAL, 13); // unset RTCWREN

for (;;) {
  bit_set(RCFGCAL, 8); // simulate
  bit_set(RCFGCAL, 9); // rtc_read
  printf("%4LX %4LX %4LX %4LX\n\r", RTCVAL, RTCVAL, RTCVAL, RTCVAL);
  delay_ms(1000);
}


Good luck.
drdelphi



Joined: 22 Apr 2007
Posts: 22
Location: Romania

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Feb 15, 2009 4:14 am     Reply with quote

Not tested :

Code:
time_t my_rtc_read(void) {
  time_t res;
  unsigned int16 a, b, c;
  char tmp;

  bit_set(RCFGCAL, 8);
  bit_set(RCFGCAL, 9);

  res.tm_year = RTCVAL & 0xFF;
  a = RTCVAL;
  b = RTCVAL;
  c = RTCVAL;
 
  tmp = a & 0xFF;
  a >>= 8;
  res.tm_mday = (tmp >> 4) * 10 + (tmp & 0x0F);
  res.tm_mon = (a >> 4) * 10 + (a & 0x0F);

  tmp = b & 0xFF;
  b >>= 8;
  res.tm_hour = (tmp >> 4) * 10 + (tmp & 0x0F);
  res.tm_wday = (b >> 4) * 10 + (b & 0x0F);

  tmp = c & 0xFF;
  c >>= 8;
  res.tm_sec = (tmp >> 4) * 10 + (tmp & 0x0F);
  res.tm_min = (c >> 4) * 10 + (c & 0x0F);

  return(res);
}
nirayo



Joined: 23 Feb 2009
Posts: 10

View user's profile Send private message

the code for rtc_read
PostPosted: Wed Aug 19, 2009 1:57 am     Reply with quote

drdelphi wrote:
Not tested :

Code:
time_t my_rtc_read(void) {
  time_t res;
  unsigned int16 a, b, c;
  char tmp;

  bit_set(RCFGCAL, 8);
  bit_set(RCFGCAL, 9);

  res.tm_year = RTCVAL & 0xFF;
  a = RTCVAL;
  b = RTCVAL;
  c = RTCVAL;
 
  tmp = a & 0xFF;
  a >>= 8;
  res.tm_mday = (tmp >> 4) * 10 + (tmp & 0x0F);
  res.tm_mon = (a >> 4) * 10 + (a & 0x0F);

  tmp = b & 0xFF;
  b >>= 8;
  res.tm_hour = (tmp >> 4) * 10 + (tmp & 0x0F);
  res.tm_wday = (b >> 4) * 10 + (b & 0x0F);

  tmp = c & 0xFF;
  c >>= 8;
  res.tm_sec = (tmp >> 4) * 10 + (tmp & 0x0F);
  res.tm_min = (c >> 4) * 10 + (c & 0x0F);

  return(res);
}


You have to put the return field as a parameter, you cannot return struct from a function in c.
Other then that I don't know why not use the regular rtc_read(time_t*)
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

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

PostPosted: Wed Aug 19, 2009 6:16 am     Reply with quote

I think you *can* return a struct in C - since a struct is a known length at compile time, stack space will be used to hold the struct members. It might not be too efficient of course since the returned struct will probably be copied to another variable. Almost always better to have a pointer to a struct as a parameter.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Aug 20, 2009 3:12 am     Reply with quote

Yes, you can return a struct with PCD, but it's not placed on the stack rather in a shared static memory area, as all local variables with CCS C (and also function results in 8-bit-compilers). The immediate function result is a pointer to this area.
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

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

PostPosted: Thu Aug 20, 2009 7:26 am     Reply with quote

FvM, thank you for the clarification re the CCS implementation - as good as a stack (except when you want recursion!)
eoinoc



Joined: 30 Apr 2009
Posts: 16

View user's profile Send private message

PostPosted: Tue Oct 19, 2010 7:58 am     Reply with quote

Thanks for the code it seems to work for me also.

Just a quick query, the RTC time seems to be way off.
I'm losing 5 seconds in ten minutes, as far as I can see the calibration bits in RTCGCAL only adjust it by a few milliseconds over minutes.

Has anyone seen similar behaviour?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Oct 19, 2010 11:26 am     Reply with quote

A deviation in this order of magnitude can be expected if you are clocking the RTC by the internal RC oscillator rather than a watch crystal.
eoinoc



Joined: 30 Apr 2009
Posts: 16

View user's profile Send private message

PostPosted: Wed Oct 20, 2010 2:38 am     Reply with quote

Yes you are right ! The fuse was not being set correctly and was using LPRC instead of SOSC
eoinoc



Joined: 30 Apr 2009
Posts: 16

View user's profile Send private message

PostPosted: Fri Oct 22, 2010 6:32 am     Reply with quote

Hi again, I am now using an external 32.768 xtal between SOSC and SOSC1 and I am seeing funny behaviour.
I have verified that the 32.768k xtal is oscillating on the SOSC pins.

When i look at the RTCC output pin i am seeing a relatively high frequency square wave of about 100Hz.
Code:

void main(void)
{
int8 x,y;
int16 a,b,c,d;

bit_clear(trisb,15);
bit_clear(trisb,14);


#asm

mov #0x742,w1
mov.b #0x02,w0
mov #0x46,w2
mov #0x57,w3
mov.b w2,[w1]
mov.b w3,[w1]
mov.b w0,[w1]
bset oscconlow,#1

push w7   
 push w8
disi #5
mov #0x55, w7   
mov w7, 0x766     
mov #0xAA, w8       
mov w8, 0x766   
bset RCFGCAL, #13     
pop w8     
pop w7

#endasm

bit_set(RCFGCAL,15);
bit_set(RCFGCAL,10);
bit_set(padcfg,1);
bit_set(RCFGCAL,8);
bit_set(RCFGCAL,9);

RTCVAL=0X0010;// year
RTCVAL=0X1019;// month day
RTCVAL=0X0214;//day of week hour
RTCVAL=0X3600; //minutes seconds

bit_clear(RCFGCAL,13);
output_low(pin_b15);

   while(true)
   {
   bit_set(RCFGCAL,8);
   bit_set(RCFGCAL,9);
   a=rtcval;
   b=rtcval;
   c=rtcval;
   d=rtcval;

   x=make8(d,0);
   y=make8(d,1);
     if(bit_test(rcfgcal,11))
      output_high(pin_b15);
     else
      output_low(pin_b15);


    }
}



Any ideas why I see this behaviour ?
I also see the same behaviour when I poll the half second status bit.

My PIC is 24HJ32GP302
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Oct 22, 2010 9:48 am     Reply with quote

Did you connect capacitors to the SOSC pins?
eoinoc



Joined: 30 Apr 2009
Posts: 16

View user's profile Send private message

PostPosted: Sat Oct 23, 2010 8:28 am     Reply with quote

Yes adding load capacitance seems to have helped tried lots of different combinations.
These Watch Xtals seem to need a really special PCB layout.

On my breadboard here I cant make it give an output pulse 1 second high and one second low on the RTCC pin. Oh well I hope it will work on my PCB.
Now the seconds pin is oscillating at around 2hz
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 Previous  1, 2
Page 2 of 2

 
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