A sample library to get the MPR121-based touch board from SparkFun up and running. This code is based on the sample Arduino code from SparkFun but is ported to C++ for mbed. The Mbed will require 4.7K pull-up resistors on the i2c SCL and SDA lines to communicate with the board. The IRQ line does not. The example code has the SDA on P28, SCL on P27 and IRQ on P26

Dependencies:   mbed

Committer:
abuckton
Date:
Mon Feb 28 12:20:18 2011 +0000
Revision:
1:d1837531c318
Parent:
0:2e5b82508aea
Cleaned up copyright, attributed original source

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abuckton 1:d1837531c318 1 /*
abuckton 1:d1837531c318 2 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au)
abuckton 1:d1837531c318 3
abuckton 1:d1837531c318 4 Permission is hereby granted, free of charge, to any person obtaining a copy
abuckton 1:d1837531c318 5 of this software and associated documentation files (the "Software"), to deal
abuckton 1:d1837531c318 6 in the Software without restriction, including without limitation the rights
abuckton 1:d1837531c318 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
abuckton 1:d1837531c318 8 copies of the Software, and to permit persons to whom the Software is
abuckton 1:d1837531c318 9 furnished to do so, subject to the following conditions:
abuckton 1:d1837531c318 10
abuckton 1:d1837531c318 11 The above copyright notice and this permission notice shall be included in
abuckton 1:d1837531c318 12 all copies or substantial portions of the Software.
abuckton 1:d1837531c318 13
abuckton 1:d1837531c318 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
abuckton 1:d1837531c318 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
abuckton 1:d1837531c318 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
abuckton 1:d1837531c318 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
abuckton 1:d1837531c318 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
abuckton 1:d1837531c318 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
abuckton 1:d1837531c318 20 THE SOFTWARE.
abuckton 1:d1837531c318 21 */
abuckton 1:d1837531c318 22
abuckton 0:2e5b82508aea 23 #include <mbed.h>
abuckton 0:2e5b82508aea 24 #include <string>
abuckton 0:2e5b82508aea 25 #include <list>
abuckton 0:2e5b82508aea 26
abuckton 0:2e5b82508aea 27 #include <mpr121.h>
abuckton 0:2e5b82508aea 28
abuckton 0:2e5b82508aea 29 // Create the interrupt receiver object on pin 26
abuckton 0:2e5b82508aea 30 InterruptIn interrupt(p26);
abuckton 0:2e5b82508aea 31
abuckton 0:2e5b82508aea 32 // Setup the Serial to the PC for debugging
abuckton 0:2e5b82508aea 33 Serial pc(USBTX, USBRX);
abuckton 0:2e5b82508aea 34
abuckton 0:2e5b82508aea 35 // Setup the i2c bus on pins 28 and 27
abuckton 0:2e5b82508aea 36 I2C i2c(p28, p27);
abuckton 0:2e5b82508aea 37
abuckton 0:2e5b82508aea 38 // Setup the Mpr121:
abuckton 0:2e5b82508aea 39 // constructor(i2c object, i2c address of the mpr121)
abuckton 0:2e5b82508aea 40 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
abuckton 0:2e5b82508aea 41
abuckton 0:2e5b82508aea 42 void fallInterrupt(){
abuckton 0:2e5b82508aea 43 int value=mpr121.read(0x00);
abuckton 0:2e5b82508aea 44 value +=mpr121.read(0x01)<<8;
abuckton 0:2e5b82508aea 45 pc.printf("MPR value: %x \r\n", value);
abuckton 0:2e5b82508aea 46 }
abuckton 0:2e5b82508aea 47
abuckton 0:2e5b82508aea 48 int main() {
abuckton 0:2e5b82508aea 49
abuckton 0:2e5b82508aea 50 pc.printf("\nHello from the mbed & mpr121\n\r");
abuckton 0:2e5b82508aea 51
abuckton 0:2e5b82508aea 52 unsigned char dataArray[2];
abuckton 0:2e5b82508aea 53 int key;
abuckton 0:2e5b82508aea 54 int count = 0;
abuckton 0:2e5b82508aea 55
abuckton 0:2e5b82508aea 56 pc.printf("Test 1: read a value: \r\n");
abuckton 0:2e5b82508aea 57 dataArray[0] = mpr121.read(AFE_CFG);
abuckton 0:2e5b82508aea 58 pc.printf("Read value=%x\r\n\n",dataArray[0]);
abuckton 0:2e5b82508aea 59
abuckton 0:2e5b82508aea 60 pc.printf("Test 2: read a value: \r\n");
abuckton 0:2e5b82508aea 61 dataArray[0] = mpr121.read(0x5d);
abuckton 0:2e5b82508aea 62 pc.printf("Read value=%x\r\n\n",dataArray[0]);
abuckton 0:2e5b82508aea 63
abuckton 0:2e5b82508aea 64 pc.printf("Test 3: write & read a value: \r\n");
abuckton 0:2e5b82508aea 65 mpr121.read(ELE0_T);
abuckton 0:2e5b82508aea 66 mpr121.write(ELE0_T,0x22);
abuckton 0:2e5b82508aea 67 dataArray[0] = mpr121.read(ELE0_T);
abuckton 0:2e5b82508aea 68 pc.printf("Read value=%x\r\n\n",dataArray[0]);
abuckton 0:2e5b82508aea 69
abuckton 0:2e5b82508aea 70 pc.printf("Test 4: Write many values: \r\n");
abuckton 0:2e5b82508aea 71 unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41};
abuckton 0:2e5b82508aea 72 mpr121.writeMany(0x42,data,7);
abuckton 0:2e5b82508aea 73
abuckton 0:2e5b82508aea 74 // Now read them back ..
abuckton 0:2e5b82508aea 75 key = 0x42;
abuckton 0:2e5b82508aea 76 count = 0;
abuckton 0:2e5b82508aea 77 while(count < 7){
abuckton 0:2e5b82508aea 78 char result = mpr121.read(key);
abuckton 0:2e5b82508aea 79 key++;
abuckton 0:2e5b82508aea 80 count++;
abuckton 0:2e5b82508aea 81 pc.printf("Read value: '%x'=%x\n\r",key,result);
abuckton 0:2e5b82508aea 82 }
abuckton 0:2e5b82508aea 83
abuckton 0:2e5b82508aea 84 pc.printf("Test 5: Read Electrodes:\r\n");
abuckton 0:2e5b82508aea 85 key = ELE0_T;
abuckton 0:2e5b82508aea 86 count = 0;
abuckton 0:2e5b82508aea 87 while(count < 24){
abuckton 0:2e5b82508aea 88 char result = mpr121.read(key);
abuckton 0:2e5b82508aea 89 pc.printf("Read key:%x value:%x\n\r",key,result);
abuckton 0:2e5b82508aea 90 key++;
abuckton 0:2e5b82508aea 91 count++; }
abuckton 0:2e5b82508aea 92 pc.printf("--------- \r\n\n");
abuckton 0:2e5b82508aea 93
abuckton 1:d1837531c318 94 // mpr121.setProximityMode(true);
abuckton 0:2e5b82508aea 95
abuckton 0:2e5b82508aea 96 pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG));
abuckton 0:2e5b82508aea 97
abuckton 0:2e5b82508aea 98 interrupt.fall(&fallInterrupt);
abuckton 0:2e5b82508aea 99 interrupt.mode(PullUp);
abuckton 0:2e5b82508aea 100
abuckton 0:2e5b82508aea 101 while(1){
abuckton 0:2e5b82508aea 102 wait(5);
abuckton 0:2e5b82508aea 103 pc.printf(".");
abuckton 0:2e5b82508aea 104 }
abuckton 0:2e5b82508aea 105 }
abuckton 0:2e5b82508aea 106
abuckton 0:2e5b82508aea 107
abuckton 0:2e5b82508aea 108