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
Bookmarks