Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: PCA9626_Hello PCA9624_Hello PCA9622_Hello
base_class/PCA962x.cpp@0:7a206e6db594, 2015-02-26 (annotated)
- Committer:
- nxp_ip
- Date:
- Thu Feb 26 09:16:41 2015 +0000
- Revision:
- 0:7a206e6db594
Initial version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nxp_ip | 0:7a206e6db594 | 1 | #include "mbed.h" |
| nxp_ip | 0:7a206e6db594 | 2 | #include "PCA962x.h" |
| nxp_ip | 0:7a206e6db594 | 3 | |
| nxp_ip | 0:7a206e6db594 | 4 | PCA962x::PCA962x( PinName i2c_sda, PinName i2c_scl, char i2c_address ) |
| nxp_ip | 0:7a206e6db594 | 5 | : i2c_p( new I2C( i2c_sda, i2c_scl ) ), i2c( *i2c_p ), address( i2c_address ) |
| nxp_ip | 0:7a206e6db594 | 6 | { |
| nxp_ip | 0:7a206e6db594 | 7 | } |
| nxp_ip | 0:7a206e6db594 | 8 | |
| nxp_ip | 0:7a206e6db594 | 9 | PCA962x::PCA962x( I2C &i2c_, char i2c_address ) |
| nxp_ip | 0:7a206e6db594 | 10 | : i2c_p( NULL ), i2c( i2c_ ), address( i2c_address ) |
| nxp_ip | 0:7a206e6db594 | 11 | { |
| nxp_ip | 0:7a206e6db594 | 12 | } |
| nxp_ip | 0:7a206e6db594 | 13 | |
| nxp_ip | 0:7a206e6db594 | 14 | PCA962x::~PCA962x() |
| nxp_ip | 0:7a206e6db594 | 15 | { |
| nxp_ip | 0:7a206e6db594 | 16 | if ( NULL != i2c_p ) |
| nxp_ip | 0:7a206e6db594 | 17 | delete i2c_p; |
| nxp_ip | 0:7a206e6db594 | 18 | } |
| nxp_ip | 0:7a206e6db594 | 19 | |
| nxp_ip | 0:7a206e6db594 | 20 | void PCA962x::reset( void ) |
| nxp_ip | 0:7a206e6db594 | 21 | { |
| nxp_ip | 0:7a206e6db594 | 22 | char va[] = { 0xA5, 0x5A }; |
| nxp_ip | 0:7a206e6db594 | 23 | i2c.write( 0x06, va, sizeof( va ) ); |
| nxp_ip | 0:7a206e6db594 | 24 | } |
| nxp_ip | 0:7a206e6db594 | 25 | |
| nxp_ip | 0:7a206e6db594 | 26 | void PCA962x::pwm( int port, float v ) |
| nxp_ip | 0:7a206e6db594 | 27 | { |
| nxp_ip | 0:7a206e6db594 | 28 | char reg_addr; |
| nxp_ip | 0:7a206e6db594 | 29 | |
| nxp_ip | 0:7a206e6db594 | 30 | reg_addr = pwm_register_access( port ); |
| nxp_ip | 0:7a206e6db594 | 31 | |
| nxp_ip | 0:7a206e6db594 | 32 | if ( PWMALL == reg_addr ) { |
| nxp_ip | 0:7a206e6db594 | 33 | int np = number_of_ports(); |
| nxp_ip | 0:7a206e6db594 | 34 | float va[ np ]; |
| nxp_ip | 0:7a206e6db594 | 35 | |
| nxp_ip | 0:7a206e6db594 | 36 | for ( int i = 0; i < np; i++ ) |
| nxp_ip | 0:7a206e6db594 | 37 | va[ i ] = v; |
| nxp_ip | 0:7a206e6db594 | 38 | |
| nxp_ip | 0:7a206e6db594 | 39 | pwm( va ); |
| nxp_ip | 0:7a206e6db594 | 40 | |
| nxp_ip | 0:7a206e6db594 | 41 | } else { |
| nxp_ip | 0:7a206e6db594 | 42 | write( reg_addr, (char)(v * 255.0) ); |
| nxp_ip | 0:7a206e6db594 | 43 | } |
| nxp_ip | 0:7a206e6db594 | 44 | } |
| nxp_ip | 0:7a206e6db594 | 45 | |
| nxp_ip | 0:7a206e6db594 | 46 | void PCA962x::pwm( float *vp ) |
| nxp_ip | 0:7a206e6db594 | 47 | { |
| nxp_ip | 0:7a206e6db594 | 48 | int n_of_ports = number_of_ports(); |
| nxp_ip | 0:7a206e6db594 | 49 | char data[ n_of_ports + 1 ]; |
| nxp_ip | 0:7a206e6db594 | 50 | |
| nxp_ip | 0:7a206e6db594 | 51 | *data = pwm_register_access( 0 ); |
| nxp_ip | 0:7a206e6db594 | 52 | |
| nxp_ip | 0:7a206e6db594 | 53 | for ( int i = 1; i <= n_of_ports; i++ ) |
| nxp_ip | 0:7a206e6db594 | 54 | data[ i ] = (char)(*vp++ * 255.0); |
| nxp_ip | 0:7a206e6db594 | 55 | |
| nxp_ip | 0:7a206e6db594 | 56 | write( data, sizeof( data ) ); |
| nxp_ip | 0:7a206e6db594 | 57 | } |
| nxp_ip | 0:7a206e6db594 | 58 | |
| nxp_ip | 0:7a206e6db594 | 59 | void PCA962x::write( char *data, int length ) |
| nxp_ip | 0:7a206e6db594 | 60 | { |
| nxp_ip | 0:7a206e6db594 | 61 | *data |= AUTO_INCREMENT; |
| nxp_ip | 0:7a206e6db594 | 62 | i2c.write( address, data, length ); |
| nxp_ip | 0:7a206e6db594 | 63 | } |
| nxp_ip | 0:7a206e6db594 | 64 | |
| nxp_ip | 0:7a206e6db594 | 65 | void PCA962x::write( char reg_addr, char data ) |
| nxp_ip | 0:7a206e6db594 | 66 | { |
| nxp_ip | 0:7a206e6db594 | 67 | char c[2]; |
| nxp_ip | 0:7a206e6db594 | 68 | |
| nxp_ip | 0:7a206e6db594 | 69 | c[0] = reg_addr; |
| nxp_ip | 0:7a206e6db594 | 70 | c[1] = data; |
| nxp_ip | 0:7a206e6db594 | 71 | |
| nxp_ip | 0:7a206e6db594 | 72 | i2c.write( address, c, 2 ); |
| nxp_ip | 0:7a206e6db594 | 73 | } |
| nxp_ip | 0:7a206e6db594 | 74 | |
| nxp_ip | 0:7a206e6db594 | 75 | void PCA962x::read( char reg_addr, char *data, int length ) |
| nxp_ip | 0:7a206e6db594 | 76 | { |
| nxp_ip | 0:7a206e6db594 | 77 | reg_addr |= 0x80; |
| nxp_ip | 0:7a206e6db594 | 78 | i2c.write( address, (char *)(®_addr), 1, true ); |
| nxp_ip | 0:7a206e6db594 | 79 | i2c.read( address, data, length ); |
| nxp_ip | 0:7a206e6db594 | 80 | } |
| nxp_ip | 0:7a206e6db594 | 81 | |
| nxp_ip | 0:7a206e6db594 | 82 | char PCA962x::read( char reg_addr ) |
| nxp_ip | 0:7a206e6db594 | 83 | { |
| nxp_ip | 0:7a206e6db594 | 84 | i2c.write( address, (char *)(®_addr), 1, true ); |
| nxp_ip | 0:7a206e6db594 | 85 | i2c.read( address, (char *)(®_addr), 1 ); |
| nxp_ip | 0:7a206e6db594 | 86 | |
| nxp_ip | 0:7a206e6db594 | 87 | return ( reg_addr ); |
| nxp_ip | 0:7a206e6db594 | 88 | } |
PCA9622, PCA9624, PCA9626 : 8, 16 & 24ch LED driver (Voltage switch type)