my project Capacitive Sensing LED dimmer 12F1822 via PWM


Closed Thread
Results 1 to 40 of 43

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Code tags...

    When you click reply to thread a basic reply dialog shows. At the bottom right there is an Advanced button. That will take you to a full editor.
    Look for # in the toolbar.
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    I'm still not seeing anywhere in the datasheet or device files a reference to the T1GATE_INT




    see data sheet 12.6.6

    TIMER1 GATE EVENT INTERRUPT <==> [in DT-INTS ] T1GATE_INT



    also section 4 INTERRUPTS

    The PIC16(L)F722A/723A device family has 12
    interrupt sources, differentiated by corresponding
    interrupt enable and flag bits:
    • Timer0 Overflow Interrupt
    • External Edge Detect on INT Pin Interrupt
    • PORTB Change Interrupt
    Timer1 Gate Interrupt
    • A/D Conversion Complete Interrupt
    • AUSART Receive Interrupt
    • AUSART Transmit Interrupt
    • SSP Event Interrupt
    • CCP1 Event Interrupt
    • Timer2 Match with PR2 Interrupt
    • Timer1 Overflow Interrupt
    • CCP2 Event Interrupt
    Last edited by richard; - 10th May 2016 at 01:24.

  3. #3
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Cool Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Thanks for the info!

  4. #4
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Hello!

    Here's my code (with the help of others on here) that I inadvertently posted in the context of the thread:
    Code:
    '****************************************************************
    '*  Notes   : Copyright (c) 2016                                *
    '*          : Editor MicroCode Studio Plus PBP 3.0.6.0          *
    
    '*          : MUST BUILD PROJECT WITH MPASM ASSEMBLER           *                    
    '*          : Device = 16LF722A-I/SO                            *
    '*  Date    : 1/18/2016                                         *
    '*  Version : 1z                                                *
                                       *
    '*  Author  : R. WRIGHT                                         *
    '*          :                                                   *
    
    '****************************************************************
    
    
    'SET CONFIGURATION FOR INTERNAL OSCILLATOR WITH PLL MULTIPLIER ON
    #CONFIG
             __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDTE_OFF & _BOR_OFF & _PLLEN_ON
    #ENDCONFIG
    
    
    define OSC 4
    
    
    INCLUDE "DT_INTS-14.bas"     ' PBP interrupt routines (MUST BE IN SAME FOLDER)
    INCLUDE "ReEnterPBP.bas"     ' Included for use of CPS module interrupt
                                 ' Courtesy of Derrel Taylor, PBP Wizard!
                                 
               
    OSCCON = %00010000      'SETS INT OSC TO 4Mhz
    ANSELB = %00111111      'SETS INPUTS B0-B5 AS ANALOG
    TRISB = %00111111       'SETS INPUTS B0-B5 AS INPUTS
    WPUB = %00000000        'DISABLES WEAK PULL-UP BITS
    'TRISC = %00001000       'SETS INPUT C3 AS INPUT
    
    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)
                            'set the CPS module highest frequency availabe (for vcc mode)
                            '+ timer0 clock sourced from CPS module. (BIT 1)
         
    CPSCON0 = %10001100     '1 = CPS module is enabled
                            ' 000 = 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 = %00000001     '0001 = channel 1, (CPS1)
    
    
    '       Software variables
    
    CPSX_PRESENT    var WORD   'VARIABLE FOR BUTTON CURRENT VALUE
    CPSX_THRESHOLD  var WORD   'VARIABLE FOR BUTTON THRESHOLD VALUE
    CPSX_LAST       var WORD   'VARIABLE FOR PREVIOUS BUTTON VALUE
    Duty            VAR WORD   'VARIABLE FOR DUTY CYCLE
    stepp           var byte   'VARIABLE FOR BRIGHTNESS LEVEL
    incnt           var byte   'BUTTON PUSH INCRIMENT
    
    
    '      variables used by interrupt handler, comment out as per assembler
    wsave   VAR BYTE    $20     SYSTEM       ' location for W if in bank0
    'wsave   VAR BYTE    $70     SYSTEM      ' alternate save location for W 
                                             ' if using $70, comment wsave1-3
    
    ' --- IF any of these three lines cause an error ?? ------------------------
    '       Comment them out to fix the problem ----
    ' -- Which variables are needed, depends on the Chip you are using -- 
    wsave1  VAR BYTE    $A0     SYSTEM       ' location for W if in bank1
    'wsave2  VAR BYTE    $120    SYSTEM      ' location for W if in bank2
    'wsave3  VAR BYTE    $1A0    SYSTEM      ' location for W if in bank3
    
    
    '        SET DEVICE I/O PINS
    
    LED1    VAR PORTA.1          ' SET LED LEVEL INDICATORS
    LED2    VAR PORTA.2
    LED3    VAR PORTA.3
    LED4    VAR PORTA.4
    LED5    VAR PORTA.5
    
    
    ' Software/Hardware initialization
    
    PAUSE 50                    ' OSC settle delay
    Duty = 255                  ' SET INITIAL BRIGHTNESS TO FULL ON
    stepp = 5                   ' SET INITIAL LOOKUP TABLE TO 5
    incnt = 1                   ' set input counter to input 1
    
    hpwm 1,DUTY,1000            ' INITIALIZE HARDWARE PWM TO SELECTED LEVELS
    pause 1000                  ' LET EVERYTHING SETTLE
    HIGH LED5                   ' TURN ON OUTPUT FOR LED 5     
    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
    PAUSE 100
    
    
    '******************************************************************************
    '------------------------------< Main program >--------------------------------
    
    'SCAN THE THREE INPUTS UNTIL FINGER DEPRESSION IS DETECTED VIA TIMER0 INTERRUPT
    Main:
        incnt = 2                    
        cpscon1 = %00000001          ' 0001 = channel 1, (CPS1), DOWN BUTTON
        ANSELB =  %00000010          ' SETS INPUT B1 AS ANALOG
        TRISB =   %00000010          ' SETS INPUTS B1 AS INPUTS
        pause 5
        INcnt = 3   
        cpscon1 = %00000010          ' 0010 = channel 2, (CPS2), UP BUTTON
        ANSELB =  %00000100          ' SETS INPUT B2 AS ANALOG
        TRISB =   %00000100          ' SETS INPUT B2 AS INPUT
        PAuse 5
        incnt = 1                    
        cpscon1 = %00000000          ' 0000 = channel 0, (CPS0), 1/0 BUTTON
        ANSELB =  %00000001          ' SETS INPUT B0 AS ANALOG
        TRISB =   %00000001          ' SETS INPUT B0 AS INPUT
        Pause 5                      ' KEEP CYCLING UNTIL INTERRUPT OCCURS
        Goto Main
        
    '******************************************************************************
    
    
    '******************************************************************************
    '--------------------------<INTERRRUPT ROUTINE>--------------------------------
    Timer0_Int:
    @ INT_DISABLE  TMR0_INT                             ; stop timer0 interrupts while we're in here
    pause 100                                           ; PAUSE TO GET TIMER 1 COUNT
    
    ' store away the present timer0 count for the next time we come into the interrupt routine
    CPSX_PRESENT = TMR1                                 ' take a snapshot of Timer1's present count.
    CPSX_THRESHOLD = CPSX_PRESENT - ((CPSX_LAST/10)*6)  ' this sets the 'trigger' up for a 60% diversion (finger press)
    CPSX_LAST = CPSX_PRESENT  
    
    'DETERMINES WHICH BUTTON HAS BEEN PRESSED
    IF CPSX_PRESENT < CPSX_THRESHOLD THEN
             
        if (incnt = 3) AND (STEPP > 1) then            ' down button pressed so decriment stepp
        stepp = stepp - 1
        GOTO SKIP1
        ENDIF
        
        If (incnt = 2) AND (STEPP < 5) then            ' up button pressed so incriment stepp
        stepp = stepp + 1                                
        GOTO SKIP1 
        ENDIF
       
        IF (INCNT = 1) AND (stepp > 0) THEN            ' 1/0 button pressed for off
        STEPP = 0                                       
        GOTO SKIP1
        ENDIF
        
        IF (INCNT = 1) AND (stepp = 0) THEN            ' 1/0 button pressed for on
        STEPP = 5
        ENDIF
    
    ' lights appropriate level indicator leds according to the current lookup table value  
    SKIP1:
        IF STEPP = 5 THEN
        HIGH LED5
        ENDIF
        IF STEPP = 4 THEN
        HIGH LED4
        ENDIF
        IF STEPP = 3 THEN
        HIGH LED3
        ENDIF
        IF STEPP = 2 THEN
        HIGH LED2
        ENDIF
        IF STEPP = 1 THEN
        HIGH LED1
        ENDIF 
        if stepp = 0 THEN                               
        LOW LED1
        endif
     
       
    ' SETS HARDWARE PWM OUTPUT 1 TO DESIRED LEVEL at a frequency of 1Khz
        lookup stepp, [0,16,32,64,128,255],duty        ' choose level, 5 levels plus OFF
        hpwm 1,duty,1000                               ' set new level, frequency                         
        
        IF INCNT = 1 THEN                              ' extra delay for press of
        PAUSE 200                                      ' I/O button
        ENDIF
    
    ENDIF       
    
     
    
    pause 10                                          ' settle time
    TMR0 = 0                                           ' clear TIMER0    
    TMR1 = 0                                           ' clear TIMER1
    @ INT_ENABLE  TMR0_INT                             ; re-enable timer0 interrupt
    @ INT_RETURN   
    
    '*****************************************************************************
    END

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    rather than confuse hecklers fine and working project why not start a new thread that reflects your project and hardware more closely
    say "using multi cap-sense inputs on a pic16f722a" .

    there are many issues with your code starting from the tmr0 setup (prescaler ?) to a 100mS delay in the isr that will interfere with key scanning to such an extent to make success unlikely.

  6. #6
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Okay. But if the timer0 source works for Heckler, why won't it work in mine? Everything seems to be working okay, except that I want it to be a single-shot occurrence on the 1/0 button, rather than a continuous loop, as it is for the up/down buttons. I thought the 100mS pause was the period that the count from timer 1 is occurring? Thanks. The reason I posted on Hecklers was because I was hoping to get some input from him.

  7. #7
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Hello,
    Another one...in your example (PIC12F1822) with the LATA and LATA.0 references, is there a way that I can implement the same function, still using the PIC16F722A device, uisng the B port instead?

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Code:
    ccp1con=12|((PW&3)<<4)
    1. it keeps only the first two bits (Bit 0 and Bit 1) of the PW. Rest bits are set to 0
    2. shifts left by 4 positions
    3. Makes a Bitwise OR with the number 12 (binary 00001100) so to keep te bits 2 and 3 if they are set)
    4. Places the result in ccp1con

    Ioannis

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