CodeShare
Dependencies: mbed
main.cpp@0:42cceb842d76, 2016-09-25 (annotated)
- Committer:
- jeremycai3721
- Date:
- Sun Sep 25 20:20:04 2016 +0000
- Revision:
- 0:42cceb842d76
CodeShare
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jeremycai3721 | 0:42cceb842d76 | 1 | #include <mbed.h> |
jeremycai3721 | 0:42cceb842d76 | 2 | #include <mpr121.h> |
jeremycai3721 | 0:42cceb842d76 | 3 | DigitalOut led1(LED1); |
jeremycai3721 | 0:42cceb842d76 | 4 | DigitalOut led2(LED2); |
jeremycai3721 | 0:42cceb842d76 | 5 | DigitalOut led3(LED3); |
jeremycai3721 | 0:42cceb842d76 | 6 | DigitalOut led4(LED4); |
jeremycai3721 | 0:42cceb842d76 | 7 | // Create the interrupt receiver object on pin 26 |
jeremycai3721 | 0:42cceb842d76 | 8 | InterruptIn interrupt(p26); |
jeremycai3721 | 0:42cceb842d76 | 9 | // Setup the i2c bus on pins 28 and 27 |
jeremycai3721 | 0:42cceb842d76 | 10 | I2C i2c(p28, p27); |
jeremycai3721 | 0:42cceb842d76 | 11 | // Setup the Mpr121: |
jeremycai3721 | 0:42cceb842d76 | 12 | // constructor(i2c object, i2c address of the mpr121) |
jeremycai3721 | 0:42cceb842d76 | 13 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
jeremycai3721 | 0:42cceb842d76 | 14 | |
jeremycai3721 | 0:42cceb842d76 | 15 | // Key hit/release interrupt routine |
jeremycai3721 | 0:42cceb842d76 | 16 | void fallInterrupt() { |
jeremycai3721 | 0:42cceb842d76 | 17 | int key_code=0; |
jeremycai3721 | 0:42cceb842d76 | 18 | int i=0; |
jeremycai3721 | 0:42cceb842d76 | 19 | int value=mpr121.read(0x00); |
jeremycai3721 | 0:42cceb842d76 | 20 | value +=mpr121.read(0x01)<<8; |
jeremycai3721 | 0:42cceb842d76 | 21 | // LED demo mod |
jeremycai3721 | 0:42cceb842d76 | 22 | i=0; |
jeremycai3721 | 0:42cceb842d76 | 23 | // puts key number out to LEDs for demo |
jeremycai3721 | 0:42cceb842d76 | 24 | for (i=0; i<12; i++) { |
jeremycai3721 | 0:42cceb842d76 | 25 | if (((value>>i)&0x01)==1) key_code=i+1; |
jeremycai3721 | 0:42cceb842d76 | 26 | } |
jeremycai3721 | 0:42cceb842d76 | 27 | led4=key_code & 0x01; |
jeremycai3721 | 0:42cceb842d76 | 28 | led3=(key_code>>1) & 0x01; |
jeremycai3721 | 0:42cceb842d76 | 29 | led2=(key_code>>2) & 0x01; |
jeremycai3721 | 0:42cceb842d76 | 30 | led1=(key_code>>3) & 0x01; |
jeremycai3721 | 0:42cceb842d76 | 31 | } |
jeremycai3721 | 0:42cceb842d76 | 32 | |
jeremycai3721 | 0:42cceb842d76 | 33 | int main() { |
jeremycai3721 | 0:42cceb842d76 | 34 | interrupt.fall(&fallInterrupt); |
jeremycai3721 | 0:42cceb842d76 | 35 | interrupt.mode(PullUp); |
jeremycai3721 | 0:42cceb842d76 | 36 | while (1) {} |
jeremycai3721 | 0:42cceb842d76 | 37 | } |