Simple Test Program to check ACK from i2c Devices connected to an MBED board and then to report their addresses
main.cpp@1:df2444f34fec, 2021-02-15 (annotated)
- Committer:
- martinsimpson
- Date:
- Mon Feb 15 17:20:16 2021 +0000
- Revision:
- 1:df2444f34fec
- Parent:
- 0:2db41a0c2f17
Removed lcd and Serial class references
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martinsimpson | 0:2db41a0c2f17 | 1 | #include "mbed.h" |
martinsimpson | 0:2db41a0c2f17 | 2 | |
martinsimpson | 0:2db41a0c2f17 | 3 | #define Device_Name_ADDR (0xEE) // Device You Wish to Use Address - using i2c Address |
martinsimpson | 0:2db41a0c2f17 | 4 | #define WRITE (0x00) // i2c Write bit |
martinsimpson | 0:2db41a0c2f17 | 5 | #define READ (0x01) // i2c Read bit |
martinsimpson | 0:2db41a0c2f17 | 6 | |
martinsimpson | 0:2db41a0c2f17 | 7 | /* This is a program to demonstrate the ease of use of the i2c bus |
martinsimpson | 0:2db41a0c2f17 | 8 | and Detecting any Devices connected and returning their addresses |
martinsimpson | 0:2db41a0c2f17 | 9 | Do not forget that Pull Up resistors are required on the SDA and SCL lines |
martinsimpson | 0:2db41a0c2f17 | 10 | (Typically 2K ohms for 400KHz bus speeds) NB these could be provided on the PCB Modules. |
martinsimpson | 0:2db41a0c2f17 | 11 | These are required since devices connected with the i2c will have 'open drain' (or open collector) |
martinsimpson | 0:2db41a0c2f17 | 12 | circuitry to allow wire 'ORing' of their respective connections. Used with a stm32-Nucleo-F401RE. |
martinsimpson | 0:2db41a0c2f17 | 13 | Used both Serial Port to USB and LCD Module to display results can use either or both just comment out if you wish. |
martinsimpson | 0:2db41a0c2f17 | 14 | Martin Simpson January 2015 */ |
martinsimpson | 0:2db41a0c2f17 | 15 | |
martinsimpson | 0:2db41a0c2f17 | 16 | I2C i2c(I2C_SDA, I2C_SCL); //I2C Class Pin Assignments see I2C.h |
martinsimpson | 0:2db41a0c2f17 | 17 | |
martinsimpson | 0:2db41a0c2f17 | 18 | int main() |
martinsimpson | 0:2db41a0c2f17 | 19 | { |
martinsimpson | 1:df2444f34fec | 20 | |
martinsimpson | 1:df2444f34fec | 21 | char ucdata_write[2]; |
martinsimpson | 1:df2444f34fec | 22 | short count=0; |
martinsimpson | 0:2db41a0c2f17 | 23 | |
martinsimpson | 0:2db41a0c2f17 | 24 | unsigned int uifrequency=400000; //400KHz for i2c Max |
martinsimpson | 0:2db41a0c2f17 | 25 | i2c.frequency (uifrequency); |
martinsimpson | 0:2db41a0c2f17 | 26 | |
martinsimpson | 1:df2444f34fec | 27 | printf("\n\rHello World "); |
martinsimpson | 1:df2444f34fec | 28 | printf("at %uKHz i2c Frequency\n\r",uifrequency/1000); |
martinsimpson | 1:df2444f34fec | 29 | printf("Using mbed.org Martin\n\r"); |
martinsimpson | 0:2db41a0c2f17 | 30 | |
martinsimpson | 0:2db41a0c2f17 | 31 | ucdata_write[0]=0;ucdata_write[1]=0; |
martinsimpson | 0:2db41a0c2f17 | 32 | |
martinsimpson | 0:2db41a0c2f17 | 33 | for (int Device_Adress=0;Device_Adress<=0xFE;Device_Adress+=2)//Stepping in 2 Because Read/Write use LSB |
martinsimpson | 0:2db41a0c2f17 | 34 | { |
martinsimpson | 0:2db41a0c2f17 | 35 | if (!i2c.write((Device_Adress|WRITE), ucdata_write, 1, 0))// Check for ACK from i2c Device NB I am 'ORing' the Write Bit |
martinsimpson | 0:2db41a0c2f17 | 36 | { |
martinsimpson | 1:df2444f34fec | 37 | printf("ACK from the Device at Address %#4x\n\r",Device_Adress); |
martinsimpson | 1:df2444f34fec | 38 | count++; |
martinsimpson | 0:2db41a0c2f17 | 39 | wait(2); |
martinsimpson | 0:2db41a0c2f17 | 40 | } |
martinsimpson | 0:2db41a0c2f17 | 41 | else |
martinsimpson | 0:2db41a0c2f17 | 42 | { |
martinsimpson | 0:2db41a0c2f17 | 43 | //Left the following in for development/Future coding |
martinsimpson | 0:2db41a0c2f17 | 44 | //pc.printf("\n\rCannot get an ACK from the Device check connections!\n\r"); |
martinsimpson | 0:2db41a0c2f17 | 45 | //lcd.printf("No ACK from\nDevice!"); |
martinsimpson | 0:2db41a0c2f17 | 46 | } |
martinsimpson | 0:2db41a0c2f17 | 47 | } |
martinsimpson | 1:df2444f34fec | 48 | printf("\n\r %d Devices have been detected!\n\r",count); |
martinsimpson | 0:2db41a0c2f17 | 49 | } |