View previous topic :: View next topic |
Author |
Message |
respected
Joined: 16 May 2006 Posts: 95
|
PIC18F26J50 Deep Sleep Problem |
Posted: Wed Sep 09, 2020 8:28 am |
|
|
Anyone using the deep sleep feature? The MCU switches to Deep sleep mode but then I can't wake it up
In the datasheet it said it could be awakened with RTC alarm, but I couldn't run it.
This is my code
Code: | #include <18F26J50.h>
//#device ADC=10
#fuses INTRC_PLL,NOWDT,PLL2,CPUDIV2,RTCOSC_T1
#use delay(clock=24M)
rtc_time_t time;
void main(void)
{
setup_oscillator(OSC_PLL_ON);
setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 );
setup_rtc(RTC_ENABLE , 0);
time.tm_year = 20;
time.tm_mon = 5;
time.tm_mday = 21;
time.tm_wday = 4;
time.tm_hour = 16;
time.tm_min = 30;
time.tm_sec = 0;
rtc_write(&time);
setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_SECONDS, 0);
time.tm_year = 20;
time.tm_mon = 5;
time.tm_mday = 21;
time.tm_wday = 4;
time.tm_hour = 16;
time.tm_min = 30;
time.tm_sec = 0;
rtc_alarm_write(&time);
clear_interrupt(INT_RTC);
enable_interrupts(INT_RTC);
enable_interrupts(GLOBAL);
while (TRUE)
{
output_low(PIN_B0); //not used
output_low(PIN_B1); //not used
output_low(PIN_B6); //not used
output_low(PIN_B7); //not used
sleep(DEEP_SLEEP);
delay_cycles(1); //Instruction pre-fetched - NOP
output_high(PIN_B3);
delay_ms(200);
output_low(PIN_B3);
delay_ms(100);
clear_interrupt(INT_RTC);
}//while
}//main |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Sep 09, 2020 10:21 am |
|
|
The default clock source for the RTCC module is an external 32K
oscillator. I can't see where you are switching it to use the T1 source?.
On the external oscillator it won't say running in deep sleep mode. |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Wed Sep 09, 2020 10:47 am |
|
|
Ttelmah wrote: |
I can't see where you are switching it to use the T1 source? |
what do you mean? I don't understand
Code: | setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 ); |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Sep 09, 2020 10:49 am |
|
|
That sets up timer 1, but your RTCC is not being set to use this.... |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Wed Sep 09, 2020 2:10 pm |
|
|
How can i use?
I use RTCOSC_T1, setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 );
Isn't it enough for this ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Sep 10, 2020 12:53 am |
|
|
OK. Yes. I did not see this fuse.
The chips I've used have this setting in software, and it has to be done
as part of the RTC setup.
Series of problems.
You must _never_ enable an interrupt without having
a handler present for it. This will result in the chip crashing or hanging.
You don't need to enable this interrupt at all. Get rid of these lines.
Second problem the alarm mask. This is a repeated cause of many issues.
It is completely 'non intuitive'. As you have it configured, the alarm will
only happen once, when the alarm time matches the clock time to
the nearest ten seconds. To have the RTC alarm repeat every ten seconds
requires the ALARMRPT value to be set. Setting it to 0, as you have, means
the alarm will not repeat...
The waking from deep sleep with the RTC is setup with:
sleep(DEEP_SLEEP | WAKE_FROM_RTCC); |
|
|
respected
Joined: 16 May 2006 Posts: 95
|
|
Posted: Thu Sep 10, 2020 6:33 am |
|
|
I tried. But unfortunately the result is the same.
Code: | #include <18F26J50.h>
//#device ADC=10
#fuses INTRC_PLL,NOWDT,PLL2,CPUDIV2,RTCOSC_T1
#use delay(clock=24M)
rtc_time_t time;
//!#INT_RTC
//!void RTC_isr(void)
//!{
//!
//!}
void main(void)
{
setup_oscillator(OSC_PLL_ON);
setup_timer_1(T1_EXTERNAL| T1_DIV_BY_1 );
setup_rtc(RTC_ENABLE , 0);
time.tm_year = 20;
time.tm_mon = 5;
time.tm_mday = 21;
time.tm_wday = 4;
time.tm_hour = 16;
time.tm_min = 30;
time.tm_sec = 0;
rtc_write(&time);
setup_rtc_alarm(RTC_ALARM_ENABLE, RTC_ALARM_10_SECONDS, 10);
time.tm_year = 20;
time.tm_mon = 5;
time.tm_mday = 21;
time.tm_wday = 4;
time.tm_hour = 16;
time.tm_min = 30;
time.tm_sec = 10;
rtc_alarm_write(&time);
//! clear_interrupt(INT_RTC);
//! enable_interrupts(INT_RTC);
//! enable_interrupts(GLOBAL);
while (TRUE)
{
output_high(PIN_B2);
delay_ms(200);
output_low(PIN_B2);
delay_ms(100);
output_low(PIN_B0); //not used
output_low(PIN_B1); //not used
output_low(PIN_B6); //not used
output_low(PIN_B7); //not used
sleep(DEEP_SLEEP | WAKE_FROM_RTCC);
delay_cycles(1); //Instruction pre-fetched - NOP
output_high(PIN_B3);
delay_ms(200);
output_low(PIN_B3);
delay_ms(100);
}//while
}//main |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Sep 10, 2020 7:37 am |
|
|
I think you are missing a key point.
Deep sleep, is not like normal sleep. The chip will _not_ continue executing
code after this. It restarts. You have to test 'restart_cause' to find that
the chip has waked up from deep sleep mode, rather than being reset.
Quote: |
When a wake event occurs in Deep Sleep mode (by
MCLR Reset, RTCC alarm, INT0 interrupt, ULPWU or
DSWDT), the device will exit Deep Sleep mode and
perform a Power-on Reset (POR). When the device is
released from Reset, code execution will resume at the
device’s Reset vector.
|
Basically the chip powers off. The RAM is probably lost, the registers are
lost. All that remains running is the selected peripheral, and a small
amount of dedicated RAM (DSGPR0 & 1).
The chip is never going to get to your delay code after the sleep.
It's going to reset.... |
|
|
|