a

Dependencies:   mbed

Fork of MPR121_Demo by jim hamblen

Committer:
4180_1
Date:
Wed Mar 16 01:49:13 2011 +0000
Revision:
0:e09703934ff4
Child:
1:40d403361755

        

Who changed what in which revision?

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