my project Capacitive Sensing LED dimmer 12F1822 via PWM


Results 1 to 40 of 43

Threaded View

  1. #1
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427

    Default my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Well, After posting here a couple of days ago fishing for someone who has had success setting up the Cap Sense module on the 12F1822, and getting no response, I did some searching and was finally able to get it going.

    I found a couple of threads where HankMcSpank had tackeled it... (thanks hank)
    Which led to another thread where some correcttions reggarding the use of TMR0 vs TMR1.
    (I can't find either of those threads now or I'd post a link)

    Any way here is a little project I have wanted to do and CapSense seems to work and makes it a bit simpler.

    See attached schematic.

    This is the LED I am using... http://dx.com/p/g4-1-2w-108-lumen-35...b-dc-12v-51269

    Which makes use of Darrel's Instant Interrupts (thanks Darrel!). You will have to go get those include files from his web site dt.cambs.net

    here is the code...


    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
    
    
    ' ----------------[ I/O Definitions ]-----------------------------------
    FET VAR PORTA.5  ' PWM output to control the FET switch
    
    '       Software/Hardware initialisation
    '       ================================
    PAUSE 50                           ' OSC settle delay
    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:
    pause 100   ' 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
        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
    Name:  12F1822_CapSense.jpg
Views: 4647
Size:  88.1 KB

    enjoy! and tell me what you think (good or bad)
    Last edited by Heckler; - 1st December 2013 at 05:29.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

Similar Threads

  1. 16F726 Capacitive sensing module
    By Byte_Butcher in forum General
    Replies: 39
    Last Post: - 15th May 2014, 19:40
  2. Need help setting up Capacitive Sense 12F1822
    By Heckler in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th November 2013, 21:10
  3. Replies: 1
    Last Post: - 6th August 2012, 10:57
  4. Capacitive sensing module on 16f1936
    By grahamg in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 25th April 2010, 14:17
  5. mTouch capacitive sensing
    By jrprogrammer in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 1st November 2008, 22:54

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts