Author Topic: Soft latching on/off switch redux++  (Read 77 times)

0 Members and 1 Guest are viewing this topic.

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 4966
  • Country: gb
Soft latching on/off switch redux++
« on: September 18, 2020, 05:58:25 pm »
I was revisiting this a few weeks ago with a view to minimising parts count.

There's quite a reluctance to use MCUs, undoubtedly because there's a concern over sleep current draw.

But what if I were to tell you there are a few MCUs out there with <100pA current draw in sleep with a voltage range of 2V to 5V? Yes, PICOamps. That's 268,000 years on a CR2032.

At 2V, in sleep, the PIC10F20x devices sip only 27pA in SOT-236 and 35pA in DIP-8, and this seems to be reasonably consistent across a long date range of parts.

BOM:

1 PIC10F20x SOT23-6
1 PMN27UP P-Ch 5.7A 20v mosfet SOT23-6
1 10k resistor
1 push button switch
1 100nF cap

[attachimg=1]

Code: [Select]

// PIC10F206 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config WDTE = OFF       // Watchdog Timer (WDT disabled)
#pragma config CP = OFF         // Code Protect (Code protection off)
//#pragma config MCLRE = ON       // Master Clear Enable (GP3/MCLR pin function  is MCLR)
#pragma config MCLRE = OFF      // Master Clear Enable (GP3/MCLR pin fuction is digital I/O, MCLR internally tied to VDD)

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

#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#define _XTAL_FREQ 4000000

__persistent bool _b;


// 27pA @2V, 36pA @3.0V, 68pA @5V sleep
// 26.7uA @2V, 77.8uA @ 3.0V, 231uA @5V Button down
//

int main(void)
{
    TRIS=0;
   
//    ADCON0bits.ANS=0b00; // Digital IO, not analog
    OSCCALbits.FOSC4=0;
    CMCON0bits.CMPON=0;
    OPTION=0b00011111; // Wake on change enabled (bit 7), Global pull ups enabled GP0, GP1 & GP3 (bit 6), Disable T0CS (on GP2)(bit 5))
   
    if ((STATUS & 0b11111000) == 0b10010000) // See DS40001239F p37, Table 9-2
    {
        uint8_t u8NewState=GPIO;
        uint8_t u8OldState;
        uint16_t u16Count=0;
       
        // Debounce
        do
        {
            u8OldState=u8NewState;
            u8NewState=GPIO;
            if (((u8OldState ^ u8NewState) & 0b00001000)!=0)
            {
                u16Count=0;
            }
        } while (u16Count++<1000); // 1000 is about 31.8ms @ Fosc 4MHz
       
        if ((u8NewState & 0b00001000)==0)
        {
            if (_b)
            {
                _b=0;
                GPIObits.GP2=1;
            }
            else
            {
                _b=1;
                GPIObits.GP2=0;
            }
        }
    }
    else
    { // PIC's pOwer on reset
        GPIO=0b11111111;
       
        _b=0;
        GPIObits.GP2=1;
    }
   
    STATUSbits.GPWUF=0; // Clear wake up from pin change flag
    SLEEP();

    // Should never reach here, 10f2xx devices don't support PC+1 wake up
    while (1)
    {
        NOP();
    }
   
    return 0;
}

« Last Edit: September 18, 2020, 06:00:11 pm by Howardlong »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf