Bubble display example using NXP PCAL9555A GPIO expander
Fork of PCAL9555_Hello by
Diff: main.cpp
- Revision:
- 2:ae0b95d42407
- Parent:
- 1:15df36975a85
- Child:
- 3:f74f521b2e46
--- a/main.cpp Thu Mar 19 04:22:27 2015 +0000 +++ b/main.cpp Sat Feb 18 12:54:01 2017 +0000 @@ -1,14 +1,122 @@ #include "mbed.h" + +#if 1 + +#ifdef USE_LIB #include "PCAL9555.h" +#endif + +#if defined(TARGET_LPC81X) +//PCAL9555 gpio_exp(P0_10, P0_11); +I2C gpio_exp(P0_10, P0_11); +#else +//PCAL9555 gpio_exp(SDA, SCL); +I2C gpio_exp(SDA, SCL); +#endif + +//Serial pc(USBTX, USBRX); +Ticker tick; + +static int _seg = 0; +static int _chrs[4]; + +const char dispTabl[] = { + 0x7E, // 0 + 0x30, // 1 + 0x6D, // 2 + 0x79, // 3 + 0x33, // 4 + 0x5B, // 5 + 0x5F, // 6 + 0x70, // 7 + 0x7F, // 8 + 0x7B, // 9 +}; + +void update() +{ + int port_0; + int _data; + + _data = (dispTabl[_chrs[_seg]]); -PCAL9555 gpio_exp( p28, p27, 0xE8 ); // SDA, SCL, Slave_address(option) -GpioBusOut mypins( gpio_exp, X0_0, X0_1, X0_2, X0_3 ); - -int main() { - while( 1 ) { - for( int i = 0; i < 16; i++ ) { - mypins = i; - wait( 0.1 ); - } + switch(_seg) { + case 0: + port_0 = 0x70; + _seg = 1; + break; + case 1: + port_0 = 0xb0; + _seg = 2; + break; + case 2: + port_0 = 0xd0; + _seg = 3; + break; + case 3: + port_0 = 0xe0; + _seg = 0; + break; + + } +#ifdef USE_LIB + gpio_exp.write( (_data << 8) | port_0 ); +#else + char buf[3]; + buf[0] = 2; // OUTPUT + buf[1] = port_0; + buf[2] = _data; // output + gpio_exp.write(0x40, buf, 3); +#endif +} + +void write_number(int num) +{ + num %= 10000; + _chrs[0] = (num/1000); + _chrs[1] = ((num/100)%10); + _chrs[2] = (((num/10)%100)%10); + _chrs[3] = (((num%1000)%100)%10); +} + +int main() +{ +#ifdef USE_LIB + gpio_exp.configure( 0x0000 ); // Set all pins: output +#else + char buf[3]; + buf[0] = 0x06; // CONFIG + buf[1] = 0; // output + buf[2] = 0; // output + gpio_exp.write(0x40, buf, 3); +#endif + + tick.attach(&update, 0.005); // 5msec + + //pc.printf("PCAL9555 test program\n"); + + int cnt = 0; + while(1) { + write_number(cnt++); + wait(0.1); } } + +void error(const char* format, ...) { +} + +#else + +DigitalOut led(LED1); + +int main() +{ + printf("hello\n"); + + while(1) { + led = !led; + wait(0.3); + } +} + +#endif