Oops! This appears to be a new requirement: pressing the switch for less than 500 ms and then releasing it to indicate a mode change to the LED driver module.
One could
hope that the release interval has some minimum requirement, say on the order of 500 ms, to avoid the 1 ms LED OFF duration that occurs every 100 ms (when the LED is ON) from being interpreted by the LED driver as a switch release and hence a request for a mode change.
@TenderTendon, can you send me an LED driver module so I can test this?
The fact that for the PIC10F20x series there is only one "interrupt" ...a full-blown reset... is a royal PITA. The reset condition can occur in just four ways: power on reset (lasting for about 18 ms); master clear reset (if enabled); watch-dog timer timeout; and input bit-transition event. I suppose it is blessing that bits in the STATUS word allow the program to determine which of these four conditions reset the processor.
Of course there are only two reset conditions we are interested in after putting the PIC to sleep: watch-dog timer timeout and input bit-transition event. The latter to detect when the switch changes state, the former to time the one millisecond "blink" every one hundred milliseconds when the LED driver is on.
Unfortunately it is not possible to put the PIC to sleep AND use the TIMER0 function because TIMER0 operates off the microprocessor clock, which stops during sleep mode. It is possible to use the watchdog timer. The watchdog timer is a "feature" intended to recover from a software glitch by starting over from a fresh beginning. It is a rather crude timer without the accuracy of the PIC's main clock, but it is "good enuf" for its intended purpose. And it should be good enough for this project, timing not only the 100 ms intervals but also the one millisecond "MOSFET OFF" intervals too. We just need to do some housekeeping, set the watchdog timer, go to sleep. Sleep most of the time!
The project must also keep an eye on the push button switch, since this determines what state the machine will be in: OFF, ON, Sustained ON, and Forced OFF. The normal idle mode is OFF with the energy storage capacitor fully charged through the Schottky diode, the LED Driver module and the flashlight battery. In the OFF state we will let the PIC go to sleep while we wait for an input state-transition from the push button switch. No need to pulse the MOSFET off because it is already off. No need to mess with the watchdog timer yet.
When a button press occurs, the PIC wakes up. No need to "debounce" the switch. We just assume that it will be pressed for at least 100 ms and turn on the MOSFET, set the watchdog to bark after 100 ms, and go back to sleep. We are now in the ON state.
When the watchdog barks, we look at the switch again. If it is released the MOSFET is turned off, we revert to the OFF state, setup for a switch transition, and go back to sleep. If the switch is still on, things get a little more complicated. Now we need to set the watchdog to bark after only one millisecond, still turn off the MOSFET, and keep track of how many times this occurs. After the millisecond expires and the watchdog barks, turn the MOSFET on, set the watchdog timer to expire in 100 ms and go back to sleep. Each time the watchdog barks in this mode, we need to check how many times we have been here and note whether the switch is still pressed. After five iterations, if the switch is still pressed, we move to Sustained ON mode.
The same operations occur in Sustained ON as occurred in ON (turn off the MOSFET, set watchdog to bark one millisecond later, go to sleep, wake up and look at the switch, turn on the MOSFET, set watchdog to bark in 100 ms, and go to sleep again. However, while in Sustained ON state we must recognize when the switch is released and store that information in a non-volatile memory variable. We also enable, after the switch is released, the reset-on-input-transition (which we disabled when we left the OFF state and entered the ON state) so the PIC resets when the push button is pressed again, whether or not the watchdog timer has finished with its 100 ms delay.
Pressing the switch again, after releasing it in the Sustained ON state, takes us to the Forced OFF state. We immediately turn the MOSFET off, (thereby restoring power to the energy storage capacitor), disable the watchdog timer, perform whatever "housekeeping" is needed, and revert to the OFF state, waiting for another button push.
I haven't actually coded any of the above yet. I would really like to see how Adam has approached the problem so I don't re-invent his wheel. I do know there is a coding problem in the board that
@TenderTendon sent me because the MOSFET gate is always at common potential, there is no pull-up to Vcc for the normally-open push button switch, and pins 3, 5, and 6 are always at Vcc potential, in this case +4.65 VDC from three series-connected AA cells providing power. Pins 1, 2, and 4 are always at Vss (common potential).
@Arouse1973 would you upload the latest version of your code so I can try it out on the board that
@TenderTendon sent me?
I know Adam is now programming for the PIC10F320 (and I have recently received a "hobby kit" with five 10F106 PICs), but the differences may not be important. You still can't use the internal timer(s) while the PIC is in sleep mode, although apparantly the -320 can wake up from a watchdog timeout without resetting the PIC.
I have the MPLAB X IDE installed on my laptop now, and it appears to communicate with teh PICkit 3 programming interface. I also have a PIC10F206 soldered to an 8-pin carrier and installed on a small breadboard, along with five leads to connect it to the PICkit 3. I am using the same three AA cells to power this as I was using on the flashlight prototype board. All I need to do now is get up to speed on how to use the MPLAB X IDE! It's been quite an adventure so far...
73
de AC8NS
Hop