佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 4089|回复: 10

【教学】 简易MCU 触碰开关

[复制链接]
发表于 10-5-2010 10:55 AM | 显示全部楼层 |阅读模式
本帖最后由 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

  1. /*
  2. Touch Switch
  3. Date : 10 March 2006

  4. Modify release to Cari Chinese Forum
  5. 10 May 2010
  6. */
  7. #include <16F630.H>
  8. #fuses  Intrc_IO,PROTECT,WDT,nomclr,put,brownout
  9. #use delay(clock=4000000, restart_wdt)

  10. #bit rapu=0x81.7

  11. #use fast_io(A)
  12. #byte PortA = 5
  13. #byte WPUA =0x95  
  14. #bit rapu=0x81.7

  15. #bit LED    = PortA.0
  16. #bit Out    = PortA.1
  17. #bit In     = PortA.5
  18. int x;
  19. int y;
  20. int z;
  21. int fTouchDown;

  22. void GetInput()
  23. {
  24.       set_tris_A(0b00000100); // Set Port A5 as output
  25.       in=0;// we discharge the pin A5
  26.       delay_us(500); // wait it to discharge
  27.       set_tris_A(0b00100100); // Set port A5 as input
  28.       delay_us(50); // Wait for for time , this timing determine sensitivity

  29.       x=in;  // we read the pin A5
  30. }

  31. void main()
  32. {
  33.    set_tris_A(0b00000100);


  34.    // Do nothing, just blick LED to indicate program start
  35.    for(x=0;x<5;x++)
  36.    {
  37.       Led=1; delay_us(10); out=0;     delay_ms(50);
  38.       Led=0; delay_us(10); out=0;     delay_ms(50);
  39.    }


  40.    out=0;  // This is output, can be control relay or whatever
  41.    y=0;
  42.    fTouchDown=0;
  43.    rapu=0; // Enable Weak Pull up

  44.    //      76543210
  45.    WPUA =0b00000100;// individual  Weak Pull up on A2
  46.    // Above line is added due to compiler bug, so I control manually   


  47.    while(1)
  48.    {

  49.       restart_wdt();
  50.       GetInput();

  51.       //touch-- If someone touch it, the input will read Low
  52.       if(x==0)
  53.       {
  54.          LED=1;
  55.          delay_ms(5);
  56.          Led=0;
  57.          if(y<200){y++;}  // We count how many time the pin is hand touch
  58.          z=0;
  59.       }
  60.       else
  61.       {
  62.          delay_ms(1);
  63.          if(Z<200){Z++;}
  64.          if(Z>150)   // We count how many time there is no hand touch
  65.          {
  66.             fTouchDown=0;  // We confirm there is no touch after some time
  67.             y=0;
  68.          }
  69.       }

  70.       if (y>15) // we confirm there is hand touch
  71.       {
  72.          if(!fTouchDown)
  73.          {
  74.             fTouchDown=1;
  75.             Led=0;
  76.             out=1; // We turn on an output
  77.             delay_ms(2000); //for 2 sec
  78.             out=0;  // then off it again
  79.             y=0;
  80.          }
  81.       }

  82.    }
  83. }
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
想问,如何让一个capacitor fully charge?如果给5V给capacitor的两的脚,那capacitor可以被charge到5V吗?
东邪西毒 发表于 4-6-2010 06:24 PM

可以。

如果你有类似这样的问题, 请发到
[交流] 电子技术交流区
http://cforum3.cari.com.my/viewthread.php?tid=1530722
回复

使用道具 举报

发表于 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


图没问题啊~ 我这里可以看到~其他人看的到图吗?
回复

使用道具 举报

Follow Us
发表于 8-9-2010 04:55 PM | 显示全部楼层
不知道做什么,我用IE / Firefox也是看不到电路图。
回复

使用道具 举报

发表于 8-9-2010 05:40 PM | 显示全部楼层
我看到啊。。。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 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 | 显示全部楼层
今天我可以看到图片了,谢谢各位。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 26-4-2024 03:16 PM , Processed in 0.079704 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表