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
Bookmarks