Tedd OKANO
/
PCA9955A_simple
PCA9955A test program
main.cpp@5:b93b28e126fe, 2021-10-20 (annotated)
- Committer:
- okano
- Date:
- Wed Oct 20 08:17:05 2021 +0000
- Revision:
- 5:b93b28e126fe
- Parent:
- 4:507e37f8008d
comment added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:a173ba5ecb1a | 1 | #include "mbed.h" |
okano | 4:507e37f8008d | 2 | #include "PCA9955A_registers.h" |
okano | 0:a173ba5ecb1a | 3 | |
okano | 0:a173ba5ecb1a | 4 | I2C i2c( p28, p27 ); // SDA, SCL |
okano | 0:a173ba5ecb1a | 5 | |
okano | 5:b93b28e126fe | 6 | #define SLAVE_ADDR 0x02 // I2C target (slave) adress |
okano | 0:a173ba5ecb1a | 7 | |
okano | 1:0a5e299bf3ef | 8 | void register_write( char addr, char data ); |
okano | 0:a173ba5ecb1a | 9 | |
okano | 0:a173ba5ecb1a | 10 | int main() |
okano | 0:a173ba5ecb1a | 11 | { |
okano | 4:507e37f8008d | 12 | // initialization |
okano | 4:507e37f8008d | 13 | |
okano | 5:b93b28e126fe | 14 | register_write( MODE1, 0x00 ); // mode setting |
okano | 5:b93b28e126fe | 15 | register_write( MODE2, 0x25 ); // mode setting |
okano | 5:b93b28e126fe | 16 | register_write( LEDOUT0, 0xAA ); // PWM output setting for LED0 .. LED3 |
okano | 5:b93b28e126fe | 17 | register_write( LEDOUT1, 0xAA ); // PWM output setting for LED4 .. LED7 |
okano | 5:b93b28e126fe | 18 | register_write( LEDOUT2, 0xAA ); // PWM output setting for LED8 .. LED11 |
okano | 5:b93b28e126fe | 19 | register_write( LEDOUT3, 0xAA ); // PWM output setting for LED12 .. LED15 |
okano | 5:b93b28e126fe | 20 | register_write( IREFALL, 0x10 ); // set all IREF register (output current setting) |
okano | 4:507e37f8008d | 21 | |
okano | 4:507e37f8008d | 22 | // operation loop |
okano | 0:a173ba5ecb1a | 23 | |
okano | 4:507e37f8008d | 24 | while ( 1 ) { |
okano | 4:507e37f8008d | 25 | |
okano | 5:b93b28e126fe | 26 | // LED0 luminanceset from 0 to max by PWM |
okano | 4:507e37f8008d | 27 | for ( int i = 0; i < 256; i++ ) { |
okano | 4:507e37f8008d | 28 | register_write( PWM0, i ); |
okano | 4:507e37f8008d | 29 | wait( 0.01 ); |
okano | 4:507e37f8008d | 30 | } |
okano | 0:a173ba5ecb1a | 31 | |
okano | 5:b93b28e126fe | 32 | // LED0 luminanceset from max to 0 by PWM |
okano | 4:507e37f8008d | 33 | for ( int i = 255; i >= 0; i-- ) { |
okano | 4:507e37f8008d | 34 | register_write( PWM0, i ); |
okano | 4:507e37f8008d | 35 | wait( 0.01 ); |
okano | 4:507e37f8008d | 36 | } |
okano | 1:0a5e299bf3ef | 37 | } |
okano | 0:a173ba5ecb1a | 38 | } |
okano | 1:0a5e299bf3ef | 39 | |
okano | 5:b93b28e126fe | 40 | void register_write( char addr /* register address */, char data /* data value */ ) |
okano | 1:0a5e299bf3ef | 41 | { |
okano | 1:0a5e299bf3ef | 42 | char d[ 2 ]; |
okano | 2:ead3652b6fd2 | 43 | |
okano | 1:0a5e299bf3ef | 44 | d[ 0 ] = addr; |
okano | 1:0a5e299bf3ef | 45 | d[ 1 ] = data; |
okano | 5:b93b28e126fe | 46 | i2c.write( SLAVE_ADDR, d, 2 ); // two bytes transfer to I2C target device which has address of SLAVE_ADDR |
okano | 1:0a5e299bf3ef | 47 | } |
okano | 1:0a5e299bf3ef | 48 |