x

Dependents:   20180621_FT813

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCA9745B.cpp Source File

PCA9745B.cpp

00001 #include "mbed.h"
00002 #include "stdint.h"
00003 #include "PCA9745B.h"
00004 
00005 PCA9745B::PCA9745B(uint8_t number, PinName SCLK, PinName MOSI) : n(number), spi(MOSI, NC, SCLK)
00006 {
00007     numdrivers = n;   
00008     
00009     // Configure SPI to 8 bits and SPI_SPEED  
00010     spi.format(8, 0);
00011     spi.frequency(SPI_SPEED);
00012     
00013     BCr = BCg = BCb = 0x7F;
00014     pwmbuffer = (uint16_t *)calloc(2, 12*n);
00015 }
00016 
00017 void PCA9745B::write(void) {
00018   uint32_t command;
00019   // Magic word for write
00020   command = 0x25;
00021   command <<= 5;
00022   //OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16
00023   command |= 0x16;
00024   command <<= 7;
00025   command |= BCr;
00026   command <<= 7;
00027   command |= BCg;
00028   command <<= 7;
00029   command |= BCb;
00030   for (uint8_t n=0; n<numdrivers; n++) {
00031     spi.write(command >> 24);
00032     spi.write(command >> 16);
00033     spi.write(command >> 8);
00034     spi.write(command);
00035     // 12 channels per PCA9745B
00036     for (int8_t c=11; c >= 0 ; c--) {
00037       // 16 bits per channel, send MSB first
00038       spi.write(pwmbuffer[n*12+c]>>8);
00039       spi.write(pwmbuffer[n*12+c]);
00040     }
00041   }
00042 }
00043 
00044 void PCA9745B::setPWM(uint8_t chan, uint16_t pwm) {
00045   if (chan > 12*numdrivers) return;
00046   pwmbuffer[chan] = pwm;  
00047 }
00048 
00049 void PCA9745B::setLED(uint8_t lednum, uint16_t r, uint16_t g, uint16_t b) {
00050   setPWM(lednum*3, r);
00051   setPWM(lednum*3+1, g);
00052   setPWM(lednum*3+2, b);
00053 }