Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 16, 2024, 11:23:31 23:23


Login with username, password and session length


Pages: [1]
Print
Author Topic: Basics: PIC12F & low power operation (sleep mode)  (Read 5080 times)
0 Members and 1 Guest are viewing this topic.
movf_jmpz
Newbie
*
Offline Offline

Posts: 12

Thank You
-Given: 8
-Receive: 4


« on: July 08, 2013, 03:14:53 15:14 »

Hello,
I'm aware such basic topics aren't for many of us here, but maybe some beginners & hobbyists would found it useful.

Project description:
Suppose you wan't to make some kind of device that request execution of code periodically and for some quite small amount of time. In my example here it is model of street lights (just for the sake of simplicity).
There are many ways to complete this task, but I've chose 3 LEDs and small micro (PIC12F509).
Hardware isn't important here (direct drive of LED with uP output pin w/series resistor, single cell LiPo battery without voltage stabilizer).
To achieve lowest possible uP current usage one way is to use SLEEP command. It puts uP in sleep mode making it power consumption as low as about 0.1-0.2uA (according to datasheet). It is about 6000-10000 times lower than normal operation @ 4MHz.
In this particular example it doesn't make much sense, considering LEDs consuming about 20mA, but designing low power battery powered system (e.g. wireless sensor mesh) makes each uA draw important when you don't want to replace/recharge batteries each day.

Using SLEEP command forces code to be somewhat different from casual execution scheme (with loops/interrupts/etc).
SLEEP in this particular example is used to enter sleep mode after configuring watchdog (WDT) to reset uP each 72ms. (nominally 12f509's WDT timer expires each 18ms, but using prescaler it is possible to extend that period up to 2.3s).
After watchdog expires, processor core is resetted and forces execution "from the beginning of code memory".
This implies main() function to be considered as interrupt-like handler. It should checks the source of reset (WDT? power-on-reset? pin-on-change-reset?) and behave accordingly.

Here you've got the code for analysis:
Code:
#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */

// CONFIG
#pragma config OSC = IntRC      // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = ON         // Watchdog Timer Enable bit (WDT enabled)
#pragma config CP = OFF         // Code Protection bit (Code protection off)
#pragma config MCLRE = ON       // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)

/******************************************************************************/
/* Main Program                                                               */
/******************************************************************************/

void main(void)
{
    const char _iLits[4] = {0x01,0x03,0x04,0x02};
    const char _iLitt[4] = {100,28,100,56};
    char iLitIdx;
    char bTimed;
    char iBtnCnt;
    char iTicks;
   
    // setup OPTION as a first thing to do after each reset
    OPTION = 0b11001010;      /* GPWU off, GPPU off, WDT<-presc, presc 1:4 */
                        /* 18ms * 4 = 0.072s */
    // setup TRIS
    TRIS = 0x10;

    /* power-up or another kind of reset occured (wdt?) */
    if (STATUSbits.nPD)
    {
        /* after power-up init */
        bTimed = 1;
        iBtnCnt = 0;
        iTicks = 0;
        SLEEP();
    }

    /*
     * STATUSbits.TO = 0 -> WDT timeout occured
     * OPTIONbits.PSA = 1 -> prescaler to WDT
     * OPTIONbits.PS = 1-7 -> WDT presc 1:1 to 1:128
     */

    /* wake up*/

    /* check the button periodically every WDT wakeup */
    if (GPIObits.GP4)
    {
        /* button released */
        if (iBtnCnt>15)
        {
            /* was long hold? */
            /* time > threshold */
            bTimed = !bTimed;
        } else
            /* short press */
            if (iBtnCnt>2)
            {
                iLitIdx++;
            }
        iBtnCnt=0;
    } else
    {
        /* button pressed */
        iBtnCnt++;
        if (iBtnCnt>15)
        {
            GPIO = 0x07;
            while (!GPIObits.GP4)
            {
                CLRWDT(); /* prevent from resetting the CPU now */
            }
        }
    }

    /* increase Ticks ... */
    iTicks++;
    if (bTimed)
    {
        /* timed operation */
        if (iTicks>_iLitt[iLitIdx])
        {
            /* light expired, switch to next */
            iTicks=0x00;
            iLitIdx++;
        }
    }

    if (iLitIdx==4) iLitIdx = 0;
    GPIO = GPIO & 0x38;
    GPIO += _iLits[iLitIdx];

    /* go to sleep for a wdt period or wait for key*/
    SLEEP();
}


Logged
Ichan
Hero Member
*****
Offline Offline

Posts: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #1 on: July 08, 2013, 04:12:48 16:12 »

That is a good useful example.

How about a real application? A cat tag perhaps?  Cheesy

I am interested to know how long a 3v coin cell will last powering this kind of circuit.

-ichan
Logged

There is Gray, not only Black or White.
movf_jmpz
Newbie
*
Offline Offline

Posts: 12

Thank You
-Given: 8
-Receive: 4


« Reply #2 on: July 08, 2013, 05:29:12 17:29 »

Typical CR2032 coin 3V battery has about 220mAh.
Considering typical RFID frontend idle/sleep mode current at 1uA plus about 0.1uA for uP in sleep it gives about 220mAh/1.1uA = 200000h in sleep mode ideally. (it is about 22 years), but considering discharge curve of such battery and deration, i think that useable amount of time would be about 10 years Smiley
see datasheet (http://data.energizer.com/PDFs/cr2032.pdf) - there is discharge curve for 0.19mA draw at typical conditions. Battery would keep usable voltage for about 30 days, taking about 150x less current it should last about 15 years ideally (only sleep mode).

But why even considering battery powered tag ?
Tags are RF powered...

PS. Correction : Passive tags are RF powered, active tags have internal battery (and significantly higher usable transmission range)
« Last Edit: July 08, 2013, 08:22:13 20:22 by movf_jmpz » Logged
Ichan
Hero Member
*****
Offline Offline

Posts: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #3 on: July 08, 2013, 05:54:39 17:54 »

But why even considering battery powered tag ?
Tags are RF powered...

It just what comes to mind when typing, there is a discussion about cat to open her own door on the other thread.

Any suggestion for an application to play with?

-ichan
Logged

There is Gray, not only Black or White.
electrojit
Active Member
***
Offline Offline

Posts: 178

Thank You
-Given: 233
-Receive: 285


« Reply #4 on: July 09, 2013, 06:32:37 18:32 »

Nice information... wanted add just one thing...
PIC12F datasheet suggest that we need to tie up the unused pins to either ground or VCC an my personal experience says that that is not just suggestion it is mandatory to do that other wise the sleep current was not 0.1uA... so if you have any problems with high current consumed during sleep... try to ground all unused pins...

@Ichan: I did some project few years back for fancy small toy... which turns on single LED based on push button pressed for different intensity and duration using PCI10F and the manufacture did tested it using coin cell and told me that it works for aprrox 2 yrs...  they had made some excel file and had similar calculation what movf_jmpz has suggested...
Logged
h0nk
Senior Member
****
Offline Offline

Posts: 256

Thank You
-Given: 208
-Receive: 230



« Reply #5 on: July 09, 2013, 07:31:30 19:31 »

I have measured 4 uA supply current for a PIC16LF819 at 1,5 V VDD running from the internal RC Oscillator (approx. 30 kHz).
All ports in output mode and toggling.

I have seen a custom IR-remote controller "Wallis Universal", They are using PIC16F84 which is in sleep mode.
On a keypress the controller gets a reset via MCLR, scans the keymatrix and fires then the IR-LED.



Best Regards
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC