An I2C Scanner, to test that your I2C can be detected. Works with the NRF51 Development Kit.

Dependencies:   mbed-src-nrf51822

Committer:
Cannonball2134
Date:
Thu Jun 30 07:43:22 2016 +0000
Revision:
1:117c1bb3ce43
Parent:
0:839b7264c387
Working I2C Scanner on the NRF51-DK

Who changed what in which revision?

UserRevisionLine numberNew 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 1:117c1bb3ce43 7 TwoWire Wire = TwoWire(NRF_TWI1);
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 1:117c1bb3ce43 26 printf("I2C device found at address %d 0x%X %d \n\r",address, 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