Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of MPR121_Demo by
main.cpp
00001 /* 00002 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #include <mbed.h> 00024 #include <string> 00025 #include <list> 00026 00027 #include <mpr121.h> 00028 00029 DigitalOut led1(LED1); 00030 DigitalOut led2(LED2); 00031 DigitalOut led3(LED3); 00032 DigitalOut led4(LED4); 00033 00034 // Create the interrupt receiver object on pin 26 00035 InterruptIn interrupt(p26); 00036 00037 // Setup the Serial to the PC for debugging 00038 Serial pc(USBTX, USBRX); 00039 00040 // Setup the i2c bus on pins 28 and 27 00041 I2C i2c(P0_29, P0_28); 00042 00043 // Setup the Mpr121: 00044 // constructor(i2c object, i2c address of the mpr121) 00045 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); 00046 00047 void fallInterrupt() { 00048 int key_code=0; 00049 int i=0; 00050 int value=mpr121.read(0x00); 00051 value +=mpr121.read(0x01)<<8; 00052 // LED demo mod by J. Hamble 00053 i=0; 00054 // puts key number out to LEDs for demo 00055 for (i=0; i<12; i++) { 00056 if (((value>>i)&0x01)==1) key_code=i+1; 00057 } 00058 led4=key_code & 0x01; 00059 led3=(key_code>>1) & 0x01; 00060 led2=(key_code>>2) & 0x01; 00061 led1=(key_code>>3) & 0x01; 00062 } 00063 int main() { 00064 00065 pc.printf("\nHello from the mbed & mpr121\n\r"); 00066 00067 unsigned char dataArray[2]; 00068 int key; 00069 int count = 0; 00070 00071 pc.printf("Test 1: read a value: \r\n"); 00072 dataArray[0] = mpr121.read(AFE_CFG); 00073 pc.printf("Read value=%x\r\n\n",dataArray[0]); 00074 00075 pc.printf("Test 2: read a value: \r\n"); 00076 dataArray[0] = mpr121.read(0x5d); 00077 pc.printf("Read value=%x\r\n\n",dataArray[0]); 00078 00079 pc.printf("Test 3: write & read a value: \r\n"); 00080 mpr121.read(ELE0_T); 00081 mpr121.write(ELE0_T,0x22); 00082 dataArray[0] = mpr121.read(ELE0_T); 00083 pc.printf("Read value=%x\r\n\n",dataArray[0]); 00084 00085 pc.printf("Test 4: Write many values: \r\n"); 00086 unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41}; 00087 mpr121.writeMany(0x42,data,7); 00088 00089 // Now read them back .. 00090 key = 0x42; 00091 count = 0; 00092 while (count < 7) { 00093 char result = mpr121.read(key); 00094 key++; 00095 count++; 00096 pc.printf("Read value: '%x'=%x\n\r",key,result); 00097 } 00098 00099 pc.printf("Test 5: Read Electrodes:\r\n"); 00100 key = ELE0_T; 00101 count = 0; 00102 while (count < 24) { 00103 char result = mpr121.read(key); 00104 pc.printf("Read key:%x value:%x\n\r",key,result); 00105 key++; 00106 count++; 00107 } 00108 pc.printf("--------- \r\n\n"); 00109 00110 // mpr121.setProximityMode(true); 00111 00112 pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG)); 00113 00114 interrupt.fall(&fallInterrupt); 00115 interrupt.mode(PullUp); 00116 00117 while (1) { 00118 wait(5); 00119 pc.printf("."); 00120 } 00121 } 00122 00123 00124
Generated on Tue Aug 30 2022 14:52:32 by
1.7.2
