mTouch cap sense within 16F1937
Posted: Sun May 25, 2014 5:33 pm
Flowcode Version = V6
Important. If you get a error when compiling like:
Then you will require a fcdx file update from here
Hardware Required:
Cap Touch Keypad EB088
PIC Multiprogrammer EB006 With 16F1937. J18/J19 set to A6&A7
LED Board EB004
I have created an attached a Flowchart with as less C code that I can get away with for use with Cap Touch Keypad and mTouch cap sense module of 16F1937.
For those who have not got a EB006 programmer the internal oscillator is used to simplify circuitry.
The flowchart has comments to help understand how mTouch cap sense module (CTM) works.
Note:
The center pad is more sensitive because has a larger copper area then the other four pads.
Multiple pads can be activated at the same time.
If not using Cap Touch Keypad, then you may either need to adjust sensitivity or use the flowchart from the post below that uses a pot to adjust sensitivity.
I have only just learned about the mTouch cap sense module today, so Will will do my best to type a description on how it all works so this is work in progess:
I will be using binary as this makes it easer to compare with registers on the data sheet.
First Internal osc was set for 16MHz:
Input's for Cap Sense require setting up.
These channels are available for use with 16F1937: So I just placed a portB input icon with any variable set up for portB0 to portB4
Both timer0 and timer1 require enabling.
Timer1 will require gating set up.
Which means it can be automatic gated (started & stopped) by Timer0
Gating is enabled by the T1GCON register. The data sheet shows: So T1GCON register has to be set for:
Bit 7 =1 = Timer1 counting is controlled by the Timer1 gate function.
Bit 6 = 1 = Timer1 gate is active-high (Timer1 counts when gate is high).
Bit 5 = 1 = Timer1 Gate Toggle mode is enabled.
Bit 4 = 0 = Timer1 gate Single-Pulse mode is disabled.
Bit 1 to 0 = 01 = Timer0 overflow output
So on each overflow of timer0, timer1 interrupt is toggled between enabled and not enabled.
Timer1 also requires to select source for cap sens osc
This is done with t1con register: By setting bits 7 & bits 6 to 1
Also prescaler is set to 1:1 and bit 2 is set so: Do not synchronize external clock input :
Next the cpscon0 register is used to activate the cap sense module:
This register is set as:
From looking at the datasheet:
Bit 7 = 1 = CPS module is enabled etc.
I have used the timer1 interrupt icon to eliminate the requirement for writing interrupt handler codes.
What happens is:
timer0 counts then overflows, which call the timer0 interrupt macro called Timer0Int.
The previous value of timer1 osc is stored.
at this stage since timer1 is gated it will either be just started running or just stopped.
The value of 16bit timer is read and stored in Timer1OscVal
By
since timer1 could either be started or stopped the value of timer1 could either mean nothing, or it could hold the true value.
Because timer1 is always reset before timer0 interrupt has exited using:
Then the value of timer1 could be 0, since timer1 may not have been running.
If that is the case then Timer1OscVal is assigned with T1PreviousValue.
The Value of Timer1OscVal is compared with a fixed value.
As the finger gets nearer the conductor that connected to the cap sense i/p the cap sense osc slows down so timer1 is not triggered as much within the timer0 set period.
The result being time1 value drops
So when this timer1 value drops below the fixed value, then LED connected to the current active channel Lights up.
Before the timer0 ISR is exited, only if the value of Timer1OscVal <100 then Cap sence channel is incremented by 1 so the new channel is being detected.
If the interest is there perhaps the nest step is to create a user component with selectable channels and sensitivity variable.
Martin
Important. If you get a error when compiling like:
Code: Select all
???(743): Unterminated conditional
1 errors detected
Error: preprocessing error
Hardware Required:
Cap Touch Keypad EB088
PIC Multiprogrammer EB006 With 16F1937. J18/J19 set to A6&A7
LED Board EB004
I have created an attached a Flowchart with as less C code that I can get away with for use with Cap Touch Keypad and mTouch cap sense module of 16F1937.
For those who have not got a EB006 programmer the internal oscillator is used to simplify circuitry.
The flowchart has comments to help understand how mTouch cap sense module (CTM) works.
Code: Select all
Sensor LED
Touched Lit
P1(Up) D0
P2(Right) D3
P3(Down) D1
P4(Left) D2
P5(Center) D4
The center pad is more sensitive because has a larger copper area then the other four pads.
Multiple pads can be activated at the same time.
If not using Cap Touch Keypad, then you may either need to adjust sensitivity or use the flowchart from the post below that uses a pot to adjust sensitivity.
I have only just learned about the mTouch cap sense module today, so Will will do my best to type a description on how it all works so this is work in progess:
I will be using binary as this makes it easer to compare with registers on the data sheet.
First Internal osc was set for 16MHz:
Code: Select all
osccon=0x78;
These channels are available for use with 16F1937: So I just placed a portB input icon with any variable set up for portB0 to portB4
Both timer0 and timer1 require enabling.
Timer1 will require gating set up.
Which means it can be automatic gated (started & stopped) by Timer0
Gating is enabled by the T1GCON register. The data sheet shows: So T1GCON register has to be set for:
Bit 7 =1 = Timer1 counting is controlled by the Timer1 gate function.
Bit 6 = 1 = Timer1 gate is active-high (Timer1 counts when gate is high).
Bit 5 = 1 = Timer1 Gate Toggle mode is enabled.
Bit 4 = 0 = Timer1 gate Single-Pulse mode is disabled.
Bit 1 to 0 = 01 = Timer0 overflow output
Code: Select all
t1gcon=0b11100001;
Timer1 also requires to select source for cap sens osc
This is done with t1con register: By setting bits 7 & bits 6 to 1
Also prescaler is set to 1:1 and bit 2 is set so: Do not synchronize external clock input :
Code: Select all
t1con=0b11000101;
Code: Select all
cpscon0=0b10001100;
Bit 7 = 1 = CPS module is enabled etc.
I have used the timer1 interrupt icon to eliminate the requirement for writing interrupt handler codes.
What happens is:
timer0 counts then overflows, which call the timer0 interrupt macro called Timer0Int.
The previous value of timer1 osc is stored.
at this stage since timer1 is gated it will either be just started running or just stopped.
The value of 16bit timer is read and stored in Timer1OscVal
By
Code: Select all
FCV_TIMER1OSCVAL = tmr1l+(tmr1h << 8);
Because timer1 is always reset before timer0 interrupt has exited using:
Code: Select all
tmr1h=0
tmr1l=0;
If that is the case then Timer1OscVal is assigned with T1PreviousValue.
The Value of Timer1OscVal is compared with a fixed value.
As the finger gets nearer the conductor that connected to the cap sense i/p the cap sense osc slows down so timer1 is not triggered as much within the timer0 set period.
The result being time1 value drops
So when this timer1 value drops below the fixed value, then LED connected to the current active channel Lights up.
Before the timer0 ISR is exited, only if the value of Timer1OscVal <100 then Cap sence channel is incremented by 1 so the new channel is being detected.
If the interest is there perhaps the nest step is to create a user component with selectable channels and sensitivity variable.
Martin