Program to demonstrate NXP semiconductor's PCA9955B chip working with the BBC micro:bit
Dependencies: PCA995xA microbit
Fork of PCA9955A_Hello by
This is a simple program that allows you to control each port on a PCA9955B connected over i2c to a BBC micro:bit using a simple USB serial communication program.
I've written it to work with the 2 digit SMD soldering kit I designed. Drop me an email at tb942@hotmail.co.uk for further info on that...
main.cpp@6:7164a88905c1, 2018-08-14 (annotated)
- Committer:
- tb942
- Date:
- Tue Aug 14 18:42:53 2018 +0000
- Revision:
- 6:7164a88905c1
- Parent:
- 5:9f89d6e17ff7
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tb942 | 5:9f89d6e17ff7 | 1 | #include "MicroBit.h" //dem libraries tho.... don't forget 'em! (MCU specific) |
| nxp_ip | 2:cfa5103184b1 | 2 | #include "PCA9955A.h" |
| nxp_ip | 2:cfa5103184b1 | 3 | |
| tb942 | 6:7164a88905c1 | 4 | //Allows each led driver port to be triggered on/off over serial - simply send the relevant Hex character using Putty or similar: |
| tb942 | 6:7164a88905c1 | 5 | //(0,1,2,3,4,5,6) = segments for character 1, |
| tb942 | 6:7164a88905c1 | 6 | //(7,8,9,A,B,C,D) = segments for character 2, |
| tb942 | 6:7164a88905c1 | 7 | //(E,F) = decimal points |
| tb942 | 6:7164a88905c1 | 8 | |
| tb942 | 5:9f89d6e17ff7 | 9 | PCA9955A led_cntlr(I2C_SDA0, I2C_SCL0 , 0x3f<<1); //SDA, SCL, Slave_address(option) |
| tb942 | 5:9f89d6e17ff7 | 10 | MicroBitSerial pc(USBTX, USBRX); |
| tb942 | 5:9f89d6e17ff7 | 11 | |
| tb942 | 5:9f89d6e17ff7 | 12 | void lightIt(int, int); //function to turn on specific LEDs |
| tb942 | 5:9f89d6e17ff7 | 13 | void cutItA (); //function to turn off left hand LEDs |
| tb942 | 5:9f89d6e17ff7 | 14 | void cutItB (); //function to turn off right hand LEDs |
| tb942 | 5:9f89d6e17ff7 | 15 | void cutItC (); //function to turn off decimal point LEDs |
| tb942 | 5:9f89d6e17ff7 | 16 | int state[16]; |
| nxp_ip | 2:cfa5103184b1 | 17 | |
| nxp_ip | 2:cfa5103184b1 | 18 | int main() |
| tb942 | 5:9f89d6e17ff7 | 19 | { //Set the operate enable pin low (it's inverted) |
| tb942 | 5:9f89d6e17ff7 | 20 | led_cntlr.current( ALLPORTS, 1 ); //Set all outputs to max current |
| tb942 | 5:9f89d6e17ff7 | 21 | |
| nxp_ip | 2:cfa5103184b1 | 22 | |
| tb942 | 5:9f89d6e17ff7 | 23 | while(1){ |
| tb942 | 5:9f89d6e17ff7 | 24 | if (pc.readable()){ //If there's serial data available |
| tb942 | 5:9f89d6e17ff7 | 25 | char sc = pc.getc(); //Read it |
| tb942 | 5:9f89d6e17ff7 | 26 | int s = (int)sc; |
| tb942 | 5:9f89d6e17ff7 | 27 | if (s > 96) |
| tb942 | 5:9f89d6e17ff7 | 28 | s = s - 87; |
| tb942 | 5:9f89d6e17ff7 | 29 | else if (s > 47) |
| tb942 | 5:9f89d6e17ff7 | 30 | s = s - 48; |
| tb942 | 5:9f89d6e17ff7 | 31 | if (s < 16){ |
| tb942 | 5:9f89d6e17ff7 | 32 | state[s] = !state[s]; |
| tb942 | 5:9f89d6e17ff7 | 33 | int onoff = state[s]; |
| tb942 | 5:9f89d6e17ff7 | 34 | lightIt(s,onoff); |
| tb942 | 5:9f89d6e17ff7 | 35 | } |
| tb942 | 5:9f89d6e17ff7 | 36 | else if (s == 33){ |
| tb942 | 5:9f89d6e17ff7 | 37 | cutItA(); |
| tb942 | 5:9f89d6e17ff7 | 38 | cutItB(); |
| tb942 | 5:9f89d6e17ff7 | 39 | cutItC(); |
| tb942 | 5:9f89d6e17ff7 | 40 | for(int i = 0; i < 16; i++) |
| tb942 | 5:9f89d6e17ff7 | 41 | state[i] = 0; |
| tb942 | 5:9f89d6e17ff7 | 42 | } |
| tb942 | 5:9f89d6e17ff7 | 43 | pc.printf("%d",s); |
| nxp_ip | 2:cfa5103184b1 | 44 | } |
| nxp_ip | 2:cfa5103184b1 | 45 | } |
| nxp_ip | 2:cfa5103184b1 | 46 | } |
| tb942 | 5:9f89d6e17ff7 | 47 | |
| tb942 | 5:9f89d6e17ff7 | 48 | void lightIt (int led, int o){ |
| tb942 | 5:9f89d6e17ff7 | 49 | led_cntlr.pwm(led,o); |
| tb942 | 5:9f89d6e17ff7 | 50 | } |
| tb942 | 5:9f89d6e17ff7 | 51 | |
| tb942 | 5:9f89d6e17ff7 | 52 | void cutItA () { //When called display reset LEDs on left hand side |
| tb942 | 5:9f89d6e17ff7 | 53 | for ( int i = 0; i < 7; i++ ){ |
| tb942 | 5:9f89d6e17ff7 | 54 | led_cntlr.pwm(i,0); |
| tb942 | 5:9f89d6e17ff7 | 55 | } |
| tb942 | 5:9f89d6e17ff7 | 56 | } |
| tb942 | 5:9f89d6e17ff7 | 57 | |
| tb942 | 5:9f89d6e17ff7 | 58 | void cutItB () { //When called display reset LEDs on right hand side |
| tb942 | 5:9f89d6e17ff7 | 59 | for ( int i = 7; i < 14; i++ ){ |
| tb942 | 5:9f89d6e17ff7 | 60 | led_cntlr.pwm(i,0); |
| tb942 | 5:9f89d6e17ff7 | 61 | } |
| tb942 | 5:9f89d6e17ff7 | 62 | } |
| tb942 | 5:9f89d6e17ff7 | 63 | |
| tb942 | 5:9f89d6e17ff7 | 64 | void cutItC () { //When called display reset LEDs on right hand side |
| tb942 | 5:9f89d6e17ff7 | 65 | for ( int i = 14; i < 16; i++ ){ |
| tb942 | 5:9f89d6e17ff7 | 66 | led_cntlr.pwm(i,0); |
| tb942 | 5:9f89d6e17ff7 | 67 | } |
| tb942 | 5:9f89d6e17ff7 | 68 | } |
