mTouch cap sense within 16F1937

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

mTouch cap sense within 16F1937

Post by medelec35 »

Flowcode Version = V6

Important. If you get a error when compiling like:

Code: Select all

???(743): Unterminated conditional
1 errors detected
Error: preprocessing error
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.

Code: Select all

Sensor     LED
 Touched    Lit 
P1(Up)     D0
P2(Right)  D3
P3(Down)   D1
P4(Left)   D2
P5(Center) D4 
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:

Code: Select all

osccon=0x78;
Input's for Cap Sense require setting up.
These channels are available for use with 16F1937:
Cap Sense1.png
(8.58 KiB) Downloaded 11410 times
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:
Cap Sense2.png
(122.65 KiB) Downloaded 11410 times
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;
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:
Cap Sense3.png
(37.17 KiB) Downloaded 11404 times
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;
Next the cpscon0 register is used to activate the cap sense module:
Cap Sense4.png
(129.64 KiB) Downloaded 11383 times
This register is set as:

Code: Select all

cpscon0=0b10001100;
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

Code: Select all

 FCV_TIMER1OSCVAL = tmr1l+(tmr1h << 8);
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:

Code: Select all

tmr1h=0
tmr1l=0;
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
Attachments
mTouch Cap Sense1.fcfx
(10.31 KiB) Downloaded 628 times
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: mTouch cap sense within 16F1937

Post by medelec35 »

Attached is a flowchart which has a pot connected for a sensitivity control.
I just had a single square 30x30mm cut from tin foil connected to one of the cap sense inputs.
Adjusting the sensitivity, the LED was reliably activated with the finger 40mm above the foil.
There was no ground plane anywhere near the foil.
So sensitivity for cap sense modules are excellent!

Martin
Attachments
mTouch Cap Sense2.fcfx
(12.53 KiB) Downloaded 582 times
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: mTouch cap sense within 16F1937

Post by JohnCrow »

Hi Martin

When I try to compile your programs I'm getting the following error.
mTouch Cap Sense2.c
Starting preprocessor: "C:\Program Files (x86)\Matrix Multimedia\Flowcode V6\compilers\pic\boostc\pp.exe" "mTouch Cap Sense2.c" -i "C:\Program Files (x86)\Matrix Multimedia\Flowcode V6\compilers\pic\boostc\include" -d _PIC16F1937 -la -c2 -o "mTouch Cap Sense2.pp" -v -d _BOOSTC -d _PIC16 -d _PIC16x -d _CHAR_INDEX
???(743): Unterminated conditional
1 errors detected
Error: preprocessing error
failure
Completed BoostC compilation, return = 1
C:\Program Files (x86)\Matrix Multimedia\Flowcode V6\compilers\pic\boostc\boostc_16F.exe reported error code 1
FINISHED
Using FC Version 6.0.7.0 and as far as I know all the latest patches.

It also gives the same error if I try compile to hex rather than to device.
Tried on the laptop with same results too .
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: mTouch cap sense within 16F1937

Post by medelec35 »

Hi John,
JohnCrow wrote:When I try to compile your programs I'm getting the following error.

mTouch Cap Sense2.c
Starting preprocessor: "C:\Program Files (x86)\Matrix Multimedia\Flowcode V6\compilers\pic\boostc\pp.exe" "mTouch Cap Sense2.c" -i "C:\Program Files (x86)\Matrix Multimedia\Flowcode V6\compilers\pic\boostc\include" -d _PIC16F1937 -la -c2 -o "mTouch Cap Sense2.pp" -v -d _BOOSTC -d _PIC16 -d _PIC16x -d _CHAR_INDEX
???(743): Unterminated conditional
1 errors detected
Error: preprocessing error
failure
Completed BoostC compilation, return = 1
C:\Program Files (x86)\Matrix Multimedia\Flowcode V6\compilers\pic\boostc\boostc_16F.exe reported error code 1
FINISHED
Looks like you're missing the patch on this page
Without the updated 16F1937.fcdx you will get an error when using timer1 interrupt.

Thanks for the feedback John,
I have edited the tutorial to include information about required patch.

Martin
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: mTouch cap sense within 16F1937

Post by JohnCrow »

Hi Martin

I've had to change your program slightly

Timer1OscVal to 800 to get the first program to work.
Otherwise all 5 LEDs are on all the time.

The second program works as is.

Will get another 1937 chip and try that later see if it makes a difference
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: mTouch cap sense within 16F1937

Post by medelec35 »

Hi John
JohnCrow wrote:I've had to change your program slightly

Timer1OscVal to 800 to get the first program to work.
It could be dependent on electrical environment surrounding the sensor board.
So I would assume version 2 would always be better so you can trim the sensitivity depending on the electrical surroundings?

I'm only guessing of course :-)

At least your flowchart compiles now, and you have got the hardware working.
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: mTouch cap sense within 16F1937

Post by JohnCrow »

Hi Martin
It could be dependent on electrical environment surrounding the sensor board.
So I would assume version 2 would always be better so you can trim the sensitivity depending on the electrical surroundings?
Im on a wood bench top about 1M from the PC
I get the same results with a different 16F1937.

My other thoughts are the sensor board itself.
Thickness of the copper tracks / pcb or area of the pads etc. these would effect the capacitance

On my board the 4 arrow pads are about 13pF and the center is 15pF
When touched they go up to 19pF (arrows) and 24pF center

Measured to the 9-pin D connector ground plane connections
Measured using my Peak LCR40

Main thing is its working :D
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: mTouch cap sense within 16F1937

Post by medelec35 »

Hi John,
I have just started working on an improved version which can send data via UART on all the sensor readings and will also indicate what channels have been triggered.

I will post when it's completed.

Martin
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: mTouch cap sense within 16F1937

Post by JohnCrow »

Thanks Martin

I did try it with a 16F1939 and found I got a different values on the sensitivity.
16F1937 = 600 to 850
16F1939 = 750 1060

Though the span is still roughly the same
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: mTouch cap sense within 16F1937

Post by medelec35 »

Hi John,
JohnCrow wrote:I did try it with a 16F1939 and found I got a different values on the sensitivity.
16F1937 = 600 to 850
16F1939 = 750 1060
That is interesting.
Thanks for that.
Attached as promised is an improved version that will send RS232 Data of all values and if sensor channels has triggered port or not.
I have got a log of the transmitted data when I pressed one pad at a time, then all four at the same time:

Code: Select all

Ch 3  Val 3196 Sens 3135 Triggered? No
Ch 4  Val 3153 Sens 3135 Triggered? No
Ch 0  Val 3327 Sens 3135 Triggered? No
Ch 1  Val 3323 Sens 3135 Triggered? No
Ch 2  Val 3257 Sens 3135 Triggered? No
Ch 3  Val 3195 Sens 3135 Triggered? No
Ch 4  Val 3129 Sens 3135 Triggered? Yes
Ch 0  Val 3323 Sens 3135 Triggered? No
Ch 1  Val 3321 Sens 3135 Triggered? No
Ch 2  Val 3255 Sens 3135 Triggered? No
Ch 3  Val 3194 Sens 3135 Triggered? No
Ch 4  Val 3106 Sens 3135 Triggered? Yes
Ch 0  Val 3329 Sens 3135 Triggered? No
Ch 1  Val 3326 Sens 3135 Triggered? No
Ch 2  Val 3263 Sens 3135 Triggered? No
Ch 3  Val 3199 Sens 3135 Triggered? No
Ch 4  Val 3161 Sens 3135 Triggered? No
Ch 0  Val 3329 Sens 3135 Triggered? No
Ch 1  Val 3323 Sens 3135 Triggered? No
Ch 2  Val 3264 Sens 3135 Triggered? No
Ch 3  Val 3197 Sens 3135 Triggered? No
Ch 4  Val 3158 Sens 3135 Triggered? No
Ch 0  Val 3314 Sens 3135 Triggered? No
Ch 1  Val 2985 Sens 3135 Triggered? Yes
Ch 2  Val 3250 Sens 3135 Triggered? No
Ch 3  Val 3195 Sens 3135 Triggered? No
Ch 4  Val 3157 Sens 3135 Triggered? No
Ch 0  Val 3308 Sens 3135 Triggered? No
Ch 1  Val 3315 Sens 3135 Triggered? No
Ch 2  Val 3262 Sens 3135 Triggered? No
Ch 3  Val 3196 Sens 3135 Triggered? No
Ch 4  Val 3158 Sens 3135 Triggered? No
Ch 0  Val 3327 Sens 3135 Triggered? No
Ch 1  Val 3323 Sens 3135 Triggered? No
Ch 2  Val 3260 Sens 3135 Triggered? No
Ch 3  Val 3196 Sens 3135 Triggered? No
Ch 4  Val 3158 Sens 3135 Triggered? No
Ch 0  Val 3326 Sens 3135 Triggered? No
Ch 1  Val 3313 Sens 3135 Triggered? No
Ch 2  Val 2967 Sens 3135 Triggered? Yes
Ch 3  Val 3184 Sens 3135 Triggered? No
Ch 4  Val 3158 Sens 3135 Triggered? No
Ch 0  Val 3323 Sens 3135 Triggered? No
Ch 1  Val 3312 Sens 3135 Triggered? No
Ch 2  Val 2970 Sens 3135 Triggered? Yes
Ch 3  Val 3197 Sens 3135 Triggered? No
Ch 4  Val 3162 Sens 3135 Triggered? No
Ch 0  Val 3329 Sens 3135 Triggered? No
Ch 1  Val 3323 Sens 3135 Triggered? No
Ch 2  Val 3261 Sens 3135 Triggered? No
Ch 3  Val 3195 Sens 3135 Triggered? No
Ch 4  Val 3156 Sens 3135 Triggered? No
Ch 0  Val 3321 Sens 3135 Triggered? No
Ch 1  Val 3318 Sens 3135 Triggered? No
Ch 2  Val 3254 Sens 3135 Triggered? No
Ch 3  Val 3189 Sens 3135 Triggered? No
Ch 4  Val 3149 Sens 3135 Triggered? No
Ch 0  Val 2881 Sens 3135 Triggered? Yes
Ch 1  Val 3296 Sens 3135 Triggered? No
Ch 2  Val 3253 Sens 3135 Triggered? No
Ch 3  Val 3197 Sens 3135 Triggered? No
Ch 4  Val 3159 Sens 3135 Triggered? No
Ch 0  Val 3324 Sens 3135 Triggered? No
Ch 1  Val 3322 Sens 3135 Triggered? No
Ch 2  Val 3262 Sens 3135 Triggered? No
Ch 3  Val 3195 Sens 3135 Triggered? No
Ch 4  Val 3158 Sens 3135 Triggered? No
Ch 0  Val 3326 Sens 3135 Triggered? No
Ch 1  Val 3315 Sens 3135 Triggered? No
Ch 2  Val 3242 Sens 3135 Triggered? No
Ch 3  Val 2827 Sens 3135 Triggered? Yes
Ch 4  Val 3148 Sens 3135 Triggered? No
Ch 0  Val 3320 Sens 3135 Triggered? No
Ch 1  Val 3313 Sens 3135 Triggered? No
Ch 2  Val 3261 Sens 3135 Triggered? No
Ch 3  Val 3195 Sens 3135 Triggered? No
Ch 4  Val 3160 Sens 3135 Triggered? No
Ch 0  Val 3325 Sens 3135 Triggered? No
Ch 1  Val 3322 Sens 3135 Triggered? No
Ch 2  Val 3263 Sens 3135 Triggered? No
Ch 3  Val 3197 Sens 3135 Triggered? No
Ch 4  Val 3158 Sens 3135 Triggered? No
Ch 0  Val 3328 Sens 3135 Triggered? No
Ch 1  Val 3322 Sens 3135 Triggered? No
Ch 2  Val 3260 Sens 3135 Triggered? No
Ch 3  Val 3195 Sens 3135 Triggered? No
Ch 4  Val 3157 Sens 3135 Triggered? No
Ch 0  Val 3322 Sens 3135 Triggered? No
Ch 1  Val 3290 Sens 3135 Triggered? No
Ch 2  Val 2975 Sens 3135 Triggered? Yes
Ch 3  Val 2686 Sens 3135 Triggered? Yes
Ch 4  Val 2222 Sens 3135 Triggered? Yes
Ch 0  Val 2484 Sens 3135 Triggered? Yes
Ch 1  Val 2593 Sens 3135 Triggered? Yes
Ch 2  Val 2487 Sens 3135 Triggered? Yes
Ch 3  Val 2555 Sens 3135 Triggered? Yes
Ch 4  Val 2018 Sens 3135 Triggered? Yes
Ch 0  Val 2416 Sens 3135 Triggered? Yes
Ch 1  Val 2563 Sens 3135 Triggered? Yes
Ch 2  Val 2443 Sens 3135 Triggered? Yes
Ch 3  Val 2535 Sens 3135 Triggered? Yes
Ch 4  Val 1991 Sens 3135 Triggered? Yes
Ch 0  Val 2378 Sens 3135 Triggered? Yes
Ch 1  Val 2543 Sens 3135 Triggered? Yes
Ch 2  Val 2443 Sens 3135 Triggered? Yes
Ch 3  Val 2518 Sens 3135 Triggered? Yes
Ch 4  Val 1975 Sens 3135 Triggered? Yes
Note: the range is different due to altering timer0 prescaler rate.

The flowchart is versatile since I changed the config setting to use the EB006 built-in 19.66MHz crystal and there was no issues.

Before compiling to chip, it may be necessary to alter RS232 settings so it's suitable for your own setup.

Martin
Attachments
mTouch Cap Sense3.fcfx
(20.31 KiB) Downloaded 580 times
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: mTouch cap sense within 16F1937

Post by JohnCrow »

Hi Martin

Just had a quick look at the flowchart, and will have a try of this over the weekend.
Just a quick question, are you using the RS232-USB (EB055) with the PICs internal UART?, the full RS232-USB EB015 interface board, for connection to the PC
1 in 10 people understand binary, the other one doesn't !

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: mTouch cap sense within 16F1937

Post by medelec35 »

Hi John,
JohnCrow wrote:are you using the RS232-USB (EB055) with the PICs internal UART?, the full RS232-USB EB015 interface board, for connection to the PC
At the time I did not use any RS232Eblock as I was at work and Eblock was at home.
I just used a standard CP210x USB to UART Bridge VCP and used software driven uart as I have a convenient pin to connect it to.
Of course you can set to hardware uart via TX pin if you like.
Martin

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 364 times
Been thanked: 716 times
Contact:

Re: mTouch cap sense within 16F1937

Post by JohnCrow »

Thanks Martin
I've got something similar, I'll give that a go as well
1 in 10 people understand binary, the other one doesn't !

Post Reply