basic pca9685

Fork of ic2_test_bus by Martin Simpson

Revision:
1:dbe78e80d722
Parent:
0:2db41a0c2f17
diff -r 2db41a0c2f17 -r dbe78e80d722 main.cpp
--- a/main.cpp	Tue Jan 13 15:48:33 2015 +0000
+++ b/main.cpp	Mon Oct 29 14:56:07 2018 +0000
@@ -1,60 +1,126 @@
 #include "mbed.h"
-#include "TextLCD.h"
+#include"PCA9685.h"
  
-#define Device_Name_ADDR   (0xEE) // Device You Wish to Use Address - using i2c Address
+#define I2C_ADDR_PCA9685   (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 */
+// ----- NOTES -----
+// 4.7k resistor pull up to 3.3v on SDA and SDL lines
+// SCL is green on our boards
+// 
 
-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
+
+PCA9685 pwm( I2C_SDA, I2C_SCL );
+
+
+bool I2CScan( I2C *i2c, int Device_Adress )
+{
+    char ucdata_write[2];
+    
+    ucdata_write[0]=0;
+    ucdata_write[1]=0;
+    
+    // try writing to the device
+    if (!i2c->write((Device_Adress|WRITE), ucdata_write, 1, 0))// Check for ACK from i2c Device NB I am 'ORing' the Write Bit
+        {
+        // all good
+        return true;
+        }
+    // no device found
+    return false;
+}
+
 
-char ucdata_write[2];
-short count=0;
+int I2CScan( I2C *i2c )
+{
+    short count=0;
+    
+    // scan all channels
+    pc.printf("====================================================\n\r");
+    pc.printf("I2CScan (ALL Channels)\n\r");
+    for (int Device_Adress=0; Device_Adress<=0xFE; Device_Adress+=2)//Stepping in 2 Because Read/Write use LSB
+        {
+        if( I2CScan(i2c, Device_Adress) )
+            {
+            pc.printf("I2CScan: %#4x\n\r", Device_Adress );
+            count++;
+            }
+        }
+    pc.printf("%d Devices detected!\n\r",count);
+    pc.printf("====================================================\n\r\n\r");
+    
+    return count;
+}
+
+
+
+void PCA9685_initDriver() {
+    pwm.begin();
+    pwm.setPWMFreq(1600);     // max frequency
+    pwm.frequencyI2C(400000); //400kHz fast I2C comunication
+}
+
 
 int main()
 {
-    unsigned int uibaudrate=115200;
-    pc.baud(uibaudrate);
+    I2C i2c(I2C_SDA, I2C_SCL);       //I2C Class Pin Assignments see I2C.h
     
     unsigned int uifrequency=400000; //400KHz for i2c Max
-    i2c.frequency (uifrequency);
+    unsigned int uibaudrate=115200;
     
-    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");
+    // debug
+    pc.baud(uibaudrate);
+    pc.printf("\n\r ---- I2C Scanner ---- \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
+    // setup i2c
+    i2c.frequency(uifrequency);
+    pc.printf("I2C: %uKHz\n\r", uifrequency/1000);
+    
+    // scan
+    I2CScan(&i2c);
+    
+    // Init the PCA9685 on the default addr
+    PCA9685_initDriver();
+    
+    // loop led's on channel 0 and 1
+    while(1)
         {
-            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);
+        for (int i = 0; i < 4095; i=i+24)
+            {
+            pwm.setPWM_ALL(4095-i,0);
+            wait(0.0005);
+            }
+        for (int i = 4094; i > 0; i=i-24)
+            {
+            pwm.setPWM_ALL(4095-i,0);
+            wait(0.0005);
+            }
+/*
+        pc.printf("1.");
+        for (int i = 0; i < 4095; i=i+8)
+            {
+            pwm.setPWM(1,4095-i,0);
+            wait(0.0005);
+            
+            pwm.setPWM(2,i,0);
+            wait(0.0005);            
+            }
+        
+        pc.printf("2.");
+        for (int i = 4094; i > 0; i=i-8)
+            {
+            pwm.setPWM(1,4095-i,0);
+            wait(0.0005);
+            
+            pwm.setPWM(2,i,0);
+            wait(0.0005); 
+            }
+        pc.printf("\n\r");
+*/
         }
-        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);
 }
+