PCA9955 16 channel current drive(sink) LED driver class library

Dependents:   PCA9955_Hello

Committer:
nxp_ip
Date:
Fri Aug 10 01:39:02 2012 +0000
Revision:
0:2e6f5ac1b29b
PCA9955 sample code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 0:2e6f5ac1b29b 1 /** PCA9955 constant current LED driver control library
nxp_ip 0:2e6f5ac1b29b 2 *
nxp_ip 0:2e6f5ac1b29b 3 * @class PCA9955
nxp_ip 0:2e6f5ac1b29b 4 * @author NXP Semiconductors Japan
nxp_ip 0:2e6f5ac1b29b 5 * @version 0.5(15-Jun-2011) (publish 10-Aug-2012)
nxp_ip 0:2e6f5ac1b29b 6 *
nxp_ip 0:2e6f5ac1b29b 7 * Copyright: 2011 NXP Semiconductors
nxp_ip 0:2e6f5ac1b29b 8 * Released under the MIT License: http://mbed.org/license/mit
nxp_ip 0:2e6f5ac1b29b 9 * This is a library that controls PCA9955 a constant current LED driver
nxp_ip 0:2e6f5ac1b29b 10 * Example:
nxp_ip 0:2e6f5ac1b29b 11 * @code
nxp_ip 0:2e6f5ac1b29b 12 * #include "mbed.h"
nxp_ip 0:2e6f5ac1b29b 13 * #include "PCA9955.h"
nxp_ip 0:2e6f5ac1b29b 14 *
nxp_ip 0:2e6f5ac1b29b 15 * #define MAX_IREF 0x20
nxp_ip 0:2e6f5ac1b29b 16 *
nxp_ip 0:2e6f5ac1b29b 17 * PCA9955 led_driver( p28, p27, 0xC0 ); // making an instance of PCA9955
nxp_ip 0:2e6f5ac1b29b 18 *
nxp_ip 0:2e6f5ac1b29b 19 * main()
nxp_ip 0:2e6f5ac1b29b 20 * {
nxp_ip 0:2e6f5ac1b29b 21 * char v[ 16 ] = { 0xFF };
nxp_ip 0:2e6f5ac1b29b 22 * int sel;
nxp_ip 0:2e6f5ac1b29b 23 *
nxp_ip 0:2e6f5ac1b29b 24 * srand( 0 );
nxp_ip 0:2e6f5ac1b29b 25 *
nxp_ip 0:2e6f5ac1b29b 26 * led_driver = 0xFFFF;
nxp_ip 0:2e6f5ac1b29b 27 * led_driver.set_all_intensity( v ); // PWM
nxp_ip 0:2e6f5ac1b29b 28 * led_driver.set_all_intensity( MAX_IREF, true ); // IREF
nxp_ip 0:2e6f5ac1b29b 29 *
nxp_ip 0:2e6f5ac1b29b 30 * while ( 1 ) {
nxp_ip 0:2e6f5ac1b29b 31 * for ( sel = 0; sel < 16; sel++ ) {
nxp_ip 0:2e6f5ac1b29b 32 * v[ sel ] = (char)((float)(v[ sel ]) * 0.8);
nxp_ip 0:2e6f5ac1b29b 33 * }
nxp_ip 0:2e6f5ac1b29b 34 *
nxp_ip 0:2e6f5ac1b29b 35 * v[ rand() % 16 ] = 0xFF;
nxp_ip 0:2e6f5ac1b29b 36 *
nxp_ip 0:2e6f5ac1b29b 37 * led_driver.set_all_intensity( v ); // PWM
nxp_ip 0:2e6f5ac1b29b 38 * wait( 0.05 );
nxp_ip 0:2e6f5ac1b29b 39 * }
nxp_ip 0:2e6f5ac1b29b 40 * }
nxp_ip 0:2e6f5ac1b29b 41 * @endcode
nxp_ip 0:2e6f5ac1b29b 42 */
nxp_ip 0:2e6f5ac1b29b 43
nxp_ip 0:2e6f5ac1b29b 44 #ifndef MBED_PCA9955
nxp_ip 0:2e6f5ac1b29b 45 #define MBED_PCA9955
nxp_ip 0:2e6f5ac1b29b 46
nxp_ip 0:2e6f5ac1b29b 47 #include "mbed.h"
nxp_ip 0:2e6f5ac1b29b 48
nxp_ip 0:2e6f5ac1b29b 49 // PCA9955 I2C address
nxp_ip 0:2e6f5ac1b29b 50
nxp_ip 0:2e6f5ac1b29b 51 #if 0
nxp_ip 0:2e6f5ac1b29b 52 #define PCA9955_ADDR 0xC0
nxp_ip 0:2e6f5ac1b29b 53
nxp_ip 0:2e6f5ac1b29b 54 #define PCA9955_INIT_PWM 0xFF
nxp_ip 0:2e6f5ac1b29b 55 #define PCA9955_N_OF_PORTS 16
nxp_ip 0:2e6f5ac1b29b 56
nxp_ip 0:2e6f5ac1b29b 57 #define AUTO_INCREMENT 0x80
nxp_ip 0:2e6f5ac1b29b 58 #define REGISTER_START 0x00
nxp_ip 0:2e6f5ac1b29b 59 #define LEDOUT_REGISTER_START 0x02
nxp_ip 0:2e6f5ac1b29b 60 #define PWM_REGISTER_START 0x0A
nxp_ip 0:2e6f5ac1b29b 61 #define IREF_REGISTER_START 0x22
nxp_ip 0:2e6f5ac1b29b 62 #endif
nxp_ip 0:2e6f5ac1b29b 63
nxp_ip 0:2e6f5ac1b29b 64 class PCA9955 {
nxp_ip 0:2e6f5ac1b29b 65 public:
nxp_ip 0:2e6f5ac1b29b 66 PCA9955( PinName I2C_sda, PinName I2C_scl, char I2C_address );
nxp_ip 0:2e6f5ac1b29b 67 void set_single_individual_intensity( char port, char val, int iref = false );
nxp_ip 0:2e6f5ac1b29b 68 void set_all_intensity( char *p, int iref = false );
nxp_ip 0:2e6f5ac1b29b 69 void set_all_intensity( char val, int iref = false );
nxp_ip 0:2e6f5ac1b29b 70 void get_all_intensity( char *p, int iref = false );
nxp_ip 0:2e6f5ac1b29b 71 void operator=( int c );
nxp_ip 0:2e6f5ac1b29b 72 unsigned short fault_test( void );
nxp_ip 0:2e6f5ac1b29b 73
nxp_ip 0:2e6f5ac1b29b 74 private:
nxp_ip 0:2e6f5ac1b29b 75
nxp_ip 0:2e6f5ac1b29b 76 enum {
nxp_ip 0:2e6f5ac1b29b 77 PCA9955_ADDR = 0xC0,
nxp_ip 0:2e6f5ac1b29b 78 };
nxp_ip 0:2e6f5ac1b29b 79
nxp_ip 0:2e6f5ac1b29b 80 enum {
nxp_ip 0:2e6f5ac1b29b 81 PCA9955_INIT_PWM = 0xFF,
nxp_ip 0:2e6f5ac1b29b 82 PCA9955_N_OF_PORTS = 16,
nxp_ip 0:2e6f5ac1b29b 83 };
nxp_ip 0:2e6f5ac1b29b 84
nxp_ip 0:2e6f5ac1b29b 85 typedef enum {
nxp_ip 0:2e6f5ac1b29b 86 MODE1 = 0x00,
nxp_ip 0:2e6f5ac1b29b 87 MODE2,
nxp_ip 0:2e6f5ac1b29b 88 LEDOUT0,
nxp_ip 0:2e6f5ac1b29b 89 LEDOUT1,
nxp_ip 0:2e6f5ac1b29b 90 LEDOUT2,
nxp_ip 0:2e6f5ac1b29b 91 LEDOUT3,
nxp_ip 0:2e6f5ac1b29b 92 GRPPWM = 0x08,
nxp_ip 0:2e6f5ac1b29b 93 GRPFREQ,
nxp_ip 0:2e6f5ac1b29b 94 PWM0 = 0x0A,
nxp_ip 0:2e6f5ac1b29b 95 PWM1,
nxp_ip 0:2e6f5ac1b29b 96 PWM2,
nxp_ip 0:2e6f5ac1b29b 97 PWM3,
nxp_ip 0:2e6f5ac1b29b 98 PWM4,
nxp_ip 0:2e6f5ac1b29b 99 PWM5,
nxp_ip 0:2e6f5ac1b29b 100 PWM6,
nxp_ip 0:2e6f5ac1b29b 101 PWM7,
nxp_ip 0:2e6f5ac1b29b 102 PWM8,
nxp_ip 0:2e6f5ac1b29b 103 PWM9,
nxp_ip 0:2e6f5ac1b29b 104 PWM10,
nxp_ip 0:2e6f5ac1b29b 105 PWM11,
nxp_ip 0:2e6f5ac1b29b 106 PWM12,
nxp_ip 0:2e6f5ac1b29b 107 PWM13,
nxp_ip 0:2e6f5ac1b29b 108 PWM14,
nxp_ip 0:2e6f5ac1b29b 109 PWM15,
nxp_ip 0:2e6f5ac1b29b 110 IREF0 = 0x22,
nxp_ip 0:2e6f5ac1b29b 111 IREF1,
nxp_ip 0:2e6f5ac1b29b 112 IREF2,
nxp_ip 0:2e6f5ac1b29b 113 IREF3,
nxp_ip 0:2e6f5ac1b29b 114 IREF4,
nxp_ip 0:2e6f5ac1b29b 115 IREF5,
nxp_ip 0:2e6f5ac1b29b 116 IREF6,
nxp_ip 0:2e6f5ac1b29b 117 IREF7,
nxp_ip 0:2e6f5ac1b29b 118 IREF8,
nxp_ip 0:2e6f5ac1b29b 119 IREF9,
nxp_ip 0:2e6f5ac1b29b 120 IREF10,
nxp_ip 0:2e6f5ac1b29b 121 IREF11,
nxp_ip 0:2e6f5ac1b29b 122 IREF12,
nxp_ip 0:2e6f5ac1b29b 123 IREF13,
nxp_ip 0:2e6f5ac1b29b 124 IREF14,
nxp_ip 0:2e6f5ac1b29b 125 IREF15,
nxp_ip 0:2e6f5ac1b29b 126 OFFSET = 0x3A,
nxp_ip 0:2e6f5ac1b29b 127 SUBADR1,
nxp_ip 0:2e6f5ac1b29b 128 SUBADR2,
nxp_ip 0:2e6f5ac1b29b 129 SUBADR3,
nxp_ip 0:2e6f5ac1b29b 130 ALLCALLADR,
nxp_ip 0:2e6f5ac1b29b 131 RESERVED1,
nxp_ip 0:2e6f5ac1b29b 132 RESERVED2,
nxp_ip 0:2e6f5ac1b29b 133 RESERVED3,
nxp_ip 0:2e6f5ac1b29b 134 PWMALL,
nxp_ip 0:2e6f5ac1b29b 135 IREFALL,
nxp_ip 0:2e6f5ac1b29b 136 EFLAG0,
nxp_ip 0:2e6f5ac1b29b 137 EFLAG1,
nxp_ip 0:2e6f5ac1b29b 138
nxp_ip 0:2e6f5ac1b29b 139 REGISTER_START = MODE1,
nxp_ip 0:2e6f5ac1b29b 140 LEDOUT_REGISTER_START = LEDOUT0,
nxp_ip 0:2e6f5ac1b29b 141 PWM_REGISTER_START = PWM0,
nxp_ip 0:2e6f5ac1b29b 142 IREF_REGISTER_START = IREF0,
nxp_ip 0:2e6f5ac1b29b 143 }
nxp_ip 0:2e6f5ac1b29b 144 command_reg;
nxp_ip 0:2e6f5ac1b29b 145
nxp_ip 0:2e6f5ac1b29b 146 enum {
nxp_ip 0:2e6f5ac1b29b 147 AUTO_INCREMENT = 0x80,
nxp_ip 0:2e6f5ac1b29b 148 };
nxp_ip 0:2e6f5ac1b29b 149
nxp_ip 0:2e6f5ac1b29b 150
nxp_ip 0:2e6f5ac1b29b 151 I2C i2c;
nxp_ip 0:2e6f5ac1b29b 152 char i2c_addr;
nxp_ip 0:2e6f5ac1b29b 153 static char init_array[];
nxp_ip 0:2e6f5ac1b29b 154 void write( char *data, int length );
nxp_ip 0:2e6f5ac1b29b 155 void write( command_reg reg_addr, char data );
nxp_ip 0:2e6f5ac1b29b 156 void read( command_reg reg_addr, char *data, int length );
nxp_ip 0:2e6f5ac1b29b 157 char read( command_reg reg_addr );
nxp_ip 0:2e6f5ac1b29b 158 }
nxp_ip 0:2e6f5ac1b29b 159 ;
nxp_ip 0:2e6f5ac1b29b 160
nxp_ip 0:2e6f5ac1b29b 161 #endif // MBED_PCA9955