Richard Nash / Mbed OS i2c_pca9685_test1

Fork of ic2_test_bus by Martin Simpson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include"PCA9685.h"
00003  
00004 #define I2C_ADDR_PCA9685   (0xEE) // Device You Wish to Use Address - using i2c Address
00005 
00006 #define WRITE              (0x00) // i2c Write bit
00007 #define READ               (0x01) // i2c Read bit
00008 
00009 // ----- NOTES -----
00010 // 4.7k resistor pull up to 3.3v on SDA and SDL lines
00011 // SCL is green on our boards
00012 // 
00013 
00014 Serial pc(SERIAL_TX, SERIAL_RX); //Serial Class Pin Assignments see Serial.h
00015 
00016 
00017 PCA9685 pwm( I2C_SDA, I2C_SCL );
00018 
00019 
00020 bool I2CScan( I2C *i2c, int Device_Adress )
00021 {
00022     char ucdata_write[2];
00023     
00024     ucdata_write[0]=0;
00025     ucdata_write[1]=0;
00026     
00027     // try writing to the device
00028     if (!i2c->write((Device_Adress|WRITE), ucdata_write, 1, 0))// Check for ACK from i2c Device NB I am 'ORing' the Write Bit
00029         {
00030         // all good
00031         return true;
00032         }
00033     // no device found
00034     return false;
00035 }
00036 
00037 
00038 int I2CScan( I2C *i2c )
00039 {
00040     short count=0;
00041     
00042     // scan all channels
00043     pc.printf("====================================================\n\r");
00044     pc.printf("I2CScan (ALL Channels)\n\r");
00045     for (int Device_Adress=0; Device_Adress<=0xFE; Device_Adress+=2)//Stepping in 2 Because Read/Write use LSB
00046         {
00047         if( I2CScan(i2c, Device_Adress) )
00048             {
00049             pc.printf("I2CScan: %#4x\n\r", Device_Adress );
00050             count++;
00051             }
00052         }
00053     pc.printf("%d Devices detected!\n\r",count);
00054     pc.printf("====================================================\n\r\n\r");
00055     
00056     return count;
00057 }
00058 
00059 
00060 
00061 void PCA9685_initDriver() {
00062     pwm.begin();
00063     pwm.setPWMFreq(1600);     // max frequency
00064     pwm.frequencyI2C(400000); //400kHz fast I2C comunication
00065 }
00066 
00067 
00068 int main()
00069 {
00070     I2C i2c(I2C_SDA, I2C_SCL);       //I2C Class Pin Assignments see I2C.h
00071     
00072     unsigned int uifrequency=400000; //400KHz for i2c Max
00073     unsigned int uibaudrate=115200;
00074     
00075     // debug
00076     pc.baud(uibaudrate);
00077     pc.printf("\n\r ---- I2C Scanner ---- \n\r");
00078     
00079     // setup i2c
00080     i2c.frequency(uifrequency);
00081     pc.printf("I2C: %uKHz\n\r", uifrequency/1000);
00082     
00083     // scan
00084     I2CScan(&i2c);
00085     
00086     // Init the PCA9685 on the default addr
00087     PCA9685_initDriver();
00088     
00089     // loop led's on channel 0 and 1
00090     while(1)
00091         {
00092         for (int i = 0; i < 4095; i=i+24)
00093             {
00094             pwm.setPWM_ALL(4095-i,0);
00095             wait(0.0005);
00096             }
00097         for (int i = 4094; i > 0; i=i-24)
00098             {
00099             pwm.setPWM_ALL(4095-i,0);
00100             wait(0.0005);
00101             }
00102 /*
00103         pc.printf("1.");
00104         for (int i = 0; i < 4095; i=i+8)
00105             {
00106             pwm.setPWM(1,4095-i,0);
00107             wait(0.0005);
00108             
00109             pwm.setPWM(2,i,0);
00110             wait(0.0005);            
00111             }
00112         
00113         pc.printf("2.");
00114         for (int i = 4094; i > 0; i=i-8)
00115             {
00116             pwm.setPWM(1,4095-i,0);
00117             wait(0.0005);
00118             
00119             pwm.setPWM(2,i,0);
00120             wait(0.0005); 
00121             }
00122         pc.printf("\n\r");
00123 */
00124         }
00125 }
00126