Menu ▾ ▴

Software Power ON/OFF using one switch.

5 days ago
3 days ago
  • Roger Jönsson

    Roger Jönsson - 5 days ago

    I have a project where I wanted to make it more like a real product with one combined Power ON/OFF button. As I couldn't find an example I came up with a simple solution of my own, for 18F (Q84). If there are similar examples then I wasted a few hours, but also learning something new :-).

    When power is connected, it immediately sends the chip to sleep, waiting for port pin Interrupt-on-Change.
    POWER ON: When the switch is pressed it starts my program
    POWER OFF: Next time the switch is pressed is resets the chip and goes to sleep (again waiting for new Powering on...)

    #CHIP 18F27Q84, 64
    #OPTION Explicit
    
    //Software POWER ON/OFF USING ONE SWITCH
    
    DIR PORTA.1 OUT //POWER Indicator
    DIR PORTC.0 IN //POWER switch (0 when pressed, 1 when released)
    
    //Immediate put to SLEEP
    //Waiting for POWER ON interrupt by PortC.0=0)
    IOCCN0=1    //Interrupt-on-Change (Negative Edge, Power switch = 0 = pressed)
    IOCIE=1     //activate global IOC interrupt
        IOCCF0=0 //Clear PortC.0 Interrupt-on-Change Flag before sleep
        Sleep
    IOCIE=0     //deactivate global IOC interrupt, since it may interfere with other interrupts.
    
    PortA.1=1       //=Power is ON
    wait 500 ms     //Time for releasing switch, to not trigger reset
    
    //Rest of code starts here
    
    Do
        //POWER OFF by pressing Power Switch
        If PortC.0=0 Then   
            wait 500 ms     //Time for releasing switch, to not trigger start
            PortA.1=0       //=Power goes OFF
            #asmraw reset
        end if
    Loop
    

    (edit) Clarification: The idea is to use as few components as possible. In this case only one switch is needed to add (if internal weak pullups is used).

     

    Last edit: Roger Jönsson 5 days ago
  • Fabrice Engel

    Fabrice Engel - 5 days ago

    Hi Roger,

    I had similar requirement few years ago, and I found and build together a Soft Power Swith using one pushbutton and dual mosfet and some discrete components.
    The pushbutton is connected to one pin of the microcontroller, and that pin is configured per default as input to read pushbutton preasure and configured as output as soon the power must be shut off.

    You can find all the documentation and associated source-code in the shared project Short Cut Probe on the Elektor Lab page just here:
    https://www.elektormagazine.fr/labs/short-circuit-probe

    See also schematic directly here in the post. The part of the code is managing a short press and a long pressure on the pushbutton, short press for one particular action and long press for powering off.

    How did you handled the power management? Also with Mosfets?

    // Procedure check if Power On / Normalize PushButton is pressed and how long (short press = normalize, long press = power off)
    // Check here to learn more about this software debouncing solution: https://www.ganssle.com/debouncing-pt2.htm
    
    Sub CheckSoftPwSw()
        PushPowerState = FnLSL(PushPowerState,1)                             // At each run, move the bits to the left (using mask technic to ensure debouncing of the pushbutton)
        PushPowerState = PushPowerState OR SoftPwSw                          // Merge bits values history state with actual pushbutton state  
        PushPowerState = PushPowerState OR 0xfe00                            // Mask the history state to be able to detect a stable state of 0    
        If OldstatePower = 0 then
           If PushPowerState = 0xff00 then                                   // Falling edge detected, start to count
              PushTimePower = 0                                              // Clear counter, first value, next value by raising edge 
              OldstatePower = 1                                              // Power pushbutton was pressed, set flag to avoid stop counter
           End if
        End if
        If OldstatePower = 1 then
           If PushPowerState = 0xfe01 then                                   // Detect raising edge
              OldstatePower = 0                                              // Clear detection flag
              If PushtimePower > 20 then                                     // Long push time or short push time, see in interrupt routine for incrementation ("<=" need 5 program steps as just "<")
                 Call PowerOff(63000,2,5,5)                                  // Force to power off (with 2 beep tones)
              Else
                 SetPowerButton = not(SetPowerButton)                        // Toggle pushbutton flag, flag will be managed in main routine
              End if
              PushtimePower = 0                                              // Ready for new press on the pushbutton
           End if
        End if
        Clrwdt                                                               // Clear watchdog
    End sub
    

    The counter PushtimePower is increased in the Interrupt routine:

    // Interrupt handler, keep as small as possible
    
    Sub Interrupt()    
        // Timer1 section - Buzzer sound square signal generation
        If PIR1.TMR1IF = 1 then
           Buzzer = not(Buzzer)                                              // Generate square signal to create a tone by the buzzer
           TMR1H = BuzzerTime_H                                              // BuzzerTime defines the frequency of the signal
           TMR1L = BuzzerTime                                                // High frequency tone near to 65000, low frequency tone near to 58000 according EB tests     
           PIR1.TMR1IF = 0                                                   // Clear Flag, enable interrupt again, best practice but already cleared by GCBASIC compiler
        End if                                                               
        // Timer2 section - Detect PushButton press time and general running clock
        If PIR1.TMR2IF = 1 then                                              // Timer2 Interrupt occur                  
           PushTimePower += 1                                                // Increment the counter of pushbutton press time
           EndTimeOut += 1                                                   // Increment the counter for initiating power off
           MuteControl += 1                                                  // Increment the counter for muting tone by non touching probes
           TMR2 = 106                                                        // Ready for 256- 106 = 150 cycles
           PIR1.TMR2IF = 0                                                   // Clear Flag, enable interrupt again, best practice, already cleared by GCBASIC compiler
           'LedG  = not(LedG)                                                // Blinking Green Led (test purpose, remove for normal operation)
        End if
    End Sub
    
     

    Last edit: Fabrice Engel 5 days ago
  • Roger Jönsson

    Roger Jönsson - 5 days ago

    My idea was to use as few components as possible. -Only one push button switch (if using weak pullups).
    When powered OFF the PIC it is reset and the PIC is in sleep mode (with very low current draw), so it behaves pretty much like a power cycle, starting the program with the one switch..

    In my real program I also have a long and short button press. The same button is used for power on, to select audio sample and a long press turning it "off" (reset and then sleep mode, ready to wake up on (a port pin going low) interrupt).

     
  • Fabrice Engel

    Fabrice Engel - 5 days ago

    Thank for your feedback. Ok, got it, so you basically use the sleep function to stop the operations and reduce power consumption.
    Are you also using a display or is your project without display?

     
  • Roger Jönsson

    Roger Jönsson - 5 days ago

    To reduce power consumption and to restart the program. The PIC is reset after each "Power off", so there should be no problem adding a display using GCbasic commands. The PIC will be initialised fresh at each start up, just like power cycling (If I understand it correctly. -To be tested).
    So far my program has no display, just regular LED.

    Interrupts handling the audio (or something else in my code) failed before deactivating global IOC interrupt directly after wake up. It now seems to work perfectly. I found that by trial and error. I tried to read up on the problem, but didn't manage to solve it other than hacking.

    My audio thingy will have an external DAC as option, I think it goes into power save automatically if digital signal stops. If not I will add an FET control it on/off.

     
    ❤️
    1
  • Fabrice Engel

    Fabrice Engel - 4 days ago

    Interressting approach Roger, thank for sharing.

     
  • Roger Jönsson

    Roger Jönsson - 3 days ago

    I simplified my routine for long press / short press as far as I could:
    Instead of doing multiple If-then tests, it was far easier to use a Do-Loop and Exit Do.

    Do //Main do-loop
        If PortC.0=0 Then   //Is button pressed?
            Do
                wait 200 ms 
                If PortC.0=1 then //= SHORT PRESS (button is released).
                    PortA.1=0  //Blink once
                    wait 200 ms 
                    PortA.1=1
                    Exit Do
    
                    Else  //=LONG PRESS (button is still pressed)
                    PortA.1=0       //Indicating "Power OFF"
                    Wait until PortC.0=1 //Wait until button is released, to prevent immediate start after resetting.
                    wait 100 ms //button debounce
                    #asmraw reset
                end if
            Loop
        end if
    Loop
    
     
    ❤️
    1

    Last edit: Roger Jönsson 3 days ago

Log in to post a comment.