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...)
(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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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.htmSubCheckSoftPwSw()PushPowerState=FnLSL(PushPowerState,1)// At each run, move the bits to the left (using mask technic to ensure debouncing of the pushbutton)PushPowerState=PushPowerStateORSoftPwSw// Merge bits values history state with actual pushbutton state PushPowerState=PushPowerStateOR0xfe00// Mask the history state to be able to detect a stable state of 0 IfOldstatePower=0thenIfPushPowerState=0xff00then// Falling edge detected, start to countPushTimePower=0// Clear counter, first value, next value by raising edge OldstatePower=1// Power pushbutton was pressed, set flag to avoid stop counterEndifEndifIfOldstatePower=1thenIfPushPowerState=0xfe01then// Detect raising edgeOldstatePower=0// Clear detection flagIfPushtimePower>20then// Long push time or short push time, see in interrupt routine for incrementation ("<=" need 5 program steps as just "<")CallPowerOff(63000,2,5,5)// Force to power off (with 2 beep tones)ElseSetPowerButton=not(SetPowerButton)// Toggle pushbutton flag, flag will be managed in main routineEndifPushtimePower=0// Ready for new press on the pushbuttonEndifEndifClrwdt// Clear watchdogEndsub
The counter PushtimePower is increased in the Interrupt routine:
//Interrupthandler,keepassmallaspossibleSubInterrupt()//Timer1section-BuzzersoundsquaresignalgenerationIfPIR1.TMR1IF=1thenBuzzer=not(Buzzer)//GeneratesquaresignaltocreateatonebythebuzzerTMR1H=BuzzerTime_H//BuzzerTimedefinesthefrequencyofthesignalTMR1L=BuzzerTime//Highfrequencytonenearto65000,lowfrequencytonenearto58000accordingEBtestsPIR1.TMR1IF=0//ClearFlag,enableinterruptagain,bestpracticebutalreadyclearedbyGCBASICcompilerEndif//Timer2section-DetectPushButtonpresstimeandgeneralrunningclockIfPIR1.TMR2IF=1then//Timer2InterruptoccurPushTimePower+=1//IncrementthecounterofpushbuttonpresstimeEndTimeOut+=1//IncrementthecounterforinitiatingpoweroffMuteControl+=1//IncrementthecounterformutingtonebynontouchingprobesTMR2=106//Readyfor256-106=150cyclesPIR1.TMR2IF=0//ClearFlag,enableinterruptagain,bestpractice,alreadyclearedbyGCBASICcompiler'LedG = not(LedG) // Blinking Green Led (test purpose, remove for normal operation)EndifEndSub
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).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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...)
(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
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?
The counter PushtimePower is increased in the Interrupt routine:
Last edit: Fabrice Engel 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).
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?
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.
Interressting approach Roger, thank for sharing.
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.
Last edit: Roger Jönsson 3 days ago