Author Topic: PIC power consumption  (Read 7716 times)

0 Members and 1 Guest are viewing this topic.

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
PIC power consumption
« on: January 11, 2019, 10:55:04 pm »
I'm programming a microcontroller for a very low power application that needs to be able to run on a small battery for at least 6 months at a time. I'm using a PIC16F1703 (http://ww1.microchip.com/downloads/en/DeviceDoc/40001722C.pdf).
At the top of the datasheet it advertises 'extreme low power' features, suggesting that the current in normal operation can be as low as 8uA for a 32kHz clock (or around 25uA for the 500kHz internal clock I'm using). However, at the bottom of the datasheet, it says that the internal LDO consumes around 200uA alone, which is pretty much what I measured when I set up the circuit.
I'll be using a 500mAh battery, which means that at 250uA it will last around 2000 hours, or 80 days, which is shorter than I'd like.
Is there any way to get the power significantly lower?
 

Offline oPossum

  • Super Contributor
  • ***
  • Posts: 1415
  • Country: us
  • Very dangerous - may attack at any time
Re: PIC power consumption
« Reply #1 on: January 11, 2019, 11:06:18 pm »
Use a 16LF1703. It doesn't have the internal regulator.  L = Low Power
 
The following users thanked this post: JoeP

Offline Larry80

  • Contributor
  • Posts: 21
  • Country: fi
Re: PIC power consumption
« Reply #2 on: January 11, 2019, 11:07:25 pm »
For extreme low power operations you have to sleep the MCU as much as possible to obtain the standby current. Chip selection can play important role, ie. smaller 12F629 has mere 1nA advertised.
 
The following users thanked this post: JoeP

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #3 on: January 11, 2019, 11:09:36 pm »
I agree, do you need it running 24/7?
 

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
Re: PIC power consumption
« Reply #4 on: January 11, 2019, 11:20:26 pm »
Well, most of the time it's waiting for one of 6 buttons to be pressed, while also keeping track of time by incrementing a variable every time the TMR0 interrupt flag is a 1. The section in the datasheet about the sleep mode says that only in 'low power' sleep mode will the LDO be inactive, however the peripherals are very limited in this mode.
 

Offline Larry80

  • Contributor
  • Posts: 21
  • Country: fi
Re: PIC power consumption
« Reply #5 on: January 11, 2019, 11:28:09 pm »
Timekeeping is not a problem with sleep, ie. the low power 12F629 has wake up on TMR1 overflow, which is also tied to the 32khz xtal. Also you have to take a look at the interrupts, which can wake your device up automatically if button is pressed.
 
The following users thanked this post: JoeP

Offline KL27x

  • Super Contributor
  • ***
  • Posts: 4099
  • Country: us
Re: PIC power consumption
« Reply #6 on: January 11, 2019, 11:40:31 pm »
I've done a similar project. The timing did not have to be exact, though.

I used the watch dog timer to wake up the PIC every 4 seconds (IIRC, that is the longest interval you could get on the PIC). Then the PIC incremented a counter and went back to sleep until the WDT would wake it up, again.

Port change interrupt on the buttons would wake the PIC.

The code loop would turn off WDT, check what woke it, then either continue counting or doing w/e it was supposed to do when the buttons were pressed.

The timing would obviously only be as accurate as the WDT, which would plus or minus a bit.
 
The following users thanked this post: JoeP

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
Re: PIC power consumption
« Reply #7 on: January 11, 2019, 11:48:56 pm »
Unfortunately the timing needs to be very precise (no more than 30s loss a day). I could achieve this by writing a program to time 1 hour, then adjust how large the timer variable limit was to get really accurate results (<1s out after 1 hour). Presumably the TMR1 interrupt is more reliable than the watchdog timer, and I could use a similar calibration technique to make it more accurate, but it could be too much hassle in the end.
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: PIC power consumption
« Reply #8 on: January 12, 2019, 01:45:28 am »
They do make the newer 153xx series (16lf15325 for example), which adds DOZE and IDLE modes where you can keep peripheral clocks running, and also has PMD where you can turn off all unneeded peripherals.  Pin compatible and mostly the same, but do not have op-amps.

Quote
no more than 30s loss a day
You are starting with an internal clock rated at +-2%, although the datasheet feature summary says +-1% at calibration (what that means, I don't know as they are factory calibrated so not sure why the 2 different numbers). Assuming a 1% error- 60*60*24=86400 seconds in a day, 1% error is 864 seconds, and assuming only a 0.1% error, that is still 86 seconds. Maybe you need a little crystal to run timer1 for accurate time and also allow you to sleep for low power while keeping timer1 running. Maybe my math is wrong. Maybe you currently are within the 30s error, but I'm not sure I would want to find out that the time is drifting due to temperature 3 months in to its 'mission'.
 
The following users thanked this post: JoeP

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14445
  • Country: fr
Re: PIC power consumption
« Reply #9 on: January 12, 2019, 02:02:37 am »
Yes use the sleep mode as often as you can.

"A Low-Power Sleep mode can be selected by setting
the VREGPM bit of the VREGCON register. With this
bit set, the LDO and reference circuitry are placed in a
low-power state when the device is in Sleep."

So the LDO itself will draw a lot less power while in sleep mode.
 
The following users thanked this post: JoeP

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: PIC power consumption
« Reply #10 on: January 12, 2019, 07:23:43 am »
For such accurate timing a RTC seems like the way to go. I've used the DS3231, cheap probably clones from China that work very well, they are extremely accurate.
 
The following users thanked this post: Kilrah, JoeP

Offline Larry80

  • Contributor
  • Posts: 21
  • Country: fi
Re: PIC power consumption
« Reply #11 on: January 12, 2019, 08:42:54 am »
With 32.768khz quartz tied to TMR1 dedicated pins (designed just for that purpose) you can get very good accuracy and the easy timescale is a plus (exactly 2 seconds of overflow). Internal oscillator is bunch of garbage for any timekeeping, it will veer a lot by temperature, voltage and moons position.
 
The following users thanked this post: JoeP

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
Re: PIC power consumption
« Reply #12 on: January 12, 2019, 12:57:44 pm »
I might end up doing that. I've never used a crystal oscillator before; can I just connect the terminals to T1CKI and ground, set that as the TMR1 input and then it will work? Most of the online tutorials have OSC1 and OSC2 pins specifically for crystals, but the PIC16F1703 doesn't have this. Should I be looking for an oscillator component with Vdd, GND and CLKOUT terminals?
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #13 on: January 12, 2019, 01:41:43 pm »
I don't have the exact device (although I do have the otherwise functionally compatible F1705, F1709 and LF1709 in stock from the same family), but the 1703 doesn't seem to have the secondary oscillator documented in the same way that the 1704 & 1705 do: they also have a T1OSCEN bit lacking in the 1703.

What voltage is the OP running the device at? I agree with a previous comment about using the LF part if possible.

Known:
o Timing accuracy better than 30s/day accuracy (or < 350ppm or 0.035%)
o Buttons can be dealt with using interrupt on change
o PIC16F1703 does not have a secondary oscillator for TIMER1 (unlike 16F1704 & 1705 which do)

Unknown:
o Timing resolution
o Anything else...?
 

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
Re: PIC power consumption
« Reply #14 on: January 12, 2019, 02:07:41 pm »
I'm powering the pic with a 3.3V LDO from a 9V battery. I could probably save quite a bit of power if I used a switched power supply, but I don't have enough room.
I don't want to change to a different microcontroller.
The resolution doesn't need to be good whatsoever. 15s would be fine.
Presumably something like this would be fine for a crystal? https://www.ebay.co.uk/itm/5-x-Quartz-Crystal-Oscillator-Resonator-32-768Khz-6Mhz-8Mhz-11-0592Mhz-12Mhz-16M/232440510358?_trkparms=aid%3D555017%26algo%3DPL.CASSINI%26ao%3D1%26asc%3D55148%26meid%3Dc12ac3bc4a1c4729932a2220b3e3d512%26pid%3D100505%26rk%3D1%26rkt%3D1%26%26itm%3D232440510358&_trksid=p2045573.c100505.m3226
I just don't understand where the crystal goes in my circuit.
 

Offline Larry80

  • Contributor
  • Posts: 21
  • Country: fi
Re: PIC power consumption
« Reply #15 on: January 12, 2019, 03:50:22 pm »
Your specific PIC model does not support xtal at TMR1 inputs, so you would have to provide digital clock signal from somewhere else, but why stick with the wrong selection? Migrating the code between 12-18F devices is IMO very painless if the code is of reasonable length. ie. 1705 has and the feature you are looking for is T1OSCEN bit, their device selector does not seem to care about such things it seems.
« Last Edit: January 12, 2019, 04:10:46 pm by Larry80 »
 
The following users thanked this post: JoeP

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: PIC power consumption
« Reply #16 on: January 12, 2019, 03:57:09 pm »
The 1703 has no circuitry built in for any crystal, which means you have to provide a logic level clock to any external clock pin. There are oscillators available in a range of frequencies that are logic level (typically a 4 pin device with  pwr/gnd/out), and 32.768khz is a common one of course but selection may be a little limited if you can't deal with tiny parts (I briefly looked on Mouser). If you run timer1 off the ext osc (via T1CKI), it will count while in sleep. At the same time, you will have to be aware of what kind of current the ext clock requires as the goal may be somewhat defeated if its active current is not in the low ua's.

The 16LF153xx I referred to earlier does have the ability (like most) to use crystals, and in your case a simple 32.768Khz can crystal (on SOSC) and a couple caps would do the job. You also have an NCO which could be used as a counter (with sosc as clock source) - it is 20bits so you could sleep for up to 32 seconds before overflow/wakeup. Your LDO may be wasting a significant amount of current even when the micro is in sleep, so that has to be verified also.

Quote
I don't want to change to a different microcontroller.
I think the only think preventing me from switching from the 1703 to something more suited would be if I needed the op-amps. Everything else is better on the newer chips. Jumping to a different pic is easy.
 
The following users thanked this post: JoeP

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #17 on: January 12, 2019, 04:13:02 pm »
Can I ask what the contraints are over the choice of device?

e.g., cost, compatibility etc.

The (L)F1703 doesn't seem to support a crystal oscillator at all. You're limited to the internal oscillators or externally generated oscillators.

Running crystal oscillators are often significantly more efficient uA/MHz than the internal oscillators.

If you're running at 3.3v, you could use a LF part for example, although that on its own may not help much, but see the figures below I've just taken for (L)F1705 which is in the same family as the 1703, same package etc, but with crstal oscillators a a few more features. It is completely backwards compatible with your F1703 as far as I can see. The same would go for the slightly cheaper F1704.

191uA on the F part at 3.3V, VREGPM=1,     1MHz XT (XTAL)
195uA on the F part at 3.3V, VREGPM=1, 500kHz MFINTOSC
195uA on the F part at 3.3V, VREGPM=0, 500kHz MFINTOSC
  21uA on the F part at 3.3V, VREGPM=1,   32kHz LP (XTAL)
  19uA on the F part at 3.3V, VREGPM=1,   31kHz LFINTOSC
  18uA on the F part at 3.3V, VREGPM=1,   32kHz ECL
  52uA on the F part at 3.3V, VREGPM=1, 500kHz ECL
  13uA on the F part at 3.3V, VREGPM=0, 500kHz MFINTOSC (SLEEP)
180nA on the F part at 3.3V, VREGPM=1, 500kHz MFINTOSC (SLEEP)
180nA on the F part at 3.3V, VREGPM=1,   32kHz ECL (SLEEP)
178nA on the F part at 3.3V, VREGPM=1,   32kHz LP (XTAL, SLEEP)
178nA on the F part at 3.3V, VREGPM=1,     1MHz XT (XTAL, SLEEP)

126uA on the LF part at 3.3V,     1MHz XT
153uA on the LF part at 3.3V, 500kHz MFINTOSC
 7.1uA on the LF part at 3.3V,   32kHz LP (XTAL)
 5.5uA on the LF part at 3.3V,   31kHz LFINTOSC
 5.4uA on the LF part at 3.3V,   32kHz ECL
  42uA on the LF part at 3.3V, 500kHz ECL
  17nA on the LF part at 3.3V, 500kHz MFINTOSC (SLEEP)
  17nA on the LF part at 3.3V,   32kHz ECL (SLEEP)
  17nA on the LF part at 3.3V,     1MHz XT (XTAL, SLEEP)
  17nA on the LF part at 3.3V,   32kHz LP (XTAL, SLEEP)


Code: [Select]

// PIC16F1705 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
//#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
//#pragma config FOSC = ECL       // Oscillator Selection Bits (ECL, External Clock, Low Power Mode (0-0.5 MHz): device clock supplied to CLKIN pins)
#pragma config FOSC = LP        // Oscillator Selection Bits (LP Oscillator, Low-power crystal connected between OSC1 and OSC2 pins)
//#pragma config FOSC = XT        // Oscillator Selection Bits (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
//#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config CLKOUTEN = ON    // Clock Out Enable (CLKOUT function is enabled on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = OFF    // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config ZCDDIS = ON      // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR)
#pragma config PLLEN = OFF      // Phase Lock Loop enable (4x PLL is enabled when software sets the SPLLEN bit)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 32768

int main(void)
{
#if defined(_16F1703) || defined(_16F1704) || defined(_16F1705) || defined(_16F1707) || defined(_16F1708) || defined(_16F1709)
    VREGCONbits.VREGPM=1;
#endif   
    OSCCONbits.IRCF=0b0000; // 0b0000 => LFINTOSC
   
    LATA=0;
    LATC=0;
   
    TRISA=0;
    TRISC=0;
   
    ANSELA=0;
    ANSELC=0;
   
    LATCbits.LATC3=1;
    __delay_ms(1000);
    LATCbits.LATC3=0;
   
    while (1)
    {
        __delay_ms(1000);
        SLEEP();
        NOP();
    }
    return 0;
}

« Last Edit: January 12, 2019, 04:15:57 pm by Howardlong »
 
The following users thanked this post: JoeP

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
Re: PIC power consumption
« Reply #18 on: January 12, 2019, 05:32:18 pm »
Many thanks for all the replies - much appreciated.
In terms of device choice, the main reason for wanting to stay with the 1703 is because I already have them and I want to get this project done as quickly as possible (by Monday would have been nice), and also I didn't want any peripherals I wasn't aware of interfering with anything (if there are weird default modes). However, it looks as though I don't have any choice, so I'll order the 1705 and a 32.768 crystal.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #19 on: January 12, 2019, 06:33:41 pm »
I have about eight F1705 in stock and about three LF1705, both in DIP14. I am located in central London if that's any good to you, I can let you have a couple if that helps.
 
The following users thanked this post: JoeP

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #20 on: January 12, 2019, 07:15:21 pm »
This code that interrupts every 2s from TIMER1 takes 980nA on the F version and 1.07uA on the LF version, interesting that the LF device takes slightly more current. The
processor Fosc runs at 500kHz when out of sleep.
Code: [Select]

// PIC16F1705 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
//#pragma config FOSC = ECL       // Oscillator Selection Bits (ECL, External Clock, Low Power Mode (0-0.5 MHz): device clock supplied to CLKIN pins)
//#pragma config FOSC = LP        // Oscillator Selection Bits (LP Oscillator, Low-power crystal connected between OSC1 and OSC2 pins)
//#pragma config FOSC = XT        // Oscillator Selection Bits (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
//#pragma config CLKOUTEN = ON    // Clock Out Enable (CLKOUT function is enabled on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = OFF    // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config ZCDDIS = ON      // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR)
#pragma config PLLEN = OFF      // Phase Lock Loop enable (4x PLL is enabled when software sets the SPLLEN bit)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 500000

void __interrupt() ISR(void)
{
    if (PIR1bits.TMR1IF)
    {
        PIR1bits.TMR1IF=0;
        LATCbits.LATC3=!LATCbits.LATC3;
    }
}

int main(void)
{
#if defined(_16F1703) || defined(_16F1704) || defined(_16F1705) || defined(_16F1707) || defined(_16F1708) || defined(_16F1709)
    VREGCONbits.VREGPM=1;
#endif   
//    OSCCONbits.IRCF=0b0000; // 0b0000 => LFINTOSC
   
    // Set GPIOs to avoid floating inputs
    LATA=0;
    LATC=0;
   
    TRISA=0;
    TRISC=0;
   
    ANSELA=0;
    ANSELC=0;

    // So we know we've started, twiddle a GPIO
    LATCbits.LATC3=1;
    __delay_ms(1000);
    LATCbits.LATC3=0;
    __delay_ms(1000);   

    // Initilise TIMER1
    T1CONbits.T1OSCEN=1; // Turn on SOSC
    TMR1=0;
    T1CONbits.TMR1CS=0b10; // 0b10 => Use external clock source
    T1CONbits.T1CKPS=0b00; // 0b00 => div-by-1
    T1CONbits.nT1SYNC=1; // 1 => async
    PIR1bits.TMR1IF=0;
    PIE1bits.TMR1IE=1;
    T1CONbits.TMR1ON=1;

    // Enable interrupts
    INTCONbits.PEIE=1;
    INTCONbits.GIE=1;
   
    while (1)
    {
//
        SLEEP();
        LATCbits.LATC2=!LATCbits.LATC2;
        NOP();
    }
    return 0;
}

« Last Edit: January 12, 2019, 07:18:29 pm by Howardlong »
 
The following users thanked this post: JoeP

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #21 on: January 12, 2019, 08:04:19 pm »
12F629 has mere 1nA advertised.

I can confirm that it is definitely around that figure, but it becomes hard to measure precisely. However, all you can do functionally at that current is wait for a pin's state to change.
 

Offline JoePTopic starter

  • Contributor
  • Posts: 37
  • Country: gb
Re: PIC power consumption
« Reply #22 on: January 13, 2019, 11:24:54 am »
I have about eight F1705 in stock and about three LF1705, both in DIP14. I am located in central London if that's any good to you, I can let you have a couple if that helps.
Thanks for the offer. Unfortunately I'm a bit too far away (west midlands). It's not essential that it's done by Monday, it would just be more convenient.
This code that interrupts every 2s from TIMER1 takes 980nA on the F version and 1.07uA on the LF version, interesting that the LF device takes slightly more current. The
processor Fosc runs at 500kHz when out of sleep.
Excellent - 40 years on 1 battery should do it  :D.
Just a quick question regarding interrupts - am I right in saying that I don't need the interrupt handler, and the PIC will still wake up where it left off (starting with the line after SLEEP;), at which point I can check the interrupt flags? Something like this:
Code: [Select]
    while(1)
    {
       
        p = IOCAF; //interrupt on change flags
        IOCAF = 0x00;
        if(p>0)
        {
               doSomething(p);
        }
       
        if(PIR1bits.TMR1IF==1)
        {
            t++; //increment time
            if(t>=t0) //t0 is a value calibrated to represent 1 day
            {
                t=0;
            }
            PIR1bits.TMR1IF = 0;
        }
        SLEEP();
    }
Or do I need the ISR?
« Last Edit: January 13, 2019, 11:28:54 am by JoeP »
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #23 on: January 13, 2019, 03:51:36 pm »
Yes, that's about it. You should still enable PEIE, just disable GIE.

Of course the power consumption is very much determined by how much you do outside of sleep.

My oscillator was a bog standard 32.768kHz watch crystal (Epson C-001R 32.768K-A) across RA2 & RA3, and two 18pF caps to ground on a breadboard. It just worked, so I didn't bother optimising it any further. You may find that there are better values to improve accuracy and/or startup times, that's a whole rabbit hole in itself. Thankfully the LP oscillator isn't too "low power": the really low power 32kHz crystal oscillators can be hard to make work. Just make sure your board is clean of ionic contaminants like flux residue. My setup was on a simple solderless breadboard.

Here's an example with a falling edge IOC on RC0 and timer1 non-interrupt solution. Note here's no debouncing in here, I'll leave that for you.

Code: [Select]

// PIC16F1705 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
//#pragma config FOSC = ECL       // Oscillator Selection Bits (ECL, External Clock, Low Power Mode (0-0.5 MHz): device clock supplied to CLKIN pins)
//#pragma config FOSC = LP        // Oscillator Selection Bits (LP Oscillator, Low-power crystal connected between OSC1 and OSC2 pins)
//#pragma config FOSC = XT        // Oscillator Selection Bits (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
//#pragma config CLKOUTEN = ON    // Clock Out Enable (CLKOUT function is enabled on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = OFF    // Peripheral Pin Select one-way control (The PPSLOCK bit can be set and cleared repeatedly by software)
#pragma config ZCDDIS = ON      // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR)
#pragma config PLLEN = OFF      // Phase Lock Loop enable (4x PLL is enabled when software sets the SPLLEN bit)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 500000
#include <stdint.h>

int main(void)
{
#if defined(_16F1703) || defined(_16F1704) || defined(_16F1705) || defined(_16F1707) || defined(_16F1708) || defined(_16F1709)
    VREGCONbits.VREGPM=1;
#endif   
//    OSCCONbits.IRCF=0b0000; // 0b0000 => LFINTOSC
   
    // Set GPIOs to avoid floating inputs
    LATA=0;
    LATC=0;
   
    TRISA=0;
    TRISC=0;
   
    ANSELA=0;
    ANSELC=0;
   
    // IOC setup
    OPTION_REGbits.nWPUEN=0; // 0 => global enable of WPUx
    WPUCbits.WPUC0=1;
    TRISCbits.TRISC0=1;
    IOCCFbits.IOCCF0=0;
    IOCCNbits.IOCCN0=1; // Falling edge
    INTCONbits.IOCIF=0;
    INTCONbits.IOCIE=1;

    // So we know we've started, twiddle a GPIO
    LATCbits.LATC3=1;
    __delay_ms(1000);
    LATCbits.LATC3=0;
    __delay_ms(1000);   

    // Initilise TIMER1
    TMR1=0;
    T1CONbits.T1OSCEN=1; // Turn on SOSC
//    OSCCONbits.SCS=0b01; // 0b01 => Use Secondary oscillator used for CPU
    T1CONbits.TMR1CS=0b10; // 0b10 => Use external clock source
    T1CONbits.T1CKPS=0b00; // 0b00 => div-by-1
    T1CONbits.nT1SYNC=1; // 1 => async
    PIR1bits.TMR1IF=0;
    PIE1bits.TMR1IE=1;
    T1CONbits.TMR1ON=1;

    // Enable interrupts for exit-from-sleep only
    INTCONbits.PEIE=1;
   
    while (1)
    {
        SLEEP();
       
        // Deal with timer1
        if (PIR1bits.TMR1IF)
        {
            PIR1bits.TMR1IF=0;
            LATCbits.LATC2=!LATCbits.LATC2;
            LATCbits.LATC2=!LATCbits.LATC2;
            LATCbits.LATC2=!LATCbits.LATC2;
        }
       
        // Deal with all IOCs
        if (INTCONbits.IOCIF)
        {
            // RC0 IOC on falling edge
            if (IOCCFbits.IOCCF0)
            {
                IOCCFbits.IOCCF0=0;
                LATCbits.LATC3=!LATCbits.LATC3;
            }
            INTCONbits.IOCIF=0;
        }
    }
    return 0;
}

 
The following users thanked this post: JoeP, radio879

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5317
  • Country: gb
Re: PIC power consumption
« Reply #24 on: January 13, 2019, 11:50:58 pm »
 
The following users thanked this post: JoeP


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf