16F726 Capacitive sensing module


Closed Thread
Results 1 to 40 of 40

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Byte_Butcher
    I don't see the "Timer 1 Gate Event Interrupt" listed in the Available Interrupt Sources on your website Darrel.
    Yeah, it's not in there.

    But it's not too hard to add it to the DT_INTS-14.bas file....
    Code:
    ASM
      #define TMR1GATE_INT   PIR1,TMR1GIF     ;-- Timer1 Gate Interrupt
      #define INT_INT   INTCON,INTF     ;-- INT External Interrupt
      #define RBC_INT   INTCON,RBIF     ;-- RB Port Change Interrupt
      #define TMR0_INT  INTCON,T0IF     ;-- TMR0 Overflow Interrupt 16F
      #define TMR1_INT  PIR1,TMR1IF     ;-- TMR1 Overflow Interrupt
    ...
    and
    Code:
    GetIntInfo  macro  IflagReg, IflagBit
    
    Found = NO
    ; nolist
      ifdef TMR1GIF    ;----{ TMR1 Gate Interrupt }---------------[PIR1, TMR1GIF]---
          INT_Source  PIR1, TMR1GIF, PIE1, TMR1GIE
      endif
      ifdef INTF    ;----{ INT External Interrupt }----------------[INTCON, INTF]---
          INT_Source  INTCON, INTF, INTCON, INTE
      endif
      ifdef RBIF    ;----{ RB Port Change Interrupt }--------------[INTCON, RBIF]---
    ...
    <hr>
    Then in your program's interrupt definition ...
    Code:
    '-----Set up Interrrupts
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1GATE_INT,  _CheckCount,   PBP,  yes
        endm    
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR1GATE_INT     ; enable Timer1 Gate interrupts
    Good Luck!
    DT

  2. #2
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    43


    Did you find this post helpful? Yes | No

    Default Capacitor checker

    I made a capacitor checker using a 16F84A which works very well within limits. It uses the pot command. The code in PICBasic (not Pro) is:

    ' Capacitor Measurement
    ' Four digit common anode mulitplexed display
    ' Scan about 60/s (4ms pause)
    ' Driver 74HC4511
    ' Works for 2.0 uf (1K)- 100pf (20K)
    ' 8/20/06
    ' For 16F84A Chip
    ' Data output port A (Pin A5 NC)
    ' Pins 1 - 4 - Digit select for multiplex
    ' Pin5 - pot command
    ' Pin6 - Display Blanking signal (0 = blank)
    ' Pin7 - Range 1 = high value (1K resistor); 0 = low (20K)
    ' Digits 1 - 4; W1 - W4
    ' Measurement (Msr) W10
    ' Note that scan must be 60/s, and 1ms on + 3ms off shows no flicker, but
    ' leds are dim. 2ms on & 2ms off works well.
    Settings:
    Symbol Blank = pin6
    Symbol Range = pin7
    Symbol D1 = w1
    Symbol D2 = w2
    Symbol D3 = w3
    Symbol D4 = w4
    Symbol Msr = w10
    Symbol PortA = 5
    Poke $85, 0 ' Set port A to all outputs
    Output 1 ' Digit 1 - MSD
    Low 1 ' off
    Output 2 ' Digit 2
    Low 2
    Output 3 ' Digit 3
    Low 3
    Output 4 ' Digit 4
    Low 4
    Output 6 ' Blanking
    Low 6 ' Blank all segments


    Start:
    pause 750
    Blank = 0
    If range = 0 then testlow 'Check high or low state

    'Testhigh:
    pot 5,40,msr
    Msr = Msr*10 'Raise results for 4 digit display
    goto math

    Testlow:
    Pot 5,255, msr
    Msr = Msr*100

    Math: 'Set up digits in memory for display
    D1 = Msr/1000
    D4 = Msr//1000
    D2 = D4/100
    D4 = D4//100
    D3 = D4/10
    D4 = D4//10

    Display:
    poke porta, D1 'Send data
    High 1 'Select Digit
    Blank = 1 'Remove blanking
    Pause 2 'Pause
    Blank = 0 'Blank
    Low 1 'Unselect digit
    'Pause 2 'Pause again to save battery

    poke porta, D2
    High 2
    Blank = 1
    Pause 2
    Blank = 0
    Low 2
    'Pause 2

    poke porta, D3
    High 3
    Blank = 1
    Pause 2
    Blank = 0
    Low 3
    'Pause 2

    poke porta, D4
    High 4
    Blank = 1
    Pause 2
    Blank = 0
    Low 4
    Pause 2
    Goto display
    end

  3. #3
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Thanks Darrel!, CSM is working!

    Bingo!
    I changed my interrupt from TMR2/PR2 match to TMR1 gate event and it's working beautifully now. The count on the LCD now accurately reflects what's going on with the CSM oscillator.

    Thanks so much for posting the necessary modifications to DT_INTS-14.bas.
    I actually opened that file and had a look just before bed last night and had pretty much figured out to do the modifications you posted. Thanks for confirming it for me.

    It looks like I need to adjust the TMR2 prescaler a bit to maximize the number of counts in the period, but it's working great now and the next thing to do is set up the averaging and "button pressed" detection routine but I think that should be pretty easy, even for me.

    I'll post code when I've got it working well. I'm rather surprised no one here has played with the Touch Sensor module on the 16F726/7 yet....
    I spent hours searching the archives and came up with a big zero.

    Thanks again Darrel!
    Last edited by Byte_Butcher; - 12th March 2009 at 16:19.

  4. #4
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    Thanks AMay,
    The capacitance checker may come in handy for me some time.

    But what I'm working on isn't a capacitance meter, it's the Capacitive Sensing Module in the PIC 16F726/7's.

    It's used to make capacitive "Touch Sensors" that detect a human finger without any buttons, just a simple conductive pad.

    I think Darrel has got me up and running now.

  5. #5
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    OK, I was gonna sit on this for a few more days until I had a chance to perfect it a little more, but here it is now in it's crude form. I'm sure it needs lots of fixing to be "good" code, but it's functional as is and hopefully will give a starting place for folks who want to play with the capacitive sensing module on the 16F72X PIC's. Please feel free to critique it and fix it up. I need the education...

    This program sets up Capacitive Sensing pin0 (CPS0/RB0) on the 16F726 / 16F727 to be a "Touch Sensor" pin
    When the sensor pad is touched, the raw count (from TMR1) will drop below the running average and a LED on PORTA.1 will flash. If the finger is held on the pad long enough, eventually the running average will catch up with the current value and the LED will turn off.

    Relevant Data Sheets for the CSM module include:

    http://ww1.microchip.com/downloads/e...Doc/41341B.pdf 16F72X / 16LF72X Data Sheet.
    http://ww1.microchip.com/downloads/e...tes/01101a.pdf AN1101 "Introduction to Capacitive Sensing "
    http://ww1.microchip.com/downloads/e...tes/01103a.pdf AN1103 'Software Handling for Capacitive Sensing"
    http://ww1.microchip.com/downloads/e...tes/01171B.pdf AN1171 "Using the Capacitive Sensing Module on the PIC16F72X"

    Crude Code follows.... Enjoy!

    Steve

    ------------
    Code:
    '-------------------------------------------------------------------------------
    'Trying to learn to use the Capacitive Sensing Module on a 16F727 --------------
    '-------------------------------------------------------------------------------
    
    @  __config _CONFIG1, _DEBUG_OFF & _PLL_EN & _BORV_2_5 & _BOR_ON & _CP_OFF & _MCLRE_OFF & _PWRT_EN & _WDT_OFF & _INTOSCIO
    @  __config _CONFIG2, _VCAP_RA0
    
    Include "MODEDEFS.BAS"    ' Include Shiftin/out modes
    INCLUDE "DT_INTS-14.bas"  ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"  ' Include if using PBP interrupts
    
    DEFINE LCD_DREG PORTA    ' Set LCD Data port
    DEFINE LCD_DBIT 4        ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTA   ' Set LCD Register Select port
    DEFINE LCD_RSBIT 2       ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTC    ' Set LCD Enable port                                                   
    DEFINE LCD_EBIT 0        ' Set LCD Enable bit
    DEFINE LCD_BITS 4        ' Set LCD bus size (4 or 8 bits)
    DEFINE ADC_BITS 8        ' Set number of bits in ADC result
    DEFINE ADC_CLOCK 3       ' Set clock source for ADC (rc = 3)
    DEFINE ADC_SAMPLEUS 100  ' Set ADC sampling time in microseconds 
    DEFINE OSC 8
    
    OSCCON = $10         'set oscillator to 8 Mhz
    
    TRISA= %00000000     'Set 'em all to outputs
    ANSELA= %00000000	 'Set 'em all to digital
    TRISB= %11111111   	 'all input
    ANSELB= %11111111    'all analog
    TRISC= %00000000     'Set portC all outputs   
    
    CPSCON0 = %10001101  'Cap sense on, high range oscillator
    CPSCON1 = %00000000  'Cap sense channel input 0 is selected
    
    '-----Alias Pins
    RW           var    PORTC.3 : LOW rw   'Read/Write for LCD
    LED          var    PORTA.1      'LED to display when button is pressed
    
    '-----Allocate Variables
    timercount   var    word  ' raw count from TMR1
    timerave     var    word  ' long term average of timercount
    AvgCount     var    word : AvgCount = 32 'number of samples to average 
    
    '-----Set up Interrrupts
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1GATE_INT,  _CheckCount,   PBP,  yes
         endm    
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    @ INT_ENABLE  TMR1GATE_INT     ; enable Timer 2 interrupts
    
    High LED
    Pause 200       'Let the LCd wake up
    lcdout $fe, 1   'Clear LCD
    lcdout $fe, 2, " Awake! "
    pause 500
    
    '----- Timer Setup
    T2CON = %01110110 'bit7=unimplemented, bit6-3=postscaler, bit2=TMRON, bit1-0=prescaler 
    PR2 = %11111111       'give PR2 a number for TMR2 to match
    PIR1.1 = 0      'Clear the TMR2 interupt flag
    PIE1.1 = 1      'Turn TMR2 interrupt enable on
    T1CON = %11000101 'Timer clock source=CAPOSC, prescale=1:1, dedicated OSC disabled, no external clock synchronize, timer on
    T1GCON = %11100010 'Timer1 gate init/ Toggle Mode
    PIR1.7 = 0   'Clear Gate Interrupt Flag
    PIR1.1 = 0   'clear the TMR2 interupt flag
    
    Goto main   'Just get it going...
    
    '---[TMR2 - interrupt handler]--------------------------------------------------
        disable   
    CheckCount:
        T2CON.2 = 0   'stop timer2
        T1CON.0 = 0   'stop timer1
        timercount = TMR1L + TMR1H << 8   'stuff the contents of the timer register into a word
        Gosub Fingercheck
        
        TMR1L = 0     'reset counter to 0...
        TMR1H = 0     'upper 1/2 too
        TMR2 = 0       'rest timer 2
        PIR1.1 = 0   'Clear TMR2 Interrupt Flag
        T1CON.0 = 1  'restart timer1
        T2CON.2 = 1  'restart timer2
    @ INT_RETURN
        enable 
        
    '----Average the timercount result----------------------------------------------
    
    Fingercheck:
        timerave = timerave - (timerave/AvgCount)    'average the count so you have a reference to compare....
        timerave = timerave + (timercount/AvgCount)  '...with yimercount
            
        If timerave - (timerave /40) > timercount then  'If timercount drops more than 2.5% below the average, signal that a button is pressed.
            low LED      'make the LED do a little dance.
            pause 50
            High LED
            pause 25
        endif
        
        If timercount > timerave +50 then    'do a "fast recovery" on the average when finger is removed.
           timerave = timercount
        endif
        return     
        
    '----Main loop-----------------------------------------------------------
    Main:
        LCDOUT $fe,2, " Count= ", dec timercount,"    ",$fe,$C0,"Average = ", dec timerave,"    "
        pause 50  
    GOTO Main
    
        end

  6. #6
    Join Date
    Jan 2009
    Posts
    22


    Did you find this post helpful? Yes | No

    Default

    Anybody else working with capacitive sensing? Any updates?

  7. #7
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    I have used capacitive sensing in a handheld terminal with a graphic LCD. It scans 24 keys (although it will handle up to 32 keys) and does require a special MCU - I'm using a PIC18F44K20. Does auto-repeat, audible feedback, etc.
    Name:  A_66A.JPG
Views: 7852
Size:  15.5 KBName:  A_6B.JPG
Views: 7842
Size:  24.0 KB
    Last edited by rmteo; - 4th May 2010 at 21:09.

  8. #8
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Nice looking project rmteo. Do you have a PBP example for it?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Version Control
    By btaylor in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 16th October 2011, 17:12
  2. Relaxation oscillators (for capacititive touch sensing)
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 6th December 2009, 22:03
  3. mTouch capacitive sensing
    By jrprogrammer in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 1st November 2008, 22:54
  4. Replies: 1
    Last Post: - 27th July 2008, 06:14
  5. Rf module
    By tangray in forum Adverts
    Replies: 0
    Last Post: - 7th August 2006, 07:14

Members who have read this thread : 0

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