pic16f88 sleep mode - Page 1
Forum section: electronics / beginners

author: gatoulisss
author_rank: Contributor
timestamp: October 26, 2015, 12:33:56 am
content: hello everyone,im using an pic16f88 microcontroller to build an security alert system witch is powered with 9v battery the problem is that after 3-4 days that my system is working the battery is going off...after a little search ifind out about the sleep mode but because im new to this i dont know how to use it.any help? how i can wake and sleep my processor? can i wake it up when a button is pressed or something like this happens?thank you very much!

author: Maxlor
author_rank: Frequent Contributor
timestamp: October 26, 2015, 03:05:50 am
content: To save power with this MCU, you can do two things: run off the internal oscillator and slow it right down to 31.25kHz. The second is sleeping. You can then use the watchdog to wake up the MCU. If it triggers while the MCU is sleeping, it'll just effect a wake up, not a reset.And yes, wakeup on button press is possible also, if that button is connected to pin RB0. You can configure that pin to trigger the INT0 interrupt.Btw, the PIC16F88 is a rather old chip. Newer ones use less power...

author: mariush
author_rank: Super Contributor
timestamp: October 26, 2015, 03:38:38 am
content: I would also add... since the microcontroller is running on 5v, I can only assume you're using a 7805 linear regulator to obtain 5v from the 9v battery. Regulators like 7805 will need about 1.5-2v above the output voltage so as soon as your battery drops below about 6.5v, so will the output voltage.If the datasheet for pic16f88 is correct (http://www.marutsu.co.jp/contents/shop/marutsu/datasheet/PIC16F88.pdf) then as soon as the input voltage is less than 4v it will stop working, so when battery is at 5.5v-6v you're done.Also, it's worth mentioning that linear regulators work by dissipating the voltage difference as heat so if your microcontroller uses 5mA, then you'll actually use about 5.5mA from the 9v battery (i include the regulator's power consumption)... that's about 9v x 0.0055 = 0.0495 watts. With a switching regulator with about 85-90% efficiency, your micro+regulator will only consume about 5v x 0.005A x 100/85 = 0.02925 watts, which is only about 3.25mA drawn from the 9v battery when close to full charge. This would make the battery very happy.They may be a bit scary but switching regulators make sense for such applications, and they're not that hard or expensive to implement.You may also want to look into more modern pic microcontrollers, which can run at low frequencies at voltages as low as 1.8v without problems. This would allow you to replace the 9v battery with a couple of AAA batteries or a lithium ion battery or some other power source, basically giving you more battery capacity. 9v batteries just don't have that much capacity in the first place.

author: Ian.M
author_rank: Super Contributor
timestamp: October 26, 2015, 05:58:08 am
content: Swap in a PIC16LF88 which runs down to 2.0V, and power it from 3 1.5V alkalines in series.Also the PIC16(L)F88 has IOC on pins RB4-RB7 which can all be used to wake it from sleep.

author: KL27x
author_rank: Super Contributor
timestamp: October 26, 2015, 06:34:10 am
content: You must refer to the datasheet.Sleep is achieved with the SLEEP instruction. But you have to make sure you can wake the PIC back up. Any interrupt that can be triggered during sleep can wake a PIC up. Edge interrupt, IOC, comparator, WDT.For starters, you must enable global interrupts. And then you must enable w/e interrupt you are going to use to wake the PIC, whether IOC (interrupt on change) or the interrupt on edge (usually on the counter pin). You will have to refer to the datasheet, because there may be multiple special registers that have to be set to turn on the interrupt.To use interrupts, you have to service them. This can get complicated, if you haven't used them before. Before you sleep, you have to make sure the appropriate interrupt is enabled, so that the PIC can be woken. And you also have to make sure the interrupt flag has been cleared, else the PIC will likely just wake right back up, immediately. (In PIC, the interrupt flag is usually always set by that action, whether the interrupt is enabled or not). Upon wakeup, the ISR will be triggered, and you must service the interrupt in your interrupt service routine. And finally, you must make sure to avoid a read-modify-write error in the case of a IOC interrupt. You can avoid this by doing a read of the port before you clear the IOC interrupt flag. Check datasheet for more info.Super important:Now, just in case your program is over 1 page long, be sure the first command in your ISR is a pagesel $. I don't know why, but some of the PICs that even have automatic context saving still don't freaking handle this, automatically.If using just this one interrupt for wakeup, then your ISR doesn't need to be fancy. Just disable the interrupt in the ISR and then RETFIE. Making sure to enable the ISR and clear the flag before SLEEP. But if you want to do it "proper", your ISR should check to make sure the interrupt is enabled and also check that the flag is set before servicing that interrupt. Because if you are ever using more than one interrupt, you will have to do this to see which one must be serviced. When using just one interrupt, you automatically know both these conditions have been met.

author: albert22
author_rank: Regular Contributor
timestamp: October 27, 2015, 03:47:24 am
content: The datasheet is a good starting point but you will get more info in the Reference manual. You can find it in the product page.http://ww1.microchip.com/downloads/en/DeviceDoc/01146B_chapter%202.pdfAlong with some app notes an other info.For low power applications the following guide is a must read:Pic Microcontroller Low Power Tips n Trickshttp://ww1.microchip.com/downloads/en/DeviceDoc/01146B_chapter%202.pdfAs mentioned in the other posts there are many ways to wake up the 16f88 from sleep. IMO using the WDT is not very flexible.I used interrupt on change (portB) and timer2 in some applications.To achieve very low power you need to slow down the clock and carefully configure the IO ports and their pullups/pulldowns.IIRC in my application the 16f88 needed only 10uA while keeping the time and date with a 32khz crystal at the OSC2, getting out of the sleep mode once or twice per second on overflow from timer2. And only going into fast clock if necessary. Beside the microcontroller there were other ICs that where turned off before going into sleep mode. Everything was powered from a 9v battery and a low quiescent current 5V linear regulator.

author: gatoulisss
author_rank: Contributor
timestamp: October 27, 2015, 03:40:27 pm
content: i think that also pic16f88 can run 2v (min) to 5.5v (max) if the datasheet is correct so is the same with the pic16lf88yes im using the lm7805 regulator to take the 5v from the battery also im very new to this and i dont know how exactly i can do most of the think you mentionfor example if i want the microcontroller to be sleeping until one of the sensors become from 0 to 1 how i can do this? (im using mikroC)

author: Ian.M
author_rank: Super Contributor
timestamp: October 27, 2015, 04:05:04 pm
content: The PIC16F88 is not reliable below 4V. It *MAY* work, but certain instructions may fail to execute properly. The lower the voltage, and the higher the clock speed, the greater the chance of your code crashing. See datasheet fig 18-1 for 'F88 and fig 18-2 for 'LF88 (just before section 18.1) for permitted voltages and clock frequencies.INTCON.GIE (the global interrupt enable) does *NOT* have to be set (enabled) to wake up from sleep. The individual interrupt enable for the chosen wakeup interrupt source *MUST* be set, as well as INTCON.PEIE for any source that doesn't have its flags in INTCON. See fig 15-7 and section 15.13. The advantage of not enabling GIE is that after wakeup, execution continues immediately after the SLEEP instruction, instead of calling the ISR, simplifying your code. Rem,ember all possible enabled wakeup interrupt sources *MUST* have their interrupt flags cleared to enter sleep successfully. The system clock stops in sleep so either monitor the crystal, or if using the internal oscillator, enable Fosc/4 output on the Osc2 pin and monitor that to confirm it has successfully entered sleep mode.To enter sleep mode using MikroC, use:asm{SLEEP}; // enables sleep mode

author: lapm
author_rank: Frequent Contributor
timestamp: October 27, 2015, 04:06:47 pm
content: Quote from: gatoulisss on October 27, 2015, 03:40:27 pmSince you are doing battery powered device, you need to learn also how to measure current drawn. 7805 can draw several mA on its own... At least my junkbox 7805 regulators seems to draw anywhere between 4 to 6 mA depending on individual regulator. When were talking microcontroller that can draw 1-2 mA, were are talking massive loses in regulator.To get microcontroller go down 2 Volts, you need special L-version of it. its tested and specified for low voltage use...

author: gatoulisss
author_rank: Contributor
timestamp: October 28, 2015, 01:00:36 pm
content: Quote from: lapm on October 27, 2015, 04:06:47 pmis there any other regulator i can use? this one needs to capacitors and the regulator there is something else so simple to do the job?Quote from: Ian.M on October 27, 2015, 04:05:04 pmso for example in this code which is a sensor test how i can use sleep mode and how i can wake up when the sensor change from 1 to 0?Code:void main() {osccon=0x60;ansel=0;TRISB.F7 = 0; //buzzer outputTRISA.F0 = 1; // one of the sensorsPORTB.F7 = 0;for(;;){if (PORTA.F0==0) {delay_ms(3000); //wait some time before th alertwhile(PORTA.F0==0){PORTB.F7 = 1; //alert}}else {PORTB.F7 = 0;}}}thank you very much!!

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2015, 02:16:29 pm
content: That's difficult as all the digital pins that can generate wakeup events are on Port B, but your input is on RA0. It could be done using a comparator, but the increased current consumption due to having the comparator and Vref modules enabled would almost certainly make it not worth doing.

author: gatoulisss
author_rank: Contributor
timestamp: October 28, 2015, 02:31:38 pm
content: i want to understand how to use the sleep mode, the inputs and the outputs is changeable i use this only to give you how my code is look like

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2015, 04:46:39 pm
content: I don't code in MikroC, so you will have to translate the following description into compilable code.First you need to move the sensor to a digital pin capable of waking from sleep, eg RB0/INT. Change the code to use RB0, and test it.Then you need to add code to the initialisation to set the INT0IE flag to enable INT and to clear INTEDG so it triggers on the falling edge (see datasheet section 15.10.1).Inside your for( ; ; ) loop, you need to clear the INT0IF flag and immediately enter SLEEP mode. It will stay in sleep mode until the INT pin goes from 1 to 0.

author: 0b01010011
author_rank: Regular Contributor
timestamp: October 29, 2015, 02:30:36 am
content: I did a kids toy project for my son a few years ago using an old PIC16F88 which used sleep mode.I could not get the IOC to wake it up though - could have been a s/w issue but I have got it working on other PICs before and after with no problem - the only way I could wake it up was via the external interrupt (INT) pin.So maybe move your trigger to this and also look at your PSU arrangement with the 9v battery.I used a 4.5v battery pack for the kids toy (just some flashing lights) and the toy still has the original batteries - though of course my son only used the toy for 2 seconds and then has been bored with it ever since!

author: gatoulisss
author_rank: Contributor
timestamp: October 30, 2015, 12:49:08 am
content: Quote from: 0b01010011 on October 29, 2015, 02:30:36 amso can you help me with the code? how i can sleep and wake up from external interupt in mikro c??