0

I have PIC18F87J11 with MPLAB C18. I am trying to limit the consumption of battery by the device by forcing it to go to sleep. I noticed upon entering the sleep mode, timer0 stops counting up any further until awaken. Also the only method I know to wake up the device is when it receives a message from another node since I have a MRF89XA transceiver.

To put my device to sleep I just call it this function which is defined in the Compiler header file.

Sleep(); 

Q1: How do I wake up the device other than by receiving a message from some node? In the DataSheet it says to EXIT BY INTERRUPT or by EXIT BY RESET but how do you do that if there is no timer?

By reading the DataSheet its stated that Timer1 must be used to be able to monitor the time, is that really the case?

All I really want is to lower battery usage as much as possible and keep the timer working on sleep mode.

Thanks!

Ammar
  • 1,203
  • 5
  • 27
  • 64

2 Answers2

1

Yes, Timer1 is the only timer that can operate in Sleep mode. From datasheet:

Entering Sleep mode from any other mode does not require a clock switch. This is because no clocks are needed once the controller has entered Sleep. If the WDT is selected, the INTRC source will continue to operate. If the Timer1 oscillator is enabled, it will also continue to run.

To run a timer with minimal power consumption, use Sleep mode and Timer1.

If you wish to use other peripherals, like Timer0, while in power-managed mode, there's an alternative to Sleep mode called the Idle mode:

If the IDLEN bit is set to ‘1’ when a SLEEP instruction is executed, the peripherals will be clocked from the clock source selected using the SCS<1:0> bits; however, the CPU will not be clocked. The clock source status bits are not affected. Setting IDLEN and executing a SLEEP instruction provides a quick method of switching from a given Run mode to its corresponding Idle mode.

To set the IDLEN bit to 1, add OSCCON |= 0b10000000; somewhere before issuing Sleep();.

Obviously Idle mode will consume a bit more power than Sleep mode, since peripherals are left operating.

makes
  • 6,438
  • 3
  • 40
  • 58
  • You answered my question that I need timer1, but I don't fully understand your answer. Like what's the difference between timer0 and timer1? Which one is more accurate? Also how do you set IDLEN to '1' ? Like where exactly in the code ? These little things really confuse me, its not your fault, I don't have strong understanding about configuring pins. If you can explain a little bit further, that would be nice. Thanks! – Ammar Jul 16 '13 at 20:57
  • Your device has five timer modules. Both Timer0 and Timer1 are capable of 16-bit accuracy. Timer0 can be configured to operate in 8-bit or 16-bit mode. Timer1 is a 16-bit timer. Study the datasheet to learn about other differences. – makes Jul 16 '13 at 21:07
  • I have Timer0 working fine without sleep mode, if I was to implement timer1, what would be the main things to change in the code? – Ammar Jul 16 '13 at 21:11
  • To try Idle mode, try adding line `OSCCONbits.IDLEN = 1;` somewhere before `Sleep();`. – makes Jul 16 '13 at 21:16
  • Compare these tables in the datasheet to get an idea of the differences between the two timers: __13-1 Registers associated with Timer0__ and __14-2 Registers associated with Timer1__ – makes Jul 16 '13 at 21:21
  • 1
    I will do that and I will post my new problems (if any) on a new thread. Thanks for the help! – Ammar Jul 16 '13 at 21:27
  • here is the [thread](http://stackoverflow.com/questions/17728289/why-is-timer1-not-counting-up-on-pic18) related to timer1. – Ammar Jul 18 '13 at 17:11
0

I had this exact problem, but it was a minor oversight on my behalf. Timer1 can have/has a sync bit, called something like:

T1CONbits.nT1SYNC = 1; // Timer1 External Clock Input Synchronization Control bit.

This syncs the timer 1 the the main clock. As the main clock doesn't run in sleep mode, it cant sync and hence Timer1 does not operate. All i needed to do was ensure that I wasn't syncing Timer1 to the primary clock. configure it in the right sleep mode i.e. not deep sleep or idle or vBAT, and everything worked.

I was using MPLABx and XC8 with a PIC18F97j94