x

Dependents:   20180621_FT813

Files at this revision

API Documentation at this revision

Comitter:
JackB
Date:
Mon Jul 23 12:25:17 2018 +0000
Commit message:
PCA9745

Changed in this revision

PCA9745B.cpp Show annotated file Show diff for this revision Revisions of this file
PCA9745B.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0f53e5add603 PCA9745B.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9745B.cpp	Mon Jul 23 12:25:17 2018 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "stdint.h"
+#include "PCA9745B.h"
+
+PCA9745B::PCA9745B(uint8_t number, PinName SCLK, PinName MOSI) : n(number), spi(MOSI, NC, SCLK)
+{
+    numdrivers = n;   
+    
+    // Configure SPI to 8 bits and SPI_SPEED  
+    spi.format(8, 0);
+    spi.frequency(SPI_SPEED);
+    
+    BCr = BCg = BCb = 0x7F;
+    pwmbuffer = (uint16_t *)calloc(2, 12*n);
+}
+
+void PCA9745B::write(void) {
+  uint32_t command;
+  // Magic word for write
+  command = 0x25;
+  command <<= 5;
+  //OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16
+  command |= 0x16;
+  command <<= 7;
+  command |= BCr;
+  command <<= 7;
+  command |= BCg;
+  command <<= 7;
+  command |= BCb;
+  for (uint8_t n=0; n<numdrivers; n++) {
+    spi.write(command >> 24);
+    spi.write(command >> 16);
+    spi.write(command >> 8);
+    spi.write(command);
+    // 12 channels per PCA9745B
+    for (int8_t c=11; c >= 0 ; c--) {
+      // 16 bits per channel, send MSB first
+      spi.write(pwmbuffer[n*12+c]>>8);
+      spi.write(pwmbuffer[n*12+c]);
+    }
+  }
+}
+
+void PCA9745B::setPWM(uint8_t chan, uint16_t pwm) {
+  if (chan > 12*numdrivers) return;
+  pwmbuffer[chan] = pwm;  
+}
+
+void PCA9745B::setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t b) {
+  setPWM(lednum*3, r);
+  setPWM(lednum*3+1, g);
+  setPWM(lednum*3+2, b);
+}
diff -r 000000000000 -r 0f53e5add603 PCA9745B.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCA9745B.h	Mon Jul 23 12:25:17 2018 +0000
@@ -0,0 +1,69 @@
+#ifndef PCA9745B_H
+#define PCA9745B_H
+
+/**
+  * SPI speed used by the mbed to communicate with the PCA9745B
+  * The PCA9745B supports up to 10Mhz.
+  */
+#define SPI_SPEED 1000000
+
+/**
+  *  Using the TLC5 class to control an LED:
+  *  @code
+  *  #include "mbed.h"
+  *  #include "PCA9745B.h"
+  *  
+  *  // Create the TLC5711 instance
+  *  PCA9745B tlc(1, p7, p5);
+  *  
+  *  int main()
+  *  {   
+  *      
+  *      while(1)
+  *      {
+  *         // Led1 -> R0
+  *         tlc.setLED(0, 65535, 0, 0);
+  *         tlc.write( );
+  *         tlc.setLED(1, 0, 0, 0);
+  *         tlc.write( );
+  *         tlc.setLED(2, 0, 0, 0);
+  *         tlc.write( );
+  *         tlc.setLED(3, 0, 0, 0);
+  *         tlc.write( );
+  *         wait( 1 );
+  *  
+  *      }
+  *  }
+  *  @endcode
+  */
+
+ class PCA9745B
+{
+
+public:/**
+      *  Set up the PCA9745B
+      *  @param SCLK - The SCK pin of the SPI bus
+      *  @param MOSI - The MOSI pin of the SPI bus
+      *  @param number - The number of PCA9745Bs 
+      */
+  
+    PCA9745B(uint8_t number, PinName SCLK, PinName MOSI);
+
+  void setPWM(uint8_t chan, uint16_t pwm);
+  void setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t b);
+  void write(void);    
+        
+  uint8_t n;
+  
+  private:
+  
+  SPI spi;
+  
+  uint16_t *pwmbuffer;
+
+  uint8_t BCr, BCg, BCb;
+  int8_t numdrivers;
+
+};
+ 
+#endif