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

RTC PIC18F24J50

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



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

RTC PIC18F24J50
PostPosted: Fri Nov 08, 2019 12:36 pm     Reply with quote

Dear All,

I am trying to trigger an event by using the built in hardware RTC in my PIC18F24J50.

I am using this code to trigger an alarm (at this stage to light up and LED on PinA3) but for some reason, this is not happening:

Code:

#include <main_boq.h>
   //RTCC
   rtc_time_t write_clock, read_clock;

void SetupRTC() {

   
   
}



void WriteTime()
{
   write_clock.tm_year=19;
   write_clock.tm_mon=11;
   write_clock.tm_mday=8;
   write_clock.tm_wday=5;
   write_clock.tm_hour=18;
   write_clock.tm_min=30;
   write_clock.tm_sec=0;
   
   rtc_write(&write_clock);
}

void WriteAlarm()
{
   write_clock.tm_year=19;
   write_clock.tm_mon=11;
   write_clock.tm_mday=8;
   write_clock.tm_wday=5;
   write_clock.tm_hour=18;
   write_clock.tm_min=30;
   write_clock.tm_sec=6;
   
   setup_rtc_alarm(RTC_ALARM_ENABLE | RTC_CHIME_DISABLE, RTC_ALARM_SECOND, 0x01);
   rtc_alarm_write(&write_clock);
}

#INT_RTC
void RTCINT()
{
   output_high(PIN_A3);
   
}

void main()
  {
   
   WriteTime();
      delay_ms(30);
      WriteAlarm();
      setup_rtc(RTC_ENABLE, 0);
     
   while(TRUE)
   {
     
   }
   
   
  }


I am using the PIC18F24J50 and crystal of 20MHz. I double checked the wiring and everything is good. The alarm is supposed to work after 6 seconds of initializing.

This is just a test....
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Fri Nov 08, 2019 1:52 pm     Reply with quote

Read the manual for 'setup_rtc':
Quote:

Configures the Real Time Clock and Calendar module. The module requires an external 32.768 kHz clock crystal for operation.

Do you have the 32K crystal?.

If not you can feed the RTC off the internal clock (but low accuracy). Look at
the option 'RTC_INTERNAL'.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 08, 2019 2:51 pm     Reply with quote

You have an interrupt routine, but you are missing:
Code:

enable_interrupts(INT_RTC);
enable_interrupts(GLOBAL);
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Sun Nov 10, 2019 2:56 am     Reply with quote

Dear All,

Yes, I have the crystal as well. I am not understanding maybe you can help me better with some simple explanation. What is the Alarm masking? What I understood so far is that the alarm data and the actual time data is being compared. If I decide to execute an interrupt when the second matches (irrelevant the month, day, hours and minutes), the INT_RTC is being executed. right?
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Sun Nov 10, 2019 3:28 am     Reply with quote

On the alarm masking, everything _below_ the mask must match.
So the mask would have to be set to seconds for the second match to work.

Have you proved your clock is actually working?. Can you set, and read the
time?.
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Tue Nov 12, 2019 6:33 pm     Reply with quote

Dear All,

I continued with my program. Just to clarify, I connected the 32.768kHz on the PIC pins 11 and 12 of the PIC18F24J50.

I arranged the program to be minimalist:

Code:
#include <main_boq.h>
   //RTCC
   rtc_time_t wizardTempTime;

#INT_RTC
void  RTC_isr(void)
{
      output_toggle(PIN_B2);
}

void main()
{
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);      //13.1 ms overflow

   //RTCC
   setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 0);
   wizardTempTime.tm_year = 19;
   wizardTempTime.tm_mon = 10;
   wizardTempTime.tm_mday = 26;
   wizardTempTime.tm_wday = 6;
   wizardTempTime.tm_hour = 18;
   wizardTempTime.tm_min = 41;
   wizardTempTime.tm_sec = 30;
   rtc_write(&wizardTempTime);
   setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 2);
   wizardTempTime.tm_year = 19;
   wizardTempTime.tm_mon = 10;
   wizardTempTime.tm_mday = 26;
   wizardTempTime.tm_wday = 6;
   wizardTempTime.tm_hour = 18;
   wizardTempTime.tm_min = 41;
   wizardTempTime.tm_sec = 35;
   rtc_alarm_write(&wizardTempTime);


   enable_interrupts(INT_RTC);
   enable_interrupts(GLOBAL);

   while(TRUE)
   {
      //TODO: User Code
   }

}


My first issue is that when i compile the program an error is popping up with the message: Error 12 CPUDIV1 T1_CLK_OUT

when I check on the device file 18F24J50.h this constant (T1_CLK_OUT) is not listed. I replace it with T1_FOSC and it compiled. I tried to look it on the Manual but nothing is being referred to this constant.

Another issue is that according to my prohram te LED must toggle about 5 seconds when I power up the device. For some reason, it is being toggled immediately. What I am missing? Than it toggled again after exactly 1 minute 5 second.
temtronic



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

View user's profile Send private message

PostPosted: Tue Nov 12, 2019 6:48 pm     Reply with quote

q1: What value are the caps on pins 11,12 to gnd ? Typically they are 10-12pfd.

a2: Normally you should clear any interrupts before the 'global enable' to provide a 'clean' startup of your program. In your case it is possible the RTCC has set the interrupt.
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Tue Nov 12, 2019 7:27 pm     Reply with quote

Dear temtronic,

I used the 22pf capacitors.

What i would like to verify if the setting of the rtc is good or not since the wizard is give me constants and when i compile it the same compilet is giving me error.

I will check the clear_interrupts(); before enable the interrupts
temtronic



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

View user's profile Send private message

PostPosted: Tue Nov 12, 2019 8:45 pm     Reply with quote

Have a look at the datasheet, pretty sure Microchip says 12pfd. If you use bigger ones, the clock will run a little slower, so 1 second will be longer.

I NEVER use the 'Wizard' as it was created by another programmer, who uses HIS values for defaults which aren't what I consider should be default values.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 13, 2019 3:58 am     Reply with quote

I've used 22pf caps for the RTC crystal on a J11 and it still ran. http://www.ccsinfo.com/forum/viewtopic.php?t=45139&start=3
temtronic



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

View user's profile Send private message

PostPosted: Wed Nov 13, 2019 6:43 am     Reply with quote

Hay PCM P , that's good to know especially since I can't read them itty bitty numbers on parts without a X5 lens these days, sigh.
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 2:03 pm     Reply with quote

Dear All,

I continued to work on this problem but now I am stuck and do not know where...
I am using the PIC18F24J50 pic with 20MHz crystal. I would like to make a delay after start up by loading an alarm just after 5 seconds and setup the RTC Alarm as RTC_ALARM_SECOND but for some reason nothing is happening after 5 seconds. I also clear the interrupt and the first problem was tackled.


Code:
#include <mainrtc.h>
   //RTCC
   rtc_time_t wizardTempTime;

#INT_RTC
void  RTC_isr(void)
{
      output_toggle(PIN_A3);
}

void main()
{
   //setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);      //2.0 s overflow

   //RTCC
   setup_rtc(RTC_ENABLE | RTC_OUTPUT_SECONDS, 0);
   wizardTempTime.tm_year = 19;
   wizardTempTime.tm_mon = 11;
   wizardTempTime.tm_mday = 10;
   wizardTempTime.tm_wday = 0;
   wizardTempTime.tm_hour = 8;
   wizardTempTime.tm_min = 15;
   wizardTempTime.tm_sec = 0;
   rtc_write(&wizardTempTime);
   setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_SECOND, 1);
   wizardTempTime.tm_year = 19;
   wizardTempTime.tm_mon = 11;
   wizardTempTime.tm_mday = 10;
   wizardTempTime.tm_wday = 0;
   wizardTempTime.tm_hour = 8;
   wizardTempTime.tm_min = 15;
   wizardTempTime.tm_sec = 5;
   rtc_alarm_write(&wizardTempTime);

   output_low(PIN_A3);
   clear_interrupt(INT_RTC);
   enable_interrupts(INT_RTC);
   enable_interrupts(GLOBAL);

   while(TRUE)
   {
      restart_wdt();
   }

}
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Fri Nov 15, 2019 12:44 am     Reply with quote

Dear all,

Regarding timer0, i would like to use an external oscillator because i need a very accurate timings. Now the PIC has only one pin RA4, do you think that a circuit like this is ok or you propose another one?

http://www.bowdenshobbycircuits.info/oscilate.gif
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 15, 2019 10:25 am     Reply with quote

Post the contents of these files:
Code:
main_boq.h

mainrtc.h
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