查看: 4181|回复: 10
|
【教学】 简易MCU 触碰开关
[复制链接]
|
|
本帖最后由 pic 于 10-5-2010 01:50 PM 编辑
最近, 有网友问到触碰开关, 如何用MCU 来实现?
最好的答案就是用Microchip 的mTouch 技术。
Microchip 的mTouch。
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2695
但是, 在Microchip 推出mTouch 技术之前, 人们是怎样做的呢? 会难吗? 其实很容易。
这里就来介绍, 先声明这是旧技术,这是mTouch 以外的旧方法。
首先,有什么要求?很简单, 只需要一个I/O, 上拉电阻, 你的触碰金属, 几乎任何的MCU 都可以做到。
不需要ADC, 甚至不用Timer, 只是简单的I/O做 充电, 放电, 延时delay, 就可以了。
原理如下,看下图, 人体, 就是一个无形的电容。
只要用电容, 不管大小, 通过470K 的电阻充电, 就需要时间。
通过时间的测量,可以知道有没有触碰。

“寄生”电容
操作流程如下:
1. 先把MCU 的pin 设定输出output, 并输出Logic 0, 等一段时间 (500uS)放电Discharge,
这时, 这个MCU 的pin 是接近 0V了。
2. 现在把MCU 的pin 设定成Input, 这时会变成高输入阻抗,
这时会有两种情形。
a) 没有触碰的话, 没有电容, 因为上拉电阻470K, 这时MCU Pin 会变成5V, 读成Logic High。
b) 有触碰的话, 就有“寄生”电容, 。 电容一开始是没有电荷(charge)的, 所以会通过上拉电阻470K 来充电。
这需要一些时间, 在充电完成前, MCU 是会“连续”读到Low logic 的, 才变成Logic High。
从这里,计算看“连续” 读到几次的Low, 我们可以假设是不是有碰触了。
电路图

源码开放, 是在2006年写的, 用16F630, 其实大家可以用任何一种PIC MCU
- /*
- Touch Switch
- Date : 10 March 2006
- Modify release to Cari Chinese Forum
- 10 May 2010
- */
- #include <16F630.H>
- #fuses Intrc_IO,PROTECT,WDT,nomclr,put,brownout
- #use delay(clock=4000000, restart_wdt)
- #bit rapu=0x81.7
- #use fast_io(A)
- #byte PortA = 5
- #byte WPUA =0x95
- #bit rapu=0x81.7
- #bit LED = PortA.0
- #bit Out = PortA.1
- #bit In = PortA.5
- int x;
- int y;
- int z;
- int fTouchDown;
- void GetInput()
- {
- set_tris_A(0b00000100); // Set Port A5 as output
- in=0;// we discharge the pin A5
- delay_us(500); // wait it to discharge
- set_tris_A(0b00100100); // Set port A5 as input
- delay_us(50); // Wait for for time , this timing determine sensitivity
- x=in; // we read the pin A5
- }
- void main()
- {
- set_tris_A(0b00000100);
- // Do nothing, just blick LED to indicate program start
- for(x=0;x<5;x++)
- {
- Led=1; delay_us(10); out=0; delay_ms(50);
- Led=0; delay_us(10); out=0; delay_ms(50);
- }
- out=0; // This is output, can be control relay or whatever
- y=0;
- fTouchDown=0;
- rapu=0; // Enable Weak Pull up
- // 76543210
- WPUA =0b00000100;// individual Weak Pull up on A2
- // Above line is added due to compiler bug, so I control manually
- while(1)
- {
- restart_wdt();
- GetInput();
- //touch-- If someone touch it, the input will read Low
- if(x==0)
- {
- LED=1;
- delay_ms(5);
- Led=0;
- if(y<200){y++;} // We count how many time the pin is hand touch
- z=0;
- }
- else
- {
- delay_ms(1);
- if(Z<200){Z++;}
- if(Z>150) // We count how many time there is no hand touch
- {
- fTouchDown=0; // We confirm there is no touch after some time
- y=0;
- }
- }
- if (y>15) // we confirm there is hand touch
- {
- if(!fTouchDown)
- {
- fTouchDown=1;
- Led=0;
- out=1; // We turn on an output
- delay_ms(2000); //for 2 sec
- out=0; // then off it again
- y=0;
- }
- }
- }
- }
复制代码 |
|
|
|
|
|
|
|
发表于 12-5-2010 01:56 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 4-6-2010 06:24 PM
|
显示全部楼层
想问,如何让一个capacitor fully charge?如果给5V给capacitor的两的脚,那capacitor可以被charge到5V吗? |
|
|
|
|
|
|
|

楼主 |
发表于 6-6-2010 08:28 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 8-9-2010 12:28 PM
|
显示全部楼层
最近, 有网友问到触碰开关, 如何用MCU 来实现?
最好的答案就是用Microchip 的mTouch 技术。
Microchip ...
pic 发表于 10-5-2010 10:55 AM 
不好意识,我看不到电路图。
谢谢。 |
|
|
|
|
|
|
|

楼主 |
发表于 8-9-2010 01:32 PM
|
显示全部楼层
不好意识,我看不到电路图。
谢谢。
qweeer 发表于 8-9-2010 12:28 PM 
图没问题啊~ 我这里可以看到~其他人看的到图吗? |
|
|
|
|
|
|
|
发表于 8-9-2010 04:55 PM
|
显示全部楼层
不知道做什么,我用IE / Firefox也是看不到电路图。
 |
|
|
|
|
|
|
|
发表于 8-9-2010 05:40 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 8-9-2010 06:21 PM
|
显示全部楼层
不知道做什么,我用IE / Firefox也是看不到电路图。
qweeer 发表于 8-9-2010 04:55 PM 
奇怪了~网站有问题? 你能够看到我的头像吗?
或你的browser block 了 图?
如果还是不可以, 不要紧, PM 你的email 给我, 寄给你好了。。 |
|
|
|
|
|
|
|
发表于 9-9-2010 01:02 PM
|
显示全部楼层
头像就看到,其他在cari的图片都看到,就是着看不到这个,我其他的电脑也看不到。
不懂是不是我的streamys网路的问题。我今晚再试其他的网路看看。
谢谢你们先。 |
|
|
|
|
|
|
|
发表于 13-9-2010 11:19 PM
|
显示全部楼层
今天我可以看到图片了,谢谢各位。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|