example of capacitive sensor example for 18fxxK22 series


Closed Thread
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default example of capacitive sensor example for 18fxxK22 series

    Hi Guys looking for a an example for capacitive touch switch using the register setup for the 18f series of the CTMU registers


    looking to toggle an output after touching for a certain time

    regards

    sheldon

  2. #2
    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

    has no one done a capacitive touch switch sensor on the 18F series using CTMU ?

  3. #3
    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

    here what i have gathered from microchip ap note AN1250 and the 18F87k22 data sheet

    the readings i am getting ranging 0 - 4095 , jumping all over the place , without me touching the port pin at all

    no edge has been detected on the status bits

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

    http://ww1.microchip.com/downloads/e...Doc/39960d.pdf - section 27.x

    some input on what i may be doing wrong on the setup / operation of the CTMU would be great

    Cheers

    Sheldon
    Code:
    
    Cap_Touch_Sensor:
      '--------- Touch Switch varables / constants  ----------------
     
     
     ' ----- Setup CTMU - for Touch Switch on AD17 , 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 = %00010100    ' 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 status 1= event occured 0= Not occured ,Bit0 = Edge 1 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 
     
     
     
     
         CTMUCONH.7 = 1            ' enable CTMU 
       '  ANCON2.1 = 0              ' Set PortG.3 = 0 - Set portG.3 to Digital - to ensure sensor port is at zero potential - AN17 - AD touch switch input 1R-TX mode
       '  TRISG.3  = 0              ' Set TRISG.3 = 0 - Set portG.3 to Output  - to ensure sensor port is at zero potential - AN17 - AD touch switch input 1R-TX mode
       '  LATG.3   = 0              ' Clear PortG.3 = 0 to ensure sensor port is at zero potential
       '  pauseus 5                 ' wait for zero potential
        
         ANCON2.1 = 1              ' Set PortG.3 = 1 - Select portG.3 to Analog  - AN17 - AD touch switch input 1R-TX mode
         TRISG.3  = 1              ' Set TRISG.3 = 0 - Select portG.3 to Input   - AN17 - AD touch switch input 1R-TX mode
         ADCON0 = %01000100        ' Select the A/D channel - AN17 but dont turn on a/d 
         CTMUCONH.1 = 1            ' IDISSEN (Bit1) - Drain any charge on the A/D channel  
         pauseus 125                ' wait for charge to drain 
         CTMUCONH.1 = 0            ' IDISSEN (Bit1) - Stop Discharge on the A/D channel  
         CTMUCONL.1 = 0            ' make sure edge2stat is 0 
         CTMUCONL.0 = 1            ' Begin Charging  Edge1stat
         pauseus 125                ' wait for charge 
         CTMUCONL.0 = 0            ' stop Charging  Edge1stat
          HSEROUT2 ["before a/d  - edge2 = ",dec CTMUCONL.1, "  edge1 = ",dec CTMUCONL.0,13,10]
         ADCON0 = %01000101        ' Select the A/D channel - AN17 and turn on A/D module 
         adcin 17,value1            ' do ADC reading on AN17 - touch switch input 
         HSEROUT2 ["value1 = ",dec4 value1,13,10]
         HSEROUT2 ["edge2 = ",dec CTMUCONL.1, "  edge1 = ",dec CTMUCONL.0,13,10,10,10]
        ' gosub aVERAGE             ' accumulate the values for base line and set Touchsw1 flag 
        ' HSEROUT2 ["value1 avg = ",dec4 value1,13,10]
        ' HSEROUT2 ["TouchSW1 = ",dec touchsw1,13,10,10,10]    
         ADCON0 = %01000100        ' Select the A/D channel - AN17 and turn off A/D module 
    return

  4. #4
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

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

    I didn't use CTMU...
    But I tried CVD method, it worked, but I wasn't happy with that...
    CVD is nice because you can use on any pic with ADC.

  5. #5
    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

    seems to use the CTV the microchip library algorithms are best employed,

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


    Did you find this post helpful? Yes | No

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

    Here is how I did it with the 12F1822... but you probably found this if you did any searching.
    http://www.picbasic.co.uk/forum/showthread.php?t=18537

    Is it significantly different on an 18f chip?
    I've never used one of those. So far all my projects have been in the 12F and 16F world.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  7. #7
    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

    yes found many examples of the 12F, 16F arrangements , and since the they dont have a CTMU like the 18F , the register setup and approche are not close at all ,

    the point after a valid reading is done maybe simular in that they can take multi readings to establish a base line for " not touched " , a touched reading value " and a " variance value " between the 2 values
    or from the apnote a debounce approch

    for this requirement , i am just looking for a simple on / off touch switch from the 18F ad ch17 ( spare ad channel)

    i have tested the ad channel to see if it reading a known other ad input value and its working fine , so it purely how the CTMU is setup and used.



    the examples in the ap note read clear enough , just not geting a result that looks right or is usable

    since the code manually sets the " edge" and read of the AD channel , the edge1 or 2 status should be always read 0 ???????????????????

    the setting for the triggers in the ADCON1 - TRIGSEL ( bit 7,6) 01 - Trigger from CTMU is not used as the start for the A/D conversion ,which then does not require CTTRIG on reg CTMUCONH.0 to output the trigger from what i think it normally would be

    i dont have any external pin ( CTED1, CTED2) for external triggers , or the ECCP1 or ECCP2 for timed input triggers ,although i have tried them with no " edge trigger seen so far"

    the subroutine is called every 10ms by ISR to get touch reading value at the moment.

    when 20 reads are done the results are averaged via DT''s modified running average routine to establish base line for touched / not touched trigger point


    been playing with this a day , and cannot get the answer as yet


    regards

    Sheldon

  8. #8
    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

    the CTMU interesting part of the 18f chips , with timed delay triggering and pulse control for touch switch applications input and few features i would love to play with , and mastering will open some great features

    for those interested in converting a working C version of touch switch using an ISR into PBP , here it is , and is good to uses as a base for conversion if you feel like it ( i am now starting to learn C again)

    I think ill stick to the manual trigger , which is called from the ISR every 10ms for touch switch reading for the moment , will post working code for that when its done

    it helps when you get a stable reading from the a/d port when not touched , which is where i was getting some issues

    touch switch CTMU EXAMPLE - C CODE.txt

  9. #9
    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

  10. #10


    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.

  11. #11
    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

    looks interesting , as you say does not address my issue for the moment but could be used for other things and to keep in mind

    http://www.azoteq.com/capacitive-sen...-switches.html

  12. #12
    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

    Just for you info

    further testing of the CTMU for my project showed that the adc reading on the CTMU changed a great deal when the programmer was connected to the pcb or the USB to TTL converter , when connected to the pcb and PC for value of the readings

    this through out the " touched" level ADC reading by a factor of 1500 ( 12bit ) .

    when setting the " touched level " for what the switch is changing , i would highly recommend isolating external earths that may not be there when the unit is running , when setting your trigger level for the fuctions that the touch switch controls


    cheers

    sheldon

Similar Threads

  1. 16F726 Capacitive sensing module
    By Byte_Butcher in forum General
    Replies: 39
    Last Post: - 15th May 2014, 20:40
  2. Need help setting up Capacitive Sense 12F1822
    By Heckler in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th November 2013, 22:10
  3. Capacitive sensor using ADC
    By johnmiller in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st November 2010, 11:46
  4. Replies: 3
    Last Post: - 29th September 2010, 15:45
  5. mTouch capacitive sensing
    By jrprogrammer in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 1st November 2008, 23: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