basic pca9685

Fork of ic2_test_bus by Martin Simpson

main.cpp

Committer:
martinsimpson
Date:
2015-01-13
Revision:
0:2db41a0c2f17
Child:
1:dbe78e80d722

File content as of revision 0:2db41a0c2f17:

#include "mbed.h"
#include "TextLCD.h"
 
#define Device_Name_ADDR   (0xEE) // Device You Wish to Use Address - using i2c Address
#define WRITE              (0x00) // i2c Write bit
#define READ               (0x01) // i2c Read bit

/* This is a program to demonstrate the ease of use of the i2c bus
   and Detecting any Devices connected and returning their addresses
   Do not forget that Pull Up resistors are required on the SDA and SCL lines
   (Typically 2K ohms for 400KHz bus speeds) NB these could be provided on the PCB Modules.
   These are required since devices connected with the i2c will have 'open drain' (or open collector)
   circuitry to allow wire 'ORing' of their respective connections. Used with a stm32-Nucleo-F401RE.
   Used both Serial Port to USB and LCD Module to display results can use either or both just comment out if you wish.
   Martin Simpson January 2015 */

I2C i2c(I2C_SDA, I2C_SCL);       //I2C Class Pin Assignments see I2C.h
 
Serial pc(SERIAL_TX, SERIAL_RX); //Serial Class Pin Assignments see Serial.h

TextLCD lcd(D2,D3,D4,D5,D6,D7);  //Text LCD Class assignments RS, E, D4 to D7 see TextLCD.h

char ucdata_write[2];
short count=0;

int main()
{
    unsigned int uibaudrate=115200;
    pc.baud(uibaudrate);
    
    unsigned int uifrequency=400000; //400KHz for i2c Max
    i2c.frequency (uifrequency);
    
    pc.printf("\n\rHello World ");
    pc.printf("at %u BAUD and %uKHz i2c Frequency\n\r",uibaudrate,uifrequency/1000);
    pc.printf("Using mbed.org Martin\n\r");
    
    lcd.cls();lcd.locate(0,0); //Tidy up LCD Display

    ucdata_write[0]=0;ucdata_write[1]=0;

    for (int Device_Adress=0;Device_Adress<=0xFE;Device_Adress+=2)//Stepping in 2 Because Read/Write use LSB
    {
    if (!i2c.write((Device_Adress|WRITE), ucdata_write, 1, 0))// Check for ACK from i2c Device NB I am 'ORing' the Write Bit
        {
            pc.printf("ACK from the Device at Address %#4x\n\r",Device_Adress);
            lcd.printf("ACK at:%#4x\n",Device_Adress);
            count=count+1;
            wait(2);
        }
        else
        {
            //Left the following in for development/Future coding
            //pc.printf("\n\rCannot get an ACK from the Device check connections!\n\r");
            //lcd.printf("No ACK from\nDevice!");
        }
    }
    pc.printf("\n\r %d Devices have been detected!\n\r",count);
    lcd.printf("%d Dev Found!",count);
}