benoitstjean
Joined: 30 Oct 2007 Posts: 577 Location: Ottawa, Ontario, Canada
|
PIC24EP512GP806 - Timer interrupt while sleeping |
Posted: Wed Apr 30, 2025 7:28 pm |
|
|
Device: PIC24EP512GP806
Compiler: 5.116
Good evening,
I never attempted this but I'd like to put my entire device to sleep but keep timer 1 interrupt active. Is this possible? If so, then what happens with the main() code where the interrupt is really handled (that's how I do it anyhow...)?
Not sure how you guys do it but in my timer 1 ISR, really, all I have are a bunch of flags that are checked and if set, increase a counter and exit. That's it. So in this timer ISR, I have like 20 different counters that I use mainly for delays and timeouts. This way, the ISR is entered and exited as quickly as possible and leaves the main do the hard work where I have a simple while loop that checks the count value of the counters and when they reach a certain value, execute the code for that counter.
Example:
Code: |
#INT_TIMER1
void TIMER1_isr( void )
{
if( Counter_ABC.IsActive == TRUE )
{
Counter_ABC.Count ++;
}
if( Counter_DEF.IsActive == TRUE )
{
Counter_DEF.Count ++;
}
if( Delay_123.IsActive == TRUE )
{
Delay_123.Count ++;
}
... and so on
}
main()
{
while( 1 )
{
if( Counter_ABC.Count == count_value_x )
{
// Do stuff here
}
if( Counter_DEF.Count == count_value_y )
{
// Do stuff here
}
if( Delay_123.Count == count_value_z )
{
// Do stuff here
}
}
} |
The reason I'm asking is that currently, the device is tied to a LoRa module and when all is running, it draws around 42mA @ 12V and when the entire device goes to sleep, it drops down to 320nA.... but this is the entire circuit, including the MCU and the only way to wake it up is through the accelerometer that will generate an interrupt if it detects motion.
I tried setting just the LoRa module to sleep but the current draw drops only to around 22mA. That's still far from the 320nA....
So what is the best approach for what I want to accomplish, that is, put LoRa and mostly everything on the MCU to sleep except for timer 1 that will stll interrupt and run code every once in a while? Ultimately, all I want is for the timer to wake-up the entire circuit, including the LoRa module, contact the remote end then go back to sleep and repeat every 3-4 minutes.... or get woken-up by the accelerometer.
Thanks,
Ben |
|
benoitstjean
Joined: 30 Oct 2007 Posts: 577 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Apr 30, 2025 7:39 pm |
|
|
UPDATE: I guess the only way is to use SLEEP_IDLE because I am using setup_timer1( TMR_INTERNAL | TMR_DIV_BY_1 ); and don't have an external oscillator?
Using SLEEP_IDLE, it drops in half from 22mA to around 11-12mA. This is pretty decent.... but feel free to provide more suggestions if you think I can take it down even lower.
Thanks again!
Ben |
|