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
Bookmarks