example of capacitive sensor example for 18fxxK22 series


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: example of capacitive sensor example for 18fxxK22 series

    Hi guys , well after some bad Ad readings from " interesting" ad channel , solved the touch switch using the CTMU

    there is no examples of setup of the CTMU for Capacitance Touch sensor for the 18F on PBP at the moment

    This example is the most simplest setup of the CTMU , which is how Microchip show in the App note and within the datasheets on the CTMU , refer to earlier posts in the link, also take a look a cap switch design http://ww1.microchip.com/downloads/e...U%2001375a.pdf

    and the material for production ( Look at PET with a metal film application as well)

    For what i wanted i needed a simple touch switch by using the body of a metal power on/off push switch to change modes , the switch supplied power only and now allowed the user to change modes of operation without having to add switches or change the way the power cct work

    A ground tag was attached to the body to the A/D channel

    for this example the value of <1000 ( 12bit ADC )was found to be correct for a touched switch in my cct , where the reading up past 2000 dec was an untouched sensor

    the subrouine is called from an ISR every 10ms , the values of 4 reading are read and averaged , the value while its <1000 waits until the button is not touched ( acts as debouce ) before it toggles the mode / mode change

    the values used in DT avg routine set to sensitivity of the touch switch by the "Fastspread" value , as usually the not touched value did not range that wide ( 500 ) and the samples at !0ms x 8 sample s gave 80ms min for a valid sample



    this still requires some tweeking but it works

    hope it helps

    cheers

    Sheldon


    IN THE ISR
    Code:
    
       gosub Cap_Touch_Sensor                 ' do reading of the capacitive Touch Sensor ( earth point of pwr switch) on AN7 / GET AN AVERAGE 
          
            if VALUE1 < 1000  THEN                  ' if the Averaged value <1000 then sensor is being touched 
               while VALUE1 < 1000                  ' while the value < 1000 ( sensor is being touched )   
                 gosub Cap_Touch_Sensor             ' do reading of the capacitive Touch Sensor 
               wend                                 ' wait till its not being touched  
              toggle IR_TX_HI_PWR                   ' toggle the output for Hi pwr mode 
            endif

    Code:
    Cap_Touch_Sensor:
       
     ' ----- Setup CTMU - for Touch Switch on AD7 , used when IR-TX ----------
          
          CTMUCONH = %00000000    ' Bit7 CTMU enable 1=On ,0 = Off, Bit6 N/A , Bit5 CTMUSIDL - 1= disable modual when in idle mode 0= continue module operation when in idl mode 
                                  ' Bit4 TGEN 1 = ENABLE 0= disable edge delay gen , bit3 EDGEN 1= edges not blocked 0= edges blocked , Bit2 EDGSEQEN - 1 = edge1 occure prior edge2 can 0= no edge seq 
                                  ' Bit1 IDISSEN 1= grounded 0= Not grounded  Analoge current Source , CTTRIG 1= Trig Output enabled  0 = disabled 
                                  
          CTMUCONL = %00000000    ' Bit7 EDG2POL 1= edge2 pos edge responce 0= edge2 neg edge responce ,Bit6-5 Edge2 select source= 11=CTED1PIN 10=CTED2 pin, 01 ECCP1 , 00 ECCP2                         
                                  ' Bit4 EDG2POL 1= edge1 pos edge responce 0= edge1 neg edge responce ,bit3-2 Edge1 source= 11=CTED1PIN 10=CTED2 pin, 01 ECCP1 , 00 ECCP2  
                                  ' Bit1 = Edge 2 (R/W) status 1= event occured 0= Not occured ,Bit0 = Edge 1 (R/W) status 1= event occured 0= Not occured , 
        
          CTMUICON = %00000011    ' Bit7-2 011111-000001 max - min pos change from nominal current , 000000 Nominal current output set by bit1-0 ,111111 -100000 min -max neg change from nominal current
                                  ' Bit1-0 IRNG - Current Source Range Select bita 11= 100xbase(55uA) 10= 10xbase(5.5uA) ,01=base (0.55uA), 00= current sourse disabled 
     
          ANCON0.7 = 1              ' Set PortF:2 Analog / Digital allocation - 0 = digital ,1 analog - ANSEL7
          TRISF.2  = 1              ' Set TRISF.2 = 1 - Select portF.2 to Input   - AN7 
          ADCON0 = %00011101        ' Select the A/D channel - AN7 turn on A/D 
       
          CTMUCONH.7 = 1            ' Turn on CTMU
          CTMUCONH.1 = 1            ' IDISSEN (Bit1) - Drain any charge on the A/D channel  
          pauseus 2                 ' wait for charge to drain 
          CTMUCONH.1 = 0            ' IDISSEN (Bit1) - Stop Discharge on the A/D channel  
          pauseus 2                 ' delay before selecting the CTMU edge1  
          CTMUCONL.0 = 1            ' Begin Charging  Edge1stat
          pauseus 2                 ' wait for charge 
          CTMUCONL.0 = 0            ' stop Charging  Edge1stat
          adcin 7,value1            ' do ADC reading on AN7 - touch switch input 
          gosub aVERAGE             ' average the readings 
          ADCON0 = %00011100        ' Select the A/D channel - AN7 and turn off A/D module 
          CTMUCONH.7 = 0            ' Turn off CTMU
    RETURN

    Code:
    Average:
      AvgCount	    CON  8		    ' Number of samples to average in Average routine
      Fastspread	CON  500	    ' Fast Average threshold +/-  in Average routine
     
        IF Value1 = ADavg Then NoChange
        IF ABS (Value1 - ADavg) > Fastspread OR Value1 < AvgCount Then FastAvg
        IF ABS (Value1 - ADavg) < AvgCount Then RealClose
        ADavg = ADavg - (ADavg/AvgCount)
        ADavg = ADavg + (Value1/AvgCount)
        GoTo AVGok
      FastAvg:
       ADAVG = VALUE1
       GoTo AVGok
      RealClose:
        ADavg = ADavg - (ADavg/(AvgCount/4))
        ADavg = ADavg + (Value1/(AvgCount/4))
      AVGok:
        Value1 = ADavg			' Put Average back into Value
                                ' copy value for use by touch switch
      NoChange:
    
    Return

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: example of capacitive sensor example for 18fxxK22 series

    Not the answer you're looking for, but have you looked at the capacitive sensors available from Azoteq? You can buy them through Mouser. A single channel costs something like 43 cents. They can do both touch and proximity sensing, need a few capacitors and a resistor to work. I've been using them by the boat-loads.

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. Capacitive sensor using ADC
    By johnmiller in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st November 2010, 10:46
  4. Replies: 3
    Last Post: - 29th September 2010, 14:45
  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 : 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