An I2C Scanner, to test that your I2C can be detected. Works with the NRF51 Development Kit.
Dependencies: mbed-src-nrf51822
main.cpp@0:839b7264c387, 2016-06-09 (annotated)
- Committer:
- Cannonball2134
- Date:
- Thu Jun 09 11:06:51 2016 +0000
- Revision:
- 0:839b7264c387
- Child:
- 1:117c1bb3ce43
Working Version of I2C Scanner 09-06-16.; The mbed-src-nrf51822 library and wire.cpp/.h is what fixed previous issues
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cannonball2134 | 0:839b7264c387 | 1 | #include "mbed.h" |
Cannonball2134 | 0:839b7264c387 | 2 | #include "wire.h" |
Cannonball2134 | 0:839b7264c387 | 3 | |
Cannonball2134 | 0:839b7264c387 | 4 | #define SCL 7 |
Cannonball2134 | 0:839b7264c387 | 5 | #define SDA 30 |
Cannonball2134 | 0:839b7264c387 | 6 | |
Cannonball2134 | 0:839b7264c387 | 7 | TwoWire Wire = TwoWire(NRF_TWI0); |
Cannonball2134 | 0:839b7264c387 | 8 | |
Cannonball2134 | 0:839b7264c387 | 9 | int main(void) |
Cannonball2134 | 0:839b7264c387 | 10 | { |
Cannonball2134 | 0:839b7264c387 | 11 | Wire.begin(SCL, SDA, TWI_FREQUENCY_100K); |
Cannonball2134 | 0:839b7264c387 | 12 | printf("IIC Demo Start \r\n"); |
Cannonball2134 | 0:839b7264c387 | 13 | |
Cannonball2134 | 0:839b7264c387 | 14 | while(1) |
Cannonball2134 | 0:839b7264c387 | 15 | { |
Cannonball2134 | 0:839b7264c387 | 16 | int nDevices = 0; |
Cannonball2134 | 0:839b7264c387 | 17 | for(int address = 1; address < 127; address++ ) |
Cannonball2134 | 0:839b7264c387 | 18 | { |
Cannonball2134 | 0:839b7264c387 | 19 | int error = -1; |
Cannonball2134 | 0:839b7264c387 | 20 | Wire.beginTransmission(address << 1); |
Cannonball2134 | 0:839b7264c387 | 21 | Wire.write(0x00); |
Cannonball2134 | 0:839b7264c387 | 22 | error = Wire.endTransmission(); |
Cannonball2134 | 0:839b7264c387 | 23 | |
Cannonball2134 | 0:839b7264c387 | 24 | if (error == 0) |
Cannonball2134 | 0:839b7264c387 | 25 | { |
Cannonball2134 | 0:839b7264c387 | 26 | printf("I2C device found at address 0x%X %d \n\r", address, error); //Returns 7-bit address |
Cannonball2134 | 0:839b7264c387 | 27 | nDevices++; |
Cannonball2134 | 0:839b7264c387 | 28 | } |
Cannonball2134 | 0:839b7264c387 | 29 | } |
Cannonball2134 | 0:839b7264c387 | 30 | if (nDevices == 0) |
Cannonball2134 | 0:839b7264c387 | 31 | { |
Cannonball2134 | 0:839b7264c387 | 32 | printf("No I2C devices found\n\r"); |
Cannonball2134 | 0:839b7264c387 | 33 | } |
Cannonball2134 | 0:839b7264c387 | 34 | else{} |
Cannonball2134 | 0:839b7264c387 | 35 | printf("\ndone\n\r"); |
Cannonball2134 | 0:839b7264c387 | 36 | |
Cannonball2134 | 0:839b7264c387 | 37 | wait(2); // wait 2 seconds for next scan |
Cannonball2134 | 0:839b7264c387 | 38 | |
Cannonball2134 | 0:839b7264c387 | 39 | } |
Cannonball2134 | 0:839b7264c387 | 40 | } |
Cannonball2134 | 0:839b7264c387 | 41 |