|
|
View previous topic :: View next topic |
Author |
Message |
ipq
Joined: 20 Aug 2015 Posts: 17
|
PIC18F24J11 secondary oscillator? |
Posted: Mon Sep 26, 2016 11:34 am |
|
|
Hello,
I'm trying to set up the MCU to work with a 32.768KHz secondary oscillator but when I read the rtcc, always get the same time. There's no increment.
How should I setup the timer1 in order to make the RTCC work even when sleeping?
If I use the following fuse, the RTCC works:
Code: |
#FUSES RTCOSC_INT //RTCC uses Internal 31KHz Oscillator as reference source |
The interruption routine works:
Code: |
#INT_RTC
void RTC_isr(void) {
printf("alarm!\n\r");
}
|
But if I try with an external crystal, I can't make it work...Any advice on how to setup timer1 to work in low power state?
My goal is to wake up the MCU with the RTCC alarm...
Thanks in advance! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 26, 2016 1:39 pm |
|
|
Did you look at this CCS example file ?
Quote: | c:\program files\picc\examples\ex_rtcc.c |
Here is a post that I did that shows how to use the RTCC to wake-up
from deep sleep:
http://www.ccsinfo.com/forum/viewtopic.php?t=45139&start=3
In that program, I use #asm code to enable deep sleep mode.
With a more modern version of the compiler, you may possibly be
able to use this line instead. I haven't tested it:
Code: | sleep(DEEP_SLEEP | WAKE_FROM_RTCC); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Sep 26, 2016 1:42 pm |
|
|
sleep(SLEEP_IDLE); |
|
|
ipq
Joined: 20 Aug 2015 Posts: 17
|
|
Posted: Tue Sep 27, 2016 12:44 am |
|
|
A couple of questions...
Is this the right sleep instruction: sleep(REG_LOW_POWER);
(I'm trying to go into sleep mode, not deep sleep mode)
One stupid question...Are you connecting the crystal to T1OSO and T1OSI???
Because I don't achieve to make the clock increment...
Thank you for your support.
P.S: By the way, the example in the PICC folder does not use interruptions to wake up the MCU, or at least the one I have:( |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Sep 28, 2016 2:58 am |
|
|
Lets take a deep breath and step back.
Have you verified that your RTC, is running from the Timer1 oscillator, and running correctly, before trying to go to sleep?.
Then, 'be aware'. It takes time to wake from a sleep. Your print, is very likely not to work properly, since the main oscillator needs a while to get back to speed.
Personally, if using an interrupt to wake from sleep, I don't use an interrupt handler. I make sure that 'GLOBAL' is disabled before going to sleep, which then means that the sleep only wakes, and doesn't call a handler at all.
Then use the setup_oscillator call to verify that the clock has reached operating speed, before trying to do anything like a print (not needed for anything that is not 'timing dependant', but must be done for things like print). This is why I like 'SLEEP_IDLE'. This cuts power down to a tiny fraction of 'running', but allows you to wake almost instantly, and do jobs involving timings (keeps just the oscillators themselves running and the peripherals). Look at section 4.4.1 in the data sheet. You can also switch 'down' to operate the CPU from the 32K oscillator, and then go to sleep, and do jobs at this low rate (again immediately) when you wake (SEC_IDLE mode). However with that caveat now given about the clock wake up, you should be able to work with something like:
Code: |
//Based on PCM_programmers code, but not using deep sleep
#include <18F24J11.h>
#fuses HS,NOWDT,RTCOSC_T1 // RTCC uses Timer1 osc with 32.768 KHz xtal
#use delay(crystal=20M) // PIC uses 20 MHz crystal
#use rs232(baud=9600, UART1, ERRORS)
//==============================================
void main(void)
{
rtc_time_t SetTime;
rtc_time_t AlarmTime;
setup_rtc(RTC_ENABLE,0);
// Set initial RTC date/time values in structure.
SetTime.tm_year = 0x11;
SetTime.tm_mon = 3;
SetTime.tm_mday = 9;
SetTime.tm_wday = 3;
SetTime.tm_hour = 0x11;
SetTime.tm_min = 0x47;
SetTime.tm_sec = 0;
// Write that structure into the RTC hardware registers
// inside the 18F26J11.
rtc_write(&SetTime);
// Set RTC alarm date/time.
AlarmTime.tm_year = 0x11;
AlarmTime.tm_mon = 3;
AlarmTime.tm_mday = 9;
AlarmTime.tm_wday = 3;
AlarmTime.tm_hour = 0x11;
AlarmTime.tm_min = 0x47;
AlarmTime.tm_sec = 5;
rtc_alarm_write(&AlarmTime);
while (TRUE)
{
setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_MINUTE, 0xFF);
//reset each time so count does not change
printf("Going to sleep.\n\r");
disable_interrupts(GLOBAL); //ensure a handler will not be called
clear_interrupts(INT_RTC); //RTC interrupt cleared
enable_interrupts(INT_RTC);
sleep(WAKE_FROM_RTCC);
//Ensures RTCWDIS is cleared
delay_cycles(1); //NOP
while (setup_oscillator(OSC_NORMAL)!=OSC_STATE_EXT_RUNNING)
delay_us(10); //wait for oscillator
printf("Woke-up from sleep ! \r\n");
}
}
|
Unlike PCM, since I wake from the sleep (rather than resetting), I have to then clear the RTC interrupt. From the deep sleep, this is not necessary.
I think this is close, but haven't got a chip to check. |
|
|
|
|
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
|