@Acetronics2... great minds must think alike
That's pretty much what I did.
@Charlie... my design is specifically designed to run on battery power.  The PIC current drain should not change by much.  Unless one starts trying to supply current via other pins.  That is why I would limit other pin outs to supplying ground when active. (I think this is correct, but I am open for debate)
My intent was to specifically create a Camping/Emergency light that could be dimmed for maximum battery life.  My crude measurements with the PWM output is that on low the current is around 1, 6, 25, 50, 100 milliamps respectivily with each of the 5 steps.
I really was not going to do much, if anything, else with the other pins.
In this case I think the current draw of the PIC should be quite stable and very low.
If one starts doing other things with the other pins then all bets are off with the zener/resistor supply.
And, hey, since I am a hobbiest it might be fun to let the smoke out of a PIC to learn a little.  See how much abuse one of these little jems can take and keep on ticking. (evil grin)
Here is my code that turns the light off after approx 30 min.
I may modify it to blink the led to warn that the 30 min is about to expire so the user can hit the touch sense pad again to go another 30.  The sky is the limit and that is the beauty (as you all surely know) of software control.  Can we all say "feature creep".
PS. As I look at the code I wonder if some are the pause statements sprinkled around are really necessay.  
One thing I do notice is that the touch sense requires a definate quick tap to be detected.  I think this is because the calculation inside the interrupt routine demands a change of approx 20% from the last INT to be counted as a "touch". One can have all sorts of fun playing with some of those numbers and seeing how it changes the Capacitive sensitivity.
	Code:
	'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : Dwight and others                                 *
'*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 11/29/2013                                        *
'*  Version : 1.0                                               *
'*  Notes   :   PIC12F1822                                                *
'*          :                                                   *
'****************************************************************
#CONFIG
  __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_SWDTEN & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
  __CONFIG _CONFIG2, _LVP_OFF
#ENDCONFIG
DEFINE  OSC 8                'tell picbasic what speed the oscillator is
INCLUDE "DT_INTS-14.bas"     ' 
INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
OSCCON = %01110010         
                        '0 = 4x PLL is disabled
                        ' 1110 = 8 MHz 
                        '     0 = unused
                        '      1X = Internal oscillator
ANSELA = %00000100        'make port A.2 is analog
TRISA  = %00000100        'port A.2 is input
WPUA   = %00000000        'turn off weak pull ups
APFCON = %00000001        'Alternate Pin Function Control Reg (default %00000000)
                          'move CCP to PortA.5 (for PWM output)
OPTION_REG = %10000111     
                        '1 = All weak pull-ups are disabled (except MCLR, if it is enabled)
                        ' 0 = Interrupt on falling edge of RB0/INT pin
                        '  0 = Timer0 clock is internal cycle clock (Fosc/4)
                        '   0 = Increment on low-to-high transition on RA4/T0CKI pin
                        '    0 = Prescaler is assigned to the Timer0 module
                        '     111 = 1:256 Timer0 prescaler rate
                
T1CON = %11000001       'enable timer 1 (bit 0) & source the clock from the CPS module (bit 6 & 7 =11
CPSCON0 = %10001100     'set the CPS module highest frequency availabe (for vcc mode) + timer0 clock sourced from CPS module. (BIT 1)
                        '1 = CPS module is enabled
                        ' 0 = CPS module is in the low range. Internal oscillator voltage references are used.
                        '  00 = unused
                        '    11 = Oscillator is in High Range. Charge/Discharge Current is nominally 18 µA
                        '      0 = Capacitive Sensing Oscillator Status bit (only readable)
                        '       0 = Timer0 clock source is controlled by the core/Timer0 module and is FOSC/4
CPSCON1 = %00000010        '0010 = channel 2, (CPS2)
CM1CON0 = 0   ' COMPARATOR OFF
CM1CON1 = 0   ' COMPARATOR OFF
CPS2_PRESENT    var WORD
CPS2_THRESHOLD  var WORD
CPS2_LAST       var WORD
'       Software variables
'       ==================
Duty    VAR WORD
stepp   var byte
AutoOFF var word
' ----------------[ I/O Definitions ]-----------------------------------
FET VAR PORTA.5  ' PWM output to control the FET switch
'       Software/Hardware initialisation
'       ================================
PAUSE 100                           ' OSC settle delay
Autooff = 0
Duty = 0
stepp = 5
hpwm 1,255,250                     ' Start with Light full ON
pause 1000
     
ASM
INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
        INT_Handler   TMR0_INT,  _Timer0_Int,   pbp,  yes
    endm
    INT_CREATE               ; Creates the interrupt processor
ENDASM
 
TMR0 = 0                  'clear  TIMER0
@ INT_ENABLE  TMR0_INT    'enable timer0 interrupts
'***********************************************************************
'------------------------------< Main program >-------------------------
Main:
if autooff = 0 then skipoff
if stepp = 0 then autooff = 0
autooff = autooff + 1
if autooff = 600 then    '(20=1 minute, 600=30 minutes) X delay till AutoOFF
    autooff = 0
    hpwm 1,0,250  'turn LED off after X delay
    stepp = 0
endif
SkipOFF:
pause 1000   ' not a whole lot going on here
goto main
'*****************************************************************************
Timer0_Int:
@ INT_DISABLE  TMR0_INT   ' stop timer0 interrupts while we're in here
pause 25
CPS2_PRESENT = TMR1                                ' take a snapshot of Timer1's present count.
CPS2_THRESHOLD = CPS2_LAST - ((CPS2_LAST/10)*2)    ' this sets the 'trigger' up for a 20% diversion (finger press)
CPS2_LAST = CPS2_PRESENT                           ' store away the present timer0count for the next time we come into the interrupt routine
if cps2_present < cps2_threshold then
    autooff = 1
    stepp = stepp +1
    if stepp>6 then stepp=0            ' check for rollover
    lookup stepp, [0,1,16,64,128,255,255],duty  'choose level, 5 levels plus OFF
    if stepp=6 then 
        hpwm 1,0,1000    'off              ' arrive here when brightness is MAX
        pause 100                          '    So blink to let user know.
        hpwm 1,255,1000  'on
        pause 100
        hpwm 1,0,1000    'off
        pause 100
        hpwm 1,255,1000  'on              ' blink to show that you are at the highest level
    endif    
    hpwm 1,duty,250                    ' else set new level                       ' button pressed so increment stepp  
    pause 100
endif
pause 50      
TMR0 = 0                  ' clear TIMER0    
TMR1 = 0                  ' clear TIMER1
@ INT_ENABLE  TMR0_INT    ' re-enable timer0 interrupt
@ INT_RETURN   
end
 
				
			
Bookmarks