Martin Simpson / Mbed 2 deprecated ic2_test_bus

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 #define Device_Name_ADDR   (0xEE) // Device You Wish to Use Address - using i2c Address
00004 #define WRITE              (0x00) // i2c Write bit
00005 #define READ               (0x01) // i2c Read bit
00006 
00007 /* This is a program to demonstrate the ease of use of the i2c bus
00008    and Detecting any Devices connected and returning their addresses
00009    Do not forget that Pull Up resistors are required on the SDA and SCL lines
00010    (Typically 2K ohms for 400KHz bus speeds) NB these could be provided on the PCB Modules.
00011    These are required since devices connected with the i2c will have 'open drain' (or open collector)
00012    circuitry to allow wire 'ORing' of their respective connections. Used with a stm32-Nucleo-F401RE.
00013    Used both Serial Port to USB and LCD Module to display results can use either or both just comment out if you wish.
00014    Martin Simpson January 2015 */
00015 
00016 I2C i2c(I2C_SDA, I2C_SCL);       //I2C Class Pin Assignments see I2C.h
00017  
00018 int main()
00019 {
00020     
00021     char ucdata_write[2];
00022     short count=0;
00023     
00024     unsigned int uifrequency=400000; //400KHz for i2c Max
00025     i2c.frequency (uifrequency);
00026     
00027     printf("\n\rHello World ");
00028     printf("at %uKHz i2c Frequency\n\r",uifrequency/1000);
00029     printf("Using mbed.org Martin\n\r");
00030 
00031     ucdata_write[0]=0;ucdata_write[1]=0;
00032 
00033     for (int Device_Adress=0;Device_Adress<=0xFE;Device_Adress+=2)//Stepping in 2 Because Read/Write use LSB
00034     {
00035     if (!i2c.write((Device_Adress|WRITE), ucdata_write, 1, 0))// Check for ACK from i2c Device NB I am 'ORing' the Write Bit
00036         {
00037             printf("ACK from the Device at Address %#4x\n\r",Device_Adress);
00038             count++;
00039             wait(2);
00040         }
00041         else
00042         {
00043             //Left the following in for development/Future coding
00044             //pc.printf("\n\rCannot get an ACK from the Device check connections!\n\r");
00045             //lcd.printf("No ACK from\nDevice!");
00046         }
00047     }
00048     printf("\n\r %d Devices have been detected!\n\r",count);
00049 }