t

Dependencies:   mbed

Committer:
abuckton
Date:
Mon Feb 28 12:07:17 2011 +0000
Revision:
0:2e5b82508aea
Child:
1:d1837531c318
First cut to get things moving

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abuckton 0:2e5b82508aea 1 #include <mbed.h>
abuckton 0:2e5b82508aea 2 #include <string>
abuckton 0:2e5b82508aea 3 #include <list>
abuckton 0:2e5b82508aea 4
abuckton 0:2e5b82508aea 5 #include <mpr121.h>
abuckton 0:2e5b82508aea 6
abuckton 0:2e5b82508aea 7 DigitalOut led1(LED1);
abuckton 0:2e5b82508aea 8 DigitalOut led2(LED2);
abuckton 0:2e5b82508aea 9 DigitalOut led3(LED3);
abuckton 0:2e5b82508aea 10 DigitalOut led4(LED4);
abuckton 0:2e5b82508aea 11
abuckton 0:2e5b82508aea 12
abuckton 0:2e5b82508aea 13 // Create the interrupt receiver object on pin 26
abuckton 0:2e5b82508aea 14 InterruptIn interrupt(p26);
abuckton 0:2e5b82508aea 15
abuckton 0:2e5b82508aea 16 // Setup the Serial to the PC for debugging
abuckton 0:2e5b82508aea 17 Serial pc(USBTX, USBRX);
abuckton 0:2e5b82508aea 18
abuckton 0:2e5b82508aea 19 // Setup the i2c bus on pins 28 and 27
abuckton 0:2e5b82508aea 20 I2C i2c(p28, p27);
abuckton 0:2e5b82508aea 21
abuckton 0:2e5b82508aea 22 // Setup the Mpr121:
abuckton 0:2e5b82508aea 23 // constructor(i2c object, i2c address of the mpr121)
abuckton 0:2e5b82508aea 24 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
abuckton 0:2e5b82508aea 25
abuckton 0:2e5b82508aea 26 void fallInterrupt(){
abuckton 0:2e5b82508aea 27 int value=mpr121.read(0x00);
abuckton 0:2e5b82508aea 28 value +=mpr121.read(0x01)<<8;
abuckton 0:2e5b82508aea 29 pc.printf("MPR value: %x \r\n", value);
abuckton 0:2e5b82508aea 30 }
abuckton 0:2e5b82508aea 31
abuckton 0:2e5b82508aea 32 int main() {
abuckton 0:2e5b82508aea 33
abuckton 0:2e5b82508aea 34 pc.printf("\nHello from the mbed & mpr121\n\r");
abuckton 0:2e5b82508aea 35
abuckton 0:2e5b82508aea 36 unsigned char dataArray[2];
abuckton 0:2e5b82508aea 37 int key;
abuckton 0:2e5b82508aea 38 int count = 0;
abuckton 0:2e5b82508aea 39
abuckton 0:2e5b82508aea 40 pc.printf("Test 1: read a value: \r\n");
abuckton 0:2e5b82508aea 41 dataArray[0] = mpr121.read(AFE_CFG);
abuckton 0:2e5b82508aea 42 pc.printf("Read value=%x\r\n\n",dataArray[0]);
abuckton 0:2e5b82508aea 43
abuckton 0:2e5b82508aea 44 pc.printf("Test 2: read a value: \r\n");
abuckton 0:2e5b82508aea 45 dataArray[0] = mpr121.read(0x5d);
abuckton 0:2e5b82508aea 46 pc.printf("Read value=%x\r\n\n",dataArray[0]);
abuckton 0:2e5b82508aea 47
abuckton 0:2e5b82508aea 48 pc.printf("Test 3: write & read a value: \r\n");
abuckton 0:2e5b82508aea 49 mpr121.read(ELE0_T);
abuckton 0:2e5b82508aea 50 mpr121.write(ELE0_T,0x22);
abuckton 0:2e5b82508aea 51 dataArray[0] = mpr121.read(ELE0_T);
abuckton 0:2e5b82508aea 52 pc.printf("Read value=%x\r\n\n",dataArray[0]);
abuckton 0:2e5b82508aea 53
abuckton 0:2e5b82508aea 54 pc.printf("Test 4: Write many values: \r\n");
abuckton 0:2e5b82508aea 55 unsigned char data[] = {0x1,0x3,0x5,0x9,0x15,0x25,0x41};
abuckton 0:2e5b82508aea 56 mpr121.writeMany(0x42,data,7);
abuckton 0:2e5b82508aea 57
abuckton 0:2e5b82508aea 58 // Now read them back ..
abuckton 0:2e5b82508aea 59 key = 0x42;
abuckton 0:2e5b82508aea 60 count = 0;
abuckton 0:2e5b82508aea 61 while(count < 7){
abuckton 0:2e5b82508aea 62 char result = mpr121.read(key);
abuckton 0:2e5b82508aea 63 key++;
abuckton 0:2e5b82508aea 64 count++;
abuckton 0:2e5b82508aea 65 pc.printf("Read value: '%x'=%x\n\r",key,result);
abuckton 0:2e5b82508aea 66 }
abuckton 0:2e5b82508aea 67
abuckton 0:2e5b82508aea 68 pc.printf("Test 5: Read Electrodes:\r\n");
abuckton 0:2e5b82508aea 69 key = ELE0_T;
abuckton 0:2e5b82508aea 70 count = 0;
abuckton 0:2e5b82508aea 71 while(count < 24){
abuckton 0:2e5b82508aea 72 char result = mpr121.read(key);
abuckton 0:2e5b82508aea 73 pc.printf("Read key:%x value:%x\n\r",key,result);
abuckton 0:2e5b82508aea 74 key++;
abuckton 0:2e5b82508aea 75 count++; }
abuckton 0:2e5b82508aea 76 pc.printf("--------- \r\n\n");
abuckton 0:2e5b82508aea 77
abuckton 0:2e5b82508aea 78 mpr121.setProximityMode(true);
abuckton 0:2e5b82508aea 79
abuckton 0:2e5b82508aea 80 pc.printf("ELE_CFG=%x", mpr121.read(ELE_CFG));
abuckton 0:2e5b82508aea 81
abuckton 0:2e5b82508aea 82 interrupt.fall(&fallInterrupt);
abuckton 0:2e5b82508aea 83 interrupt.mode(PullUp);
abuckton 0:2e5b82508aea 84
abuckton 0:2e5b82508aea 85 while(1){
abuckton 0:2e5b82508aea 86 wait(5);
abuckton 0:2e5b82508aea 87 pc.printf(".");
abuckton 0:2e5b82508aea 88 }
abuckton 0:2e5b82508aea 89
abuckton 0:2e5b82508aea 90 }
abuckton 0:2e5b82508aea 91
abuckton 0:2e5b82508aea 92
abuckton 0:2e5b82508aea 93