16F726 Capacitive sensing module


Closed Thread
Results 1 to 40 of 40

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    I have been using capacitive touch switches for several years with little problem except the
    Q prox chips became obsoleted after a couple of years.

    Any Microchip can be used as the requirements are unbelievably simple:
    An I/O pin to charge with supply voltage and an AD pin to read the cap sense value.

    I sometimes take 20 to 100 readings, bubble sort them highest to lowest, then average
    only array elements 20% to 80% to to remove any errant readings.
    The outcome is stable, varies only one or two at 8 bit.
    The AD needs to be coded for power conservation however.

    The following code is derived almost directly from AN1286:
    Code:
    'PIC18F2520  
    subLOAD_AD_ARRAY:
        ADCON0 = %00000001  'ENABLES AD
        For i = 0 To 99
            LOW PORTA.0
            LOW PORTA.2 
            ASM 
            NOP 
            ENDASM 
            TRISA.0 = 1 'INPUT
            HIGH PORTA.2
            ASM 
            NOP
            ENDASM 
    
            'AD BY REGISTER'S  
            ADCON0.1 = 1   ' START AD  GO/DONE BIT
            While ADCON0.1 = 1 : Wend  ' AD ENDED GO/DONE Bit
            wAD.LowByte = ADRESL       ' 2520 10 BIT AD ONLY
            wAD.HighByte = ADRESH  
            yAD_ARRAY100[i] = wAD >> 2
        Next
        ADCON0 = %00000000  'DISABLE AD
        Return
    The touch button is simply a 3/16" stainless steel machine screw through the faceplate with
    a double nut tightened around a soldered wire loop connected to the AD.
    I add a little grommet for looks.
    The charge lead is simply wound several times around the screw behind the faceplate.

    Another version is individual metal tabs of sheet metal adhered to the underside of the
    enclosure for buttons.
    The AD wire is soldered to each tab and the charge wire only needs to wrap around the
    AD wire or all AD wires.

    The cap sensor readings can vary over time and the base line to compare the readings against
    should be re zeroed and a new max value redetermined at regular intervals.

    I laminate an inkjet photo quality paper (not photo paper) printout for the faceplate.

    The last consideration is ESD.
    A simple 100k resistor in line on both leads is a good solution.

    Norm
    Last edited by Normnet; - 7th May 2010 at 04:50.

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


    Did you find this post helpful? Yes | No

    Default

    Hi Norm.

    I think you can shorten your code by Left justify the AD results and take only the ADRESH. Same as with the lines:

    Code:
            wAD.LowByte = ADRESL       ' 2520 10 BIT AD ONLY
            wAD.HighByte = ADRESH  
            yAD_ARRAY100[i] = wAD >> 2
    Ioannis

  3. #3
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Norm,

    There are many ways to do it. You can also use the interrupt TMR1 to detect changes in the output frequency due to a pad being touched by a finger. This is explained in AN1101 "Introduction to Capacitive Sensing".

    http://ww1.microchip.com/downloads/e...tes/01101a.pdf


    Quote Originally Posted by Normnet View Post
    The following code is derived almost directly from AN1286:
    I see that you are using AN1286 "Water-Resistant Capacitive Sensing". If you are using your touch sensors in a wet environment, then you might want to consider using inductive sensors. Take a look at the table I attached to this post.

    http://www.microchip.com/stellent/id...param=en538303

    Name:  Inductive.JPG
Views: 6882
Size:  38.5 KB

    Robert

  4. #4
    Join Date
    Oct 2005
    Location
    New Zealand
    Posts
    171


    Did you find this post helpful? Yes | No

    Default Errors left right and center

    I'm trying to use capacitive touch too - was kinda hoping that the inbuilt capacitive sensing was a bit more straight forward than the convoluted mess and complexity it looks like. Would it be too hard for microchip to make it a bit user friendly so you just enable named pins as capacitive, then read the register to see which were pressed and had inbuilt filtering etc??

    I've used Mister E's capacitive touch idea before and it seems so much simpler than the "integrated' solution.

    Anyrate - enough of my ramblings, I'd like to learn to use it - hopefully it's not as complex as it appears from outset. I've copied byte butchers code as above and pasted it directly into MCS and I have errors all over the place. I have a fresh install of PBP2.6 and have also just downloaded DTs' ints-14.

    How come it works for him and not for me? I had the chip type 16F726 like his also

    While I'm on a roll, why wont MCS allow you to copy and paste the error list???

    I think many of the issues come from the Interrupt routine but asm and interrupts are well over my head. Is there any reason Byte Butcher used interrupts rather than just checking pins periodically?

    Thanks

    Error[118]c:\pbp...asm 213 : Overwriting previous address contents (2007)
    Error[101]c:\pbp...asm 342 : ERROR: (wsave variable not found)
    Error[101]c:\pbp...asm 308 : ERROR: (" Add:" wsave VAR BYTE $20 SYSTEM)
    Error[101]c:\pbp...asm363 : ERROR: (Chip has RAM in BANK1, but WSAVE was not found)
    Error[101]c:\pbp...asm315 : :ERROR: (" Add:" wsave VAR BYTE $A0 SYSTEM)
    Error[128]c:\pbp...asm 989 : Missing argument(s)
    Error[101]c:\pbp...asm672 : ERROR: ("INT_Handler" - Interrupt Flag (FlagReg,FlagBit) not found)
    Error[128]c:\pbp...asm 1000 : Missing argument(s)
    Error[101]c:\pbp...asm 672 : ERROR: (INT_ENABLE" - Interrupt Flag ( FlagReg,FlagBit) not found)

  5. #5
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by George View Post
    I think many of the issues come from the Interrupt routine but asm and interrupts are well over my head. Is there any reason Byte Butcher used interrupts rather than just checking pins periodically?
    He used interrupts because that is the way to do it. You need to count the number of pulses in the signal for a fixed amount of time and determine if the switch has been touched or not. This app note might help you to understand the basics of CSM

    http://ww1.microchip.com/downloads/e...tes/01101a.pdf

    Robert
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

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


    Did you find this post helpful? Yes | No

    Default

    Don't you have to do better things?

    Seriously now, I mean it, while the timers do their jobs, that is to count, you can do many other tasks in your program.

    That is why the so hated interrupts are needed.

    As for your errors, do post your code. Do you need help or guesses?

    Ioannis

  7. #7
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    I experimented with capacitive touch on a 16f690, my related thread about it here (you might glean some stuff from it)...

    http://www.picbasic.co.uk/forum/showthread.php?t=12196

    My (short!) youtube video of the end result here (using a guitar's scratchplate screw's 'head', as a 'toggle switch')...



    Re the process itself...it might initially seem a lot to swallow (especially if it's your first sortie using interrupts - which are pretty much essential for capacitive touch), but like everything else, if you break it down into smaller 'building blocks' and approach each block one at a time, then it becomes much more digestible.

    You obviously need to get the relaxation oscillator working first - an oscilloscope is *extremely* useful to make sure this is working (& getting it working proved to be the most troublesome part for me....there's a whole heap of registers that need to be set 'oh so right', once I got it running, I was off to the races).

    re those wsave errors you're seeing when compiling - you need to comment out some lines in the DTS_INTS-14.BAS (search on the error wsave VAR BYTE $20 SYSTEM on this forum to find out what needs doing!)
    Last edited by HankMcSpank; - 31st August 2010 at 10:16.

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


    Did you find this post helpful? Yes | No

    Default Re: 16F726 Capacitive sensing module

    Finally I got my first touch demo working with 16F1827 chip. The CSM module is easy to use and makes program small enough.

    But, I have one problem with the stability of the output that shows whether one is touching, close to touch or not touching at all.

    Although I have included Darrel's Average routine there is a grey region that makes deciding if one is touching not very clear.

    The following test code has a LED indicator to show when touch pad is really touched or not. If finger is very close, value is playing up or down the threshold and makes the LED indicator blink.

    Code:
    OPTION_REG = %10000110    'Tmr0 from 256 presc and Int Clock
      
     T1CON = %11000001 '%00110000    ' TMR1 1:8 prescale, timer1 off
    
    led         var portb.4       
    value       var word
    AvgCount CON 32  ' = Number of samples to average
    FAspread CON 10000 ' = Fast Average threshold +/-
    ADavg  VAR WORD
    flag        var bit
    'Interrupt Engine Setup
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ASM
    INT_LIST  macro    ; IntSource,     Label,      Type,       ResetFlag?
      INT_Handler   TMR0_INT,       _Timer_0,     PBP,    yes
    
     endm
     INT_CREATE               ; Creates the interrupt processor
        INT_ENABLE   TMR0_INT
    ENDASM
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::       
    
    goto main
    
    Timer_0:
        t1con.0=0
        vALUE.lowbyte=tmr1l:VALUE.highbyte=tmr1h
        flag=1
        tmr1l=0:tmr1h=0
        t1con.0=1
    @ INT_RETURN    
    
    main:
    clear
    pause 1000
    
    start:
    if flag=1 then
        flag=0
        gosub Average
    endif
    
    hserout [27,"[H",27,"[10;0H","Value is: ",#vALUE," "]
    
    if VALUE<319 then
        high led
    else
        low led
    endif
    goto start
    
    Average:
        IF Value = ADavg Then NoChange
        IF ABS (Value - ADavg) > FAspread OR Value < AvgCount Then FastAvg
        IF ABS (Value - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)
        ADavg = ADavg + (Value/AvgCount)
        GoTo AVGok
      FastAvg:
        ADavg = Value
        GoTo AVGok
      RealClose:
        ADavg = ADavg - (ADavg/(AvgCount/4))
        ADavg = ADavg + (Value/(AvgCount/4))
      AVGok:
        Value = ADavg  ' Put Average back into Value
      NoChange:
    Return
    Any ideas welcome.
    Ioannis
    Last edited by Ioannis; - 4th December 2011 at 22:21.

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