View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
RTC PIC18F24J50 |
Posted: Fri Nov 08, 2019 12:36 pm |
|
|
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
|
|
Posted: Fri Nov 08, 2019 1:52 pm |
|
|
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
|
|
Posted: Fri Nov 08, 2019 2:51 pm |
|
|
You have an interrupt routine, but you are missing:
Code: |
enable_interrupts(INT_RTC);
enable_interrupts(GLOBAL); |
|
|
|
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
|
Posted: Sun Nov 10, 2019 2:56 am |
|
|
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
|
|
Posted: Sun Nov 10, 2019 3:28 am |
|
|
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
|
|
Posted: Tue Nov 12, 2019 6:33 pm |
|
|
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
|
|
Posted: Tue Nov 12, 2019 6:48 pm |
|
|
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
|
|
Posted: Tue Nov 12, 2019 7:27 pm |
|
|
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
|
|
Posted: Tue Nov 12, 2019 8:45 pm |
|
|
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
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Nov 13, 2019 6:43 am |
|
|
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
|
|
Posted: Thu Nov 14, 2019 2:03 pm |
|
|
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
|
|
Posted: Fri Nov 15, 2019 12:44 am |
|
|
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
|
|
Posted: Fri Nov 15, 2019 10:25 am |
|
|
Post the contents of these files:
Code: | main_boq.h
mainrtc.h |
|
|
|
|