This is a driver code for the PCA9632 is an I2C-bus controlled 4-bit LED driver optimized for Red/Green/Blue/Amber (RGBA) color mixing applications. In Individual brightness control mode, each LED output has its own 8-bit resolution (256 steps) fixed frequency Individual PWM controller that operates at 1.5625 kHz with a duty cycle that is adjustable from 0 % to 99.6 % to allow the LED to be set to a specific brightness value. This library including device class and "LedPwmOut class API" for ease of use.
Please refer to the component page for details
High-level API is available
A high-level API that can be used as the "PwmOut" of bed-SDK is available.
This API enables to make instances of each LED output pins and control PWM duty cycle by assignment.
For detail information, refer API document of LedPwmOut Class
class which is included in PCA962xA class library.
#include "mbed.h" #include "PCA9632.h" PCA9632 led_cntlr( p28, p27, 0xC4 ); // SDA, SCL, Slave_address(option) LedPwmOut led( led_cntlr, L0 ); int main() { while( 1 ) { for( float p = 0.0f; p < 1.0f; p += 0.1f ) { led = p; wait( 0.1 ); } } }
PCA9632.cpp@0:da1fe3fe7093, 2015-03-04 (annotated)
- Committer:
- nxp_ip
- Date:
- Wed Mar 04 04:12:53 2015 +0000
- Revision:
- 0:da1fe3fe7093
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxp_ip | 0:da1fe3fe7093 | 1 | #include "mbed.h" |
nxp_ip | 0:da1fe3fe7093 | 2 | #include "PCA9632.h" |
nxp_ip | 0:da1fe3fe7093 | 3 | |
nxp_ip | 0:da1fe3fe7093 | 4 | PCA9632::PCA9632( PinName i2c_sda, PinName i2c_scl, char i2c_address ) |
nxp_ip | 0:da1fe3fe7093 | 5 | : i2c_p( new I2C( i2c_sda, i2c_scl ) ), i2c( *i2c_p ), address( i2c_address ) |
nxp_ip | 0:da1fe3fe7093 | 6 | { |
nxp_ip | 0:da1fe3fe7093 | 7 | initialize(); |
nxp_ip | 0:da1fe3fe7093 | 8 | } |
nxp_ip | 0:da1fe3fe7093 | 9 | |
nxp_ip | 0:da1fe3fe7093 | 10 | PCA9632::PCA9632( I2C &i2c_, char i2c_address ) |
nxp_ip | 0:da1fe3fe7093 | 11 | : i2c_p( NULL ), i2c( i2c_ ), address( i2c_address ) |
nxp_ip | 0:da1fe3fe7093 | 12 | { |
nxp_ip | 0:da1fe3fe7093 | 13 | initialize(); |
nxp_ip | 0:da1fe3fe7093 | 14 | } |
nxp_ip | 0:da1fe3fe7093 | 15 | |
nxp_ip | 0:da1fe3fe7093 | 16 | PCA9632::~PCA9632() |
nxp_ip | 0:da1fe3fe7093 | 17 | { |
nxp_ip | 0:da1fe3fe7093 | 18 | if ( NULL != i2c_p ) |
nxp_ip | 0:da1fe3fe7093 | 19 | delete i2c_p; |
nxp_ip | 0:da1fe3fe7093 | 20 | } |
nxp_ip | 0:da1fe3fe7093 | 21 | |
nxp_ip | 0:da1fe3fe7093 | 22 | void PCA9632::initialize( void ) |
nxp_ip | 0:da1fe3fe7093 | 23 | { |
nxp_ip | 0:da1fe3fe7093 | 24 | char init_array0[] = { |
nxp_ip | 0:da1fe3fe7093 | 25 | AUTO_INCREMENT | REGISTER_START, // Command |
nxp_ip | 0:da1fe3fe7093 | 26 | 0x01, 0x00, // MODE1, MODE2 |
nxp_ip | 0:da1fe3fe7093 | 27 | }; |
nxp_ip | 0:da1fe3fe7093 | 28 | char init_array1[] = { |
nxp_ip | 0:da1fe3fe7093 | 29 | AUTO_INCREMENT | LEDOUT, // Command |
nxp_ip | 0:da1fe3fe7093 | 30 | 0xAA, // LEDOUT |
nxp_ip | 0:da1fe3fe7093 | 31 | }; |
nxp_ip | 0:da1fe3fe7093 | 32 | |
nxp_ip | 0:da1fe3fe7093 | 33 | write( init_array0, sizeof( init_array0 ) ); |
nxp_ip | 0:da1fe3fe7093 | 34 | write( init_array1, sizeof( init_array1 ) ); |
nxp_ip | 0:da1fe3fe7093 | 35 | } |
nxp_ip | 0:da1fe3fe7093 | 36 | |
nxp_ip | 0:da1fe3fe7093 | 37 | void PCA9632::reset( void ) |
nxp_ip | 0:da1fe3fe7093 | 38 | { |
nxp_ip | 0:da1fe3fe7093 | 39 | char va[] = { 0xA5, 0x5A }; |
nxp_ip | 0:da1fe3fe7093 | 40 | i2c.write( 0x06, va, sizeof( va ) ); |
nxp_ip | 0:da1fe3fe7093 | 41 | } |
nxp_ip | 0:da1fe3fe7093 | 42 | |
nxp_ip | 0:da1fe3fe7093 | 43 | void PCA9632::pwm( int port, float v ) |
nxp_ip | 0:da1fe3fe7093 | 44 | { |
nxp_ip | 0:da1fe3fe7093 | 45 | char reg_addr; |
nxp_ip | 0:da1fe3fe7093 | 46 | |
nxp_ip | 0:da1fe3fe7093 | 47 | if ( port < N_OF_PORTS ) { |
nxp_ip | 0:da1fe3fe7093 | 48 | reg_addr = PWM_REGISTER_START + port; |
nxp_ip | 0:da1fe3fe7093 | 49 | write( reg_addr, (char)(v * 255.0) ); |
nxp_ip | 0:da1fe3fe7093 | 50 | } else { |
nxp_ip | 0:da1fe3fe7093 | 51 | float va[ N_OF_PORTS ]; |
nxp_ip | 0:da1fe3fe7093 | 52 | |
nxp_ip | 0:da1fe3fe7093 | 53 | for ( int i = 0; i < N_OF_PORTS; i++ ) |
nxp_ip | 0:da1fe3fe7093 | 54 | va[ i ] = v; |
nxp_ip | 0:da1fe3fe7093 | 55 | |
nxp_ip | 0:da1fe3fe7093 | 56 | pwm( va ); |
nxp_ip | 0:da1fe3fe7093 | 57 | } |
nxp_ip | 0:da1fe3fe7093 | 58 | } |
nxp_ip | 0:da1fe3fe7093 | 59 | |
nxp_ip | 0:da1fe3fe7093 | 60 | void PCA9632::pwm( float *vp ) |
nxp_ip | 0:da1fe3fe7093 | 61 | { |
nxp_ip | 0:da1fe3fe7093 | 62 | char data[ N_OF_PORTS + 1 ]; |
nxp_ip | 0:da1fe3fe7093 | 63 | |
nxp_ip | 0:da1fe3fe7093 | 64 | *data = PWM_REGISTER_START; |
nxp_ip | 0:da1fe3fe7093 | 65 | |
nxp_ip | 0:da1fe3fe7093 | 66 | for ( int i = 1; i <= N_OF_PORTS; i++ ) |
nxp_ip | 0:da1fe3fe7093 | 67 | data[ i ] = (char)(*vp++ * 255.0); |
nxp_ip | 0:da1fe3fe7093 | 68 | |
nxp_ip | 0:da1fe3fe7093 | 69 | write( data, sizeof( data ) ); |
nxp_ip | 0:da1fe3fe7093 | 70 | } |
nxp_ip | 0:da1fe3fe7093 | 71 | |
nxp_ip | 0:da1fe3fe7093 | 72 | void PCA9632::write( char *data, int length ) |
nxp_ip | 0:da1fe3fe7093 | 73 | { |
nxp_ip | 0:da1fe3fe7093 | 74 | *data |= AUTO_INCREMENT; |
nxp_ip | 0:da1fe3fe7093 | 75 | i2c.write( address, data, length ); |
nxp_ip | 0:da1fe3fe7093 | 76 | } |
nxp_ip | 0:da1fe3fe7093 | 77 | |
nxp_ip | 0:da1fe3fe7093 | 78 | void PCA9632::write( char reg_addr, char data ) |
nxp_ip | 0:da1fe3fe7093 | 79 | { |
nxp_ip | 0:da1fe3fe7093 | 80 | char c[2]; |
nxp_ip | 0:da1fe3fe7093 | 81 | |
nxp_ip | 0:da1fe3fe7093 | 82 | c[0] = reg_addr; |
nxp_ip | 0:da1fe3fe7093 | 83 | c[1] = data; |
nxp_ip | 0:da1fe3fe7093 | 84 | |
nxp_ip | 0:da1fe3fe7093 | 85 | i2c.write( address, c, 2 ); |
nxp_ip | 0:da1fe3fe7093 | 86 | } |
nxp_ip | 0:da1fe3fe7093 | 87 | |
nxp_ip | 0:da1fe3fe7093 | 88 | void PCA9632::read( char reg_addr, char *data, int length ) |
nxp_ip | 0:da1fe3fe7093 | 89 | { |
nxp_ip | 0:da1fe3fe7093 | 90 | reg_addr |= 0x80; |
nxp_ip | 0:da1fe3fe7093 | 91 | i2c.write( address, (char *)(®_addr), 1, true ); |
nxp_ip | 0:da1fe3fe7093 | 92 | i2c.read( address, data, length ); |
nxp_ip | 0:da1fe3fe7093 | 93 | } |
nxp_ip | 0:da1fe3fe7093 | 94 | |
nxp_ip | 0:da1fe3fe7093 | 95 | char PCA9632::read( char reg_addr ) |
nxp_ip | 0:da1fe3fe7093 | 96 | { |
nxp_ip | 0:da1fe3fe7093 | 97 | i2c.write( address, (char *)(®_addr), 1, true ); |
nxp_ip | 0:da1fe3fe7093 | 98 | i2c.read( address, (char *)(®_addr), 1 ); |
nxp_ip | 0:da1fe3fe7093 | 99 | |
nxp_ip | 0:da1fe3fe7093 | 100 | return ( reg_addr ); |
nxp_ip | 0:da1fe3fe7093 | 101 | } |