gfdgd
Fork of PCA995xA by
PCA9955A/PCA9955A.cpp@7:56a45c690801, 2018-08-14 (annotated)
- Committer:
- tb942
- Date:
- Tue Aug 14 18:26:35 2018 +0000
- Revision:
- 7:56a45c690801
- Parent:
- 6:1c6e1af61981
removed #include mbed.h - it's not needed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxp_ip | 0:a624e2eeccac | 1 | #include "PCA9955A.h" |
nxp_ip | 0:a624e2eeccac | 2 | |
nxp_ip | 0:a624e2eeccac | 3 | PCA9955A::PCA9955A( PinName i2c_sda, PinName i2c_scl, char i2c_address ) |
nxp_ip | 0:a624e2eeccac | 4 | : PCA995xA( i2c_sda, i2c_scl, i2c_address ), n_of_ports( 16 ) |
nxp_ip | 0:a624e2eeccac | 5 | { |
nxp_ip | 0:a624e2eeccac | 6 | initialize(); |
nxp_ip | 0:a624e2eeccac | 7 | } |
nxp_ip | 0:a624e2eeccac | 8 | |
nxp_ip | 0:a624e2eeccac | 9 | PCA9955A::PCA9955A( I2C &i2c_obj, char i2c_address ) |
nxp_ip | 0:a624e2eeccac | 10 | : PCA995xA( i2c_obj, i2c_address ), n_of_ports( 16 ) |
nxp_ip | 0:a624e2eeccac | 11 | { |
nxp_ip | 0:a624e2eeccac | 12 | initialize(); |
nxp_ip | 0:a624e2eeccac | 13 | } |
nxp_ip | 0:a624e2eeccac | 14 | |
nxp_ip | 0:a624e2eeccac | 15 | PCA9955A::~PCA9955A() |
nxp_ip | 0:a624e2eeccac | 16 | { |
nxp_ip | 0:a624e2eeccac | 17 | } |
nxp_ip | 0:a624e2eeccac | 18 | |
nxp_ip | 0:a624e2eeccac | 19 | void PCA9955A::initialize( void ) |
nxp_ip | 0:a624e2eeccac | 20 | { |
nxp_ip | 0:a624e2eeccac | 21 | char init_array[] = { |
nxp_ip | 0:a624e2eeccac | 22 | PCA995xA::AUTO_INCREMENT | REGISTER_START, // Command |
nxp_ip | 0:a624e2eeccac | 23 | 0x00, 0x00, // MODE1, MODE2 |
nxp_ip | 0:a624e2eeccac | 24 | 0xAA, 0xAA, 0xAA, 0xAA, // LEDOUT[3:0] |
nxp_ip | 0:a624e2eeccac | 25 | 0x80, 0x00, // GRPPWM, GRPFREQ |
nxp_ip | 0:a624e2eeccac | 26 | }; |
nxp_ip | 0:a624e2eeccac | 27 | |
nxp_ip | 0:a624e2eeccac | 28 | pwm( ALLPORTS, 0.0 ); |
nxp_ip | 0:a624e2eeccac | 29 | current( ALLPORTS, 0.1 ); |
nxp_ip | 0:a624e2eeccac | 30 | |
nxp_ip | 0:a624e2eeccac | 31 | write( init_array, sizeof( init_array ) ); |
nxp_ip | 0:a624e2eeccac | 32 | gradation_group_clear(); |
nxp_ip | 0:a624e2eeccac | 33 | } |
nxp_ip | 0:a624e2eeccac | 34 | |
nxp_ip | 0:a624e2eeccac | 35 | char PCA9955A::pwm_register_access( int port ) |
nxp_ip | 0:a624e2eeccac | 36 | { |
nxp_ip | 0:a624e2eeccac | 37 | if ( port < n_of_ports ) |
nxp_ip | 0:a624e2eeccac | 38 | return ( PWM_REGISTER_START + port ); |
nxp_ip | 0:a624e2eeccac | 39 | |
nxp_ip | 0:a624e2eeccac | 40 | return ( PWMALL ); |
nxp_ip | 0:a624e2eeccac | 41 | } |
nxp_ip | 0:a624e2eeccac | 42 | |
nxp_ip | 0:a624e2eeccac | 43 | char PCA9955A::current_register_access( int port ) |
nxp_ip | 0:a624e2eeccac | 44 | { |
nxp_ip | 0:a624e2eeccac | 45 | if ( port < n_of_ports ) |
nxp_ip | 0:a624e2eeccac | 46 | return ( IREF_REGISTER_START + port ); |
nxp_ip | 0:a624e2eeccac | 47 | |
nxp_ip | 0:a624e2eeccac | 48 | return ( IREFALL ); |
nxp_ip | 0:a624e2eeccac | 49 | } |
nxp_ip | 0:a624e2eeccac | 50 | |
nxp_ip | 0:a624e2eeccac | 51 | int PCA9955A::number_of_ports( void ) |
nxp_ip | 0:a624e2eeccac | 52 | { |
nxp_ip | 0:a624e2eeccac | 53 | return ( n_of_ports ); |
nxp_ip | 0:a624e2eeccac | 54 | } |
nxp_ip | 0:a624e2eeccac | 55 | |
nxp_ip | 0:a624e2eeccac | 56 | |
nxp_ip | 0:a624e2eeccac | 57 | char PCA9955A::gradation_group[ 16 ] = { 0xFF }; |
nxp_ip | 0:a624e2eeccac | 58 | |
nxp_ip | 0:a624e2eeccac | 59 | void PCA9955A::gradation_setting( int group, char ramp_rate, char step_time, char hold_cntl, char iref ) |
nxp_ip | 0:a624e2eeccac | 60 | { |
nxp_ip | 0:a624e2eeccac | 61 | char data[ 5 ] = { (RAMP_RATE_GRP0 + (group * GRAD_GROUP_OFFSET)) | AUTO_INCREMENT, |
nxp_ip | 0:a624e2eeccac | 62 | ramp_rate, |
nxp_ip | 0:a624e2eeccac | 63 | step_time, |
nxp_ip | 0:a624e2eeccac | 64 | hold_cntl, |
nxp_ip | 0:a624e2eeccac | 65 | iref |
nxp_ip | 0:a624e2eeccac | 66 | }; |
nxp_ip | 0:a624e2eeccac | 67 | |
nxp_ip | 0:a624e2eeccac | 68 | write( data, sizeof( data ) ); |
nxp_ip | 0:a624e2eeccac | 69 | } |
nxp_ip | 0:a624e2eeccac | 70 | |
nxp_ip | 0:a624e2eeccac | 71 | void PCA9955A::gradation_start( char group, char continuous_flag ) |
nxp_ip | 0:a624e2eeccac | 72 | { |
nxp_ip | 0:a624e2eeccac | 73 | static char v[] = { GRAD_CNTL, 0 }; |
okano | 6:1c6e1af61981 | 74 | char rd; |
okano | 6:1c6e1af61981 | 75 | |
okano | 6:1c6e1af61981 | 76 | rd = read( GRAD_CNTL ) & ~(0x3 << (group * 2)); |
okano | 6:1c6e1af61981 | 77 | |
okano | 6:1c6e1af61981 | 78 | printf( "rd = 0x%02X\r\n", rd ); |
okano | 6:1c6e1af61981 | 79 | v[ 1 ] = rd | ((0x02 | continuous_flag) << (group * 2)); |
nxp_ip | 0:a624e2eeccac | 80 | // v[ 1 ] = ((0x02 | continuous_flag) << (group * 2)); |
nxp_ip | 0:a624e2eeccac | 81 | write( v, sizeof( v ) ); |
nxp_ip | 0:a624e2eeccac | 82 | } |
nxp_ip | 0:a624e2eeccac | 83 | |
nxp_ip | 0:a624e2eeccac | 84 | void PCA9955A::gradation_stop( void ) |
nxp_ip | 0:a624e2eeccac | 85 | { |
nxp_ip | 0:a624e2eeccac | 86 | char v[] = { GRAD_CNTL, 0 }; |
nxp_ip | 0:a624e2eeccac | 87 | |
nxp_ip | 0:a624e2eeccac | 88 | write( v, sizeof( v ) ); |
nxp_ip | 0:a624e2eeccac | 89 | } |
nxp_ip | 0:a624e2eeccac | 90 | |
nxp_ip | 0:a624e2eeccac | 91 | void PCA9955A::group_selector( short g0, short g1, short g2, short g3 ) |
nxp_ip | 0:a624e2eeccac | 92 | { |
nxp_ip | 0:a624e2eeccac | 93 | unsigned int pattern = 0x00000000; |
nxp_ip | 0:a624e2eeccac | 94 | short src[ 4 ]; |
nxp_ip | 0:a624e2eeccac | 95 | short port_select; |
nxp_ip | 0:a624e2eeccac | 96 | char v[ 7 ]; |
nxp_ip | 0:a624e2eeccac | 97 | int i; |
nxp_ip | 0:a624e2eeccac | 98 | int grp; |
nxp_ip | 0:a624e2eeccac | 99 | |
nxp_ip | 0:a624e2eeccac | 100 | v[ 0 ] = GRAD_MODE_SEL0 | AUTO_INCREMENT; |
nxp_ip | 0:a624e2eeccac | 101 | |
nxp_ip | 0:a624e2eeccac | 102 | port_select = g0 | g1 | g2 | g3; |
nxp_ip | 0:a624e2eeccac | 103 | v[ 1 ] = (port_select >> 0) & 0xFF; |
nxp_ip | 0:a624e2eeccac | 104 | v[ 2 ] = (port_select >> 8) & 0xFF; |
nxp_ip | 0:a624e2eeccac | 105 | |
nxp_ip | 0:a624e2eeccac | 106 | if ( 0x0000 == port_select ) { |
nxp_ip | 0:a624e2eeccac | 107 | write( v, 3 ); |
nxp_ip | 0:a624e2eeccac | 108 | return; |
nxp_ip | 0:a624e2eeccac | 109 | } |
nxp_ip | 0:a624e2eeccac | 110 | |
nxp_ip | 0:a624e2eeccac | 111 | src[ 0 ] = g0; |
nxp_ip | 0:a624e2eeccac | 112 | src[ 1 ] = g1; |
nxp_ip | 0:a624e2eeccac | 113 | src[ 2 ] = g2; |
nxp_ip | 0:a624e2eeccac | 114 | src[ 3 ] = g3; |
nxp_ip | 0:a624e2eeccac | 115 | |
nxp_ip | 0:a624e2eeccac | 116 | for ( i = 0; i < 16; i++ ) { |
nxp_ip | 0:a624e2eeccac | 117 | for ( grp = 0; grp < 4; grp++ ) { |
nxp_ip | 0:a624e2eeccac | 118 | if ( src[ grp ] & (0x1 << i) ) |
nxp_ip | 0:a624e2eeccac | 119 | pattern |= grp << (i * 2); |
nxp_ip | 0:a624e2eeccac | 120 | } |
nxp_ip | 0:a624e2eeccac | 121 | } |
nxp_ip | 0:a624e2eeccac | 122 | |
nxp_ip | 0:a624e2eeccac | 123 | for ( i = 0; i < 4; i++ ) |
nxp_ip | 0:a624e2eeccac | 124 | v[ i + 3 ] = 0xFF & (pattern >> (i * 8)); |
nxp_ip | 0:a624e2eeccac | 125 | |
nxp_ip | 0:a624e2eeccac | 126 | write( v, sizeof( v ) ); |
nxp_ip | 0:a624e2eeccac | 127 | } |
nxp_ip | 0:a624e2eeccac | 128 | |
nxp_ip | 0:a624e2eeccac | 129 | void PCA9955A::gradation_group_setting( char group, char port ) |
nxp_ip | 0:a624e2eeccac | 130 | { |
nxp_ip | 0:a624e2eeccac | 131 | short g[ 4 ] = { 0x0000 }; |
nxp_ip | 0:a624e2eeccac | 132 | |
nxp_ip | 0:a624e2eeccac | 133 | port &= 0xF; |
nxp_ip | 0:a624e2eeccac | 134 | gradation_group[ port ] = (group < 4) ? group : NOGROUP; |
nxp_ip | 0:a624e2eeccac | 135 | |
nxp_ip | 0:a624e2eeccac | 136 | for ( int i = 0; i < 16; i++ ) { |
nxp_ip | 0:a624e2eeccac | 137 | if ( gradation_group[ i ] != NOGROUP ) |
nxp_ip | 0:a624e2eeccac | 138 | g[ gradation_group[ i ] ] |= 0x0001 << i; |
nxp_ip | 0:a624e2eeccac | 139 | } |
nxp_ip | 0:a624e2eeccac | 140 | |
nxp_ip | 0:a624e2eeccac | 141 | group_selector( g[ 0 ], g[ 1 ], g[ 2 ], g[ 3 ] ); |
nxp_ip | 0:a624e2eeccac | 142 | // pwm( port, 1.0 ); |
nxp_ip | 0:a624e2eeccac | 143 | pwm( port, 0.3 ); |
nxp_ip | 0:a624e2eeccac | 144 | } |
nxp_ip | 0:a624e2eeccac | 145 | |
nxp_ip | 0:a624e2eeccac | 146 | void PCA9955A::gradation_group_clear( void ) |
nxp_ip | 0:a624e2eeccac | 147 | { |
nxp_ip | 0:a624e2eeccac | 148 | for ( int i = 0; i < 16; i++ ) { |
nxp_ip | 0:a624e2eeccac | 149 | gradation_group[ i ] = NOGROUP; |
nxp_ip | 0:a624e2eeccac | 150 | } |
nxp_ip | 0:a624e2eeccac | 151 | |
nxp_ip | 0:a624e2eeccac | 152 | group_selector( 0, 0, 0, 0 ); |
nxp_ip | 0:a624e2eeccac | 153 | } |
nxp_ip | 0:a624e2eeccac | 154 | |
nxp_ip | 0:a624e2eeccac | 155 | float PCA9955A::gradation_ramp_setting( char group, float cycle, char flag_on, char flag_off, int ramp_flag ) |
nxp_ip | 0:a624e2eeccac | 156 | { |
nxp_ip | 0:a624e2eeccac | 157 | int ramp_time; |
nxp_ip | 0:a624e2eeccac | 158 | char prescaler; |
nxp_ip | 0:a624e2eeccac | 159 | int required_steps; |
nxp_ip | 0:a624e2eeccac | 160 | char mult_factor; |
nxp_ip | 0:a624e2eeccac | 161 | char iref_inc; |
nxp_ip | 0:a624e2eeccac | 162 | int iref = 255; |
nxp_ip | 0:a624e2eeccac | 163 | |
nxp_ip | 0:a624e2eeccac | 164 | #define RAMP_RATE_GRP 1 |
nxp_ip | 0:a624e2eeccac | 165 | #define STEP_TIME_GRP 2 |
nxp_ip | 0:a624e2eeccac | 166 | #define HOLD_CNTL_GRP 3 |
nxp_ip | 0:a624e2eeccac | 167 | #define IREF_GRP 4 |
nxp_ip | 0:a624e2eeccac | 168 | |
nxp_ip | 0:a624e2eeccac | 169 | char regs[ 5 ]; |
nxp_ip | 0:a624e2eeccac | 170 | |
nxp_ip | 0:a624e2eeccac | 171 | float hold_time[] = { |
nxp_ip | 0:a624e2eeccac | 172 | 0.00, |
nxp_ip | 0:a624e2eeccac | 173 | 0.25, |
nxp_ip | 0:a624e2eeccac | 174 | 0.50, |
nxp_ip | 0:a624e2eeccac | 175 | 0.75, |
nxp_ip | 0:a624e2eeccac | 176 | 1.00, |
nxp_ip | 0:a624e2eeccac | 177 | 2.00, |
nxp_ip | 0:a624e2eeccac | 178 | 4.00, |
nxp_ip | 0:a624e2eeccac | 179 | 6.00 |
nxp_ip | 0:a624e2eeccac | 180 | }; |
nxp_ip | 0:a624e2eeccac | 181 | |
nxp_ip | 0:a624e2eeccac | 182 | |
nxp_ip | 0:a624e2eeccac | 183 | if ( ramp_flag == NO_RAMP ) { |
nxp_ip | 0:a624e2eeccac | 184 | iref_inc = 1; |
nxp_ip | 0:a624e2eeccac | 185 | ramp_time = 0; |
nxp_ip | 0:a624e2eeccac | 186 | prescaler = 0; |
nxp_ip | 0:a624e2eeccac | 187 | required_steps = 0; |
nxp_ip | 0:a624e2eeccac | 188 | mult_factor = 0; |
nxp_ip | 0:a624e2eeccac | 189 | } else { |
nxp_ip | 0:a624e2eeccac | 190 | ramp_time = (int)((cycle - (hold_time[ flag_on ] + hold_time[ flag_off ])) / ((ramp_flag == RAMP_UP_DOWN) ? 2.0 : 1.0) * 1000); |
nxp_ip | 0:a624e2eeccac | 191 | |
nxp_ip | 0:a624e2eeccac | 192 | prescaler = ( ramp_time <= 32 * (iref + 1)) ? 0 : 1; |
nxp_ip | 0:a624e2eeccac | 193 | |
nxp_ip | 0:a624e2eeccac | 194 | if ( prescaler ) |
nxp_ip | 0:a624e2eeccac | 195 | required_steps = ramp_time / 8; |
nxp_ip | 0:a624e2eeccac | 196 | else |
nxp_ip | 0:a624e2eeccac | 197 | required_steps = ramp_time * 2; |
nxp_ip | 0:a624e2eeccac | 198 | |
nxp_ip | 0:a624e2eeccac | 199 | if ( iref < required_steps ) { |
nxp_ip | 0:a624e2eeccac | 200 | iref_inc = 1; |
nxp_ip | 0:a624e2eeccac | 201 | mult_factor = required_steps / (iref + 1); |
nxp_ip | 0:a624e2eeccac | 202 | mult_factor = (mult_factor <= 0) ? 1 : mult_factor; |
nxp_ip | 0:a624e2eeccac | 203 | mult_factor = (64 < mult_factor) ? 64 : mult_factor; |
nxp_ip | 0:a624e2eeccac | 204 | } else { |
nxp_ip | 0:a624e2eeccac | 205 | iref_inc = (iref + 1) / required_steps; |
nxp_ip | 0:a624e2eeccac | 206 | iref_inc = (iref_inc <= 0) ? 1 : iref_inc; |
nxp_ip | 0:a624e2eeccac | 207 | iref_inc = (64 < iref_inc) ? 64 : iref_inc; |
nxp_ip | 0:a624e2eeccac | 208 | mult_factor = 1; |
nxp_ip | 0:a624e2eeccac | 209 | } |
nxp_ip | 0:a624e2eeccac | 210 | } |
nxp_ip | 0:a624e2eeccac | 211 | |
nxp_ip | 0:a624e2eeccac | 212 | // printf( "ramp_time=%d, required_steps=%d, iref=%d\r\n", ramp_time, required_steps, iref ); |
nxp_ip | 0:a624e2eeccac | 213 | // printf( "prescaler = %d, mult_factor=%d, iref_inc=%d\r\n", prescaler, mult_factor, iref_inc ); |
nxp_ip | 0:a624e2eeccac | 214 | // printf( "\r\n" ); |
nxp_ip | 0:a624e2eeccac | 215 | |
nxp_ip | 0:a624e2eeccac | 216 | regs[ 0 ] = 0x80 | (RAMP_RATE_GRP0 + (group * 4)); |
nxp_ip | 0:a624e2eeccac | 217 | regs[ RAMP_RATE_GRP ] = (ramp_flag << 6) | (iref_inc - 1); |
nxp_ip | 0:a624e2eeccac | 218 | regs[ STEP_TIME_GRP ] = (prescaler << 6) | (mult_factor - 1); |
nxp_ip | 0:a624e2eeccac | 219 | regs[ HOLD_CNTL_GRP ] = 0xC0 | (flag_on << 3) | flag_off; |
nxp_ip | 0:a624e2eeccac | 220 | regs[ IREF_GRP ] = iref; |
nxp_ip | 0:a624e2eeccac | 221 | |
nxp_ip | 0:a624e2eeccac | 222 | write( regs, sizeof( regs ) ); |
nxp_ip | 0:a624e2eeccac | 223 | |
nxp_ip | 0:a624e2eeccac | 224 | #if 0 |
nxp_ip | 0:a624e2eeccac | 225 | float n_of_steps; |
nxp_ip | 0:a624e2eeccac | 226 | float single_ramp_time; |
nxp_ip | 0:a624e2eeccac | 227 | float total_ramp_time; |
nxp_ip | 0:a624e2eeccac | 228 | |
nxp_ip | 0:a624e2eeccac | 229 | n_of_steps = (float)(((iref + 1) / iref_inc) * mult_factor); |
nxp_ip | 0:a624e2eeccac | 230 | single_ramp_time = n_of_steps * (prescaler ? 8.0 : 0.5); |
nxp_ip | 0:a624e2eeccac | 231 | total_ramp_time = single_ramp_time * ((ramp_flag == RAMP_UP_DOWN) ? 2.0 : 1.0); |
nxp_ip | 0:a624e2eeccac | 232 | printf( "n_of_steps = %f, single_ramp_time=%f, total_ramp_time=%f\r\n", n_of_steps, single_ramp_time, total_ramp_time ); |
nxp_ip | 0:a624e2eeccac | 233 | printf( "hold_time[ flag_on ] = %f, hold_time[ flag_off ]=%f\r\n", hold_time[ flag_on ], hold_time[ flag_off ] ); |
nxp_ip | 0:a624e2eeccac | 234 | #endif |
nxp_ip | 0:a624e2eeccac | 235 | |
nxp_ip | 0:a624e2eeccac | 236 | return ( (((((float)(((iref + 1) / iref_inc) * mult_factor)) * (prescaler ? 8.0 : 0.5)) * ((ramp_flag == RAMP_UP_DOWN) ? 2.0 : 1.0)) ) * 0.001 + (hold_time[ flag_on ] + hold_time[ flag_off ]) ); |
nxp_ip | 0:a624e2eeccac | 237 | } |
nxp_ip | 0:a624e2eeccac | 238 | |
nxp_ip | 0:a624e2eeccac | 239 | |
nxp_ip | 0:a624e2eeccac | 240 | void PCA9955A::dump_gradation_registers( void ) |
nxp_ip | 0:a624e2eeccac | 241 | { |
nxp_ip | 0:a624e2eeccac | 242 | char data[ 23 ]; |
nxp_ip | 0:a624e2eeccac | 243 | |
nxp_ip | 0:a624e2eeccac | 244 | read( RAMP_RATE_GRP0, data, sizeof( data ) ); |
nxp_ip | 0:a624e2eeccac | 245 | |
nxp_ip | 0:a624e2eeccac | 246 | printf( "GRAD_MODE_SEL[1|0] = 0x%02X%02X\r\n", data[ 17 ], data[ 16 ] ); |
nxp_ip | 0:a624e2eeccac | 247 | printf( "GRAD_GRP_SEL[3|2|1|0] = 0x%02X%02X%02X%02X\r\n", data[ 21 ], data[ 20 ], data[ 19 ], data[ 18 ] ); |
nxp_ip | 0:a624e2eeccac | 248 | printf( "GRAD_CNTL = 0x%02X\r\n", data[ 22 ] ); |
nxp_ip | 0:a624e2eeccac | 249 | printf( "RAMP_RATE = 0x%02X 0x%02X 0x%02X 0x%02X\r\n", data[ 0 ], data[ 4 ], data[ 8 ], data[ 12 ] ); |
nxp_ip | 0:a624e2eeccac | 250 | printf( "STEP_TIME = 0x%02X 0x%02X 0x%02X 0x%02X\r\n", data[ 1 ], data[ 5 ], data[ 9 ], data[ 13 ] ); |
nxp_ip | 0:a624e2eeccac | 251 | printf( "HOLD_CNTL = 0x%02X 0x%02X 0x%02X 0x%02X\r\n", data[ 2 ], data[ 6 ], data[ 10 ], data[ 14 ] ); |
nxp_ip | 0:a624e2eeccac | 252 | printf( "IREF = 0x%02X 0x%02X 0x%02X 0x%02X\r\n", data[ 3 ], data[ 7 ], data[ 11 ], data[ 15 ] ); |
nxp_ip | 0:a624e2eeccac | 253 | printf( "\r\n" ); |
nxp_ip | 0:a624e2eeccac | 254 | } |