邓维安 发表于 2012-9-17 15:51:49

触摸—capacitive touch

不要崇拜哥,哥为人民服务,哥已不在社区,但社区每一个角落都流传着哥的传说。
OK,装B的废话哥已经说完了。镜头转向摸摸板capacityve touch.

先上个图





OK,上完图之后就要上代码了,这是官方的demo

/*
TouchWheel.pde
MPR121 WhellPad Example Code

by:Waiman Zhao
Mail:[email protected]
created on: 11/2/14

      SDA -> A4
      SCL -> A5
      IRQ -> D2
*/
#include <Wire.h>
#include"mpr121.h"

int pin = 13;
volatile int state = LOW;
int key = 0;

void setup()
{
Serial.begin(9600);
Wire.begin();
mpr121QuickConfig();
delay(500);
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, FALLING);
}

void loop()
{
digitalWrite(pin, state);
if(state==HIGH){
    key=wheelKey();
    if(key>=0){
    Serial.print("wheelKey:\t");
    Serial.println(key,DEC);
    }
   
    key=keypad();
    if(key>=0){
    Serial.print("keypad:\t");
    Serial.println(key,DEC);
    }
    state = LOW;
}
}

void blink()
{
state =HIGH;
}


上完代码了就该上效果图了。



那么有同学就会想,上完效果图之后,你还能上什么呢?
没错,哥上完效果图还可以上的就是





当,当,当,当,当.......












邓维安 发表于 2012-9-17 15:53:26

附上摸摸板的库文件

迷你强 发表于 2012-9-17 17:30:27

{:soso_e136:}貌似是大神那

Randy 发表于 2012-9-17 17:44:23

邓大哥一直都是这样的大神,膜拜了。。。

friskit 发表于 2012-9-18 07:02:58

话说上面那个cad的软件是啥?为啥还有dfrobot的模块。。。

darkorigin 发表于 2012-9-18 22:38:09

friskit 发表于 2012-9-18 07:02 static/image/common/back.gif
话说上面那个cad的软件是啥?为啥还有dfrobot的模块。。。

CAD应该是计算机辅助设计 难道说的是AOTO CAD ?
DFROBOT 是一个成品ARDUINO的品牌,国内做的很多,哈尔滨的一个大学的一帮人做的,配件很全面~~

friskit 发表于 2012-9-21 23:14:58

darkorigin 发表于 2012-9-18 22:38 static/image/common/back.gif
CAD应该是计算机辅助设计 难道说的是AOTO CAD ?
DFROBOT 是一个成品ARDUINO的品牌,国内做的很多,哈尔滨 ...

呃。。我是在说那个画图的软件是啥……dfrobot的模型都有……

Keke 发表于 2014-1-12 16:04:25

邓大神,斗胆改了你的程序(mpr121.h),现在的arduino已经不在支持WProgram.h,WConstants.h,而且wire里面也改为了read和write,否则编译不过了。
我一直没有搞清楚这个wheelKey()是干什么的,能否解释一下,实在不想去看datasheet了,万谢,万谢!/*
MPR121.h
MPR121 WhellPad Example Code

by:Waiman Zhao
Mail:[email protected]
created on: 11/2/14
license: CC-SA 3.0

By Keke Zhou
Mail: [email protected]
Modified on: Jan 12th, 2014

Hardware: 3.3V Arduino Pro Mini
         SDA -> A4
         SCL -> A5
         IRQ -> D2
*/

#include <Wire.h>
#include <Arduino.h>


#define MPR121 0x5B        // ADD pin is Vcc So address is 0x5B

// MPR121 Register Defines
#define E0BV        0x1E
#define E1BV        0x1F
#define E2BV        0x20
#define E3BV        0x21
#define E4BV        0x22
#define E5BV        0x23
#define E6BV        0x24
#define E7BV        0x25
#define E8BV        0x26
#define E9BV        0x27
#define E10BV        0x28
#define E11BV        0x29
#define E12BV        0x2A


#define MHD_R        0x2B
#define NHD_R        0x2C
#define        NCL_R         0x2D
#define        FDL_R        0x2E
#define        MHD_F        0x2F
#define        NHD_F        0x30
#define        NCL_F        0x31
#define        FDL_F        0x32


#define        ELE0_T        0x41
#define        ELE0_R        0x42
#define        ELE1_T        0x43
#define        ELE1_R        0x44
#define        ELE2_T        0x45
#define        ELE2_R        0x46
#define        ELE3_T        0x47
#define        ELE3_R        0x48
#define        ELE4_T        0x49
#define        ELE4_R        0x4A
#define        ELE5_T        0x4B
#define        ELE5_R        0x4C
#define        ELE6_T        0x4D
#define        ELE6_R        0x4E
#define        ELE7_T        0x4F
#define        ELE7_R        0x50
#define        ELE8_T        0x51
#define        ELE8_R        0x52
#define        ELE9_T        0x53
#define        ELE9_R        0x54
#define        ELE10_T        0x55
#define        ELE10_R        0x56
#define        ELE11_T        0x57
#define        ELE11_R        0x58


#define        AFE1_CFG        0x5C
#define        AFE2_CFG        0x5D
#define        ELE_CFG                0x5E


#define GPIO_CTRL0        0x73
#define        GPIO_CTRL1        0x74
#define GPIO_DATA        0x75
#define        GPIO_DIR        0x76
#define        GPIO_EN                0x77
#define        GPIO_SET        0x78
#define        GPIO_CLEAR        0x79
#define        GPIO_TOGGLE        0x7A


#define        ATO_CFG0        0x7B
#define        ATO_CFGU        0x7D
#define        ATO_CFGL        0x7E
#define        ATO_CFGT        0x7F
#define        RES_CF          0x80

// Global Constants        //隔住阻焊层
#define TOU_THRESH        0x06
#define        REL_THRESH        0x03
#define        START_BV        0x90
// Global Constants        //隔住0.5mm
//#define TOU_THRESH        0x06
//#define        REL_THRESH        0x03



void mpr121Write(uint8_t reg,uint8_t data){
   Wire.beginTransmission((uint8_t)MPR121);
   Wire.write(reg);
   Wire.write(data);
   Wire.endTransmission();
}

void mpr121Read(uint8_t reg,uint8_t length,uint8_t *ReturnData)
{
   Wire.beginTransmission(MPR121);
   Wire.write(reg);
   //Wire.endTransmission();
   Wire.requestFrom((uint8_t)MPR121, length);
   uint8_t i=0;
   while(Wire.available())    // slave may send less than requested
   {
   ReturnData = Wire.read(); // receive a byte as character
   i++;
   }
}

int readTouch()
{
   Wire.requestFrom((uint8_t)MPR121,(uint8_t)2);
   uint8_t a,b;
   if(Wire.available()>=2)    // slave may send less than requested
   {
   a = Wire.read(); // receive a byte as character
   b = Wire.read();
   return (a | (int)(b<<8));
   }else
   {
   return 0;
   }   
}

void mpr121QuickConfig(void){
        // STOP
        mpr121Write(ELE_CFG, 0x00);
      mpr121Write(RES_CF, 0X63);
      delay(10);
        // Section A
        // This group controls filtering when data is > baseline.
        mpr121Write(MHD_R, 0x01);
        mpr121Write(NHD_R, 0x01);
        mpr121Write(NCL_R, 0x00);
        mpr121Write(FDL_R, 0x00);

        // Section B
        // This group controls filtering when data is < baseline.
        mpr121Write(MHD_F, 0x01);
        mpr121Write(NHD_F, 0x01);
        mpr121Write(NCL_F, 0xFF);
        mpr121Write(FDL_F, 0x02);

        // Section C
        // This group sets touch and release thresholds for each electrode
        mpr121Write(ELE0_T, TOU_THRESH);
        mpr121Write(ELE0_R, REL_THRESH);
        mpr121Write(ELE1_T, TOU_THRESH);
        mpr121Write(ELE1_R, REL_THRESH);
        mpr121Write(ELE2_T, TOU_THRESH);
        mpr121Write(ELE2_R, REL_THRESH);
        mpr121Write(ELE3_T, TOU_THRESH);
        mpr121Write(ELE3_R, REL_THRESH);
        mpr121Write(ELE4_T, TOU_THRESH);
        mpr121Write(ELE4_R, REL_THRESH);
        mpr121Write(ELE5_T, TOU_THRESH);
        mpr121Write(ELE5_R, REL_THRESH);
        mpr121Write(ELE6_T, TOU_THRESH);
        mpr121Write(ELE6_R, REL_THRESH);
        mpr121Write(ELE7_T, TOU_THRESH);
        mpr121Write(ELE7_R, REL_THRESH);
        mpr121Write(ELE8_T, TOU_THRESH);
        mpr121Write(ELE8_R, REL_THRESH);
        mpr121Write(ELE9_T, TOU_THRESH);
        mpr121Write(ELE9_R, REL_THRESH);
        mpr121Write(ELE10_T, TOU_THRESH);
        mpr121Write(ELE10_R, REL_THRESH);
        mpr121Write(ELE11_T, TOU_THRESH);
        mpr121Write(ELE11_R, REL_THRESH);

        // Section D
        // Set the Filter Configuration
        // Set ESI2
        mpr121Write(AFE1_CFG, 0x10);
        //AFE配置1 (默认=0x10)
        //        一级采样FFI:00-6次
        //        充放电电流CDC:100000 - 32uA
        mpr121Write(AFE2_CFG, 0x04);
        //AFE配置2        (默认=0x24)
        //        充电时间CDT:010 - 1us
        //        二级采样SFI:00 - 4次
        //        二级采样间隔ESI:100 - 16ms


        // Section E
        // Electrode Configuration
        // Enable 6 Electrodes and set to run mode
        // Set ELE_CFG to 0x00 to return to standby mode
        mpr121Write(ELE_CFG, 0x8C);        // Enables all 12 Electrodes

        //AFE配置1
        //        CL
        //        ELEPROX
        //        ELE:
        // Section F
        // Enable Auto Config and auto Reconfig
        /*mpr121Write(ATO_CFG0, 0x0B);
        mpr121Write(ATO_CFGU, 0xC9);        // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V   mpr121Write(ATO_CFGL, 0x82);        // LSL = 0.65*USL = 0x82 @3.3V
        mpr121Write(ATO_CFGT, 0xB5);*/        // Target = 0.9*USL = 0xB5 @3.3V
}


int8_t wheelKey(void)
{
      switch(readTouch())
      {
                case 0x0001:
                        return 1;
                break;
                case 0x0003:
                        return 2;
                break;
                case 0x0002:
                        return 3;
                break;
                case 0x0006:
                        return 4;
                break;
                case 0x0004:
                        return 5;
                break;
                case 0x000C:
                        return 6;
                break;
                case 0x0008:
                        return 7;
                break;
                case 0x0018:
                        return 8;
                break;
                case 0x0010:
                        return 9;
                break;
                case 0x0030:
                        return 10;
                break;
                case 0x0020:
                        return 11;
                break;
                case 0x0060:
                        return 12;
                break;
                case 0x0040:
                        return 13;
                break;
                case 0x00C0:
                        return 14;
                break;
                case 0x0080:
                        return 15;
                break;
                case 0x0081:
                        return 16;
                break;
                default:
                        return -1;
                break;               
      }
}


int8_t keypad(void)
{
      switch(readTouch())
      {
                case 0x0001:
                        return 1;
                break;
                case 0x0002:
                        return 4;
                break;
                case 0x0004:
                        return 7;
                break;
                case 0x0008:
                        return 11;
                break;
                case 0x0010:
                        return 2;
                break;
                case 0x0020:
                        return 5;
                break;
                case 0x0040:
                        return 8;
                break;
                case 0x0080:
                        return 0;
                break;
                case 0x0100:
                        return 3;
                break;
                case 0x0200:
                        return 6;
                break;
                case 0x0400:
                        return 9;
                break;
                case 0x0800:
                        return 12;
                break;
                default:
                        return -1;
                break;               
      }
}

邓维安 发表于 2014-2-13 09:24:36

Keke 发表于 2014-1-12 16:04 static/image/common/back.gif
邓大神,斗胆改了你的程序(mpr121.h),现在的arduino已经不在支持WProgram.h,WConstants.h,而且wire里面 ...

wheelKey()函数,就是你做按键滑动效果的参考值,它是由一连串的触发感应到的值,不是固定的一个值,我是这么理解的。

Keke 发表于 2014-2-14 09:07:06

邓维安 发表于 2014-2-13 09:24 static/image/common/back.gif
wheelKey()函数,就是你做按键滑动效果的参考值,它是由一连串的触发感应到的值,不是固定的一个值,我是 ...

wheelkey和keypad两个函数连续调用,如果keypad读出来的是按键标示,那第一个wheelkey究竟读出的值具体有什么含义呢?你所说的这个滑动效果参考值究竟是什么呢?

邓维安 发表于 2014-2-17 08:20:27

Keke 发表于 2014-2-14 09:07 static/image/common/back.gif
wheelkey和keypad两个函数连续调用,如果keypad读出来的是按键标示,那第一个wheelkey究竟读出的值具体有 ...

keypad和wheelkey都是读按键值,你可以这样理解,keypad定义的是单键触摸的值,wheelkey定义是两个键同时触摸的值。这么简单的问题,你要自已看啊。要自已动手去做,脑袋去想,才能解决疑问。
页: [1]
查看完整版本: 触摸—capacitive touch