MCP23009 is a general IO I2C chip . This lib provide higher level interface ( set pin value, set io modes etc ) without need to have knowledge of the I2C registers
mcp23009.h@1:9146d773a5a3, 2016-06-03 (annotated)
- Committer:
- wbeaumont
- Date:
- Fri Jun 03 16:01:45 2016 +0000
- Revision:
- 1:9146d773a5a3
- Parent:
- 0:d829b4edd564
added more functions , tested with hardware using n FRDM_KL05Z)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 0:d829b4edd564 | 1 | #ifndef MCP_23009_H |
wbeaumont | 0:d829b4edd564 | 2 | #define MCP_23009_H |
wbeaumont | 0:d829b4edd564 | 3 | #include "getVersion.h" |
wbeaumont | 0:d829b4edd564 | 4 | |
wbeaumont | 0:d829b4edd564 | 5 | #include "stdbool.h" |
wbeaumont | 0:d829b4edd564 | 6 | |
wbeaumont | 0:d829b4edd564 | 7 | |
wbeaumont | 0:d829b4edd564 | 8 | #include "dev_interface_def.h" |
wbeaumont | 0:d829b4edd564 | 9 | #include "I2CInterface.h" |
wbeaumont | 0:d829b4edd564 | 10 | #include "DACInterface.h" |
wbeaumont | 0:d829b4edd564 | 11 | |
wbeaumont | 1:9146d773a5a3 | 12 | #define VERSION_MCP23009_HDR "0.20" |
wbeaumont | 0:d829b4edd564 | 13 | |
wbeaumont | 0:d829b4edd564 | 14 | /** MCP23009 class. |
wbeaumont | 0:d829b4edd564 | 15 | * Used for interfacing with a mcp23009 8 bit io expander |
wbeaumont | 0:d829b4edd564 | 16 | * |
wbeaumont | 0:d829b4edd564 | 17 | * It has to be used with the https://developer.mbed.org/users/wbeaumont/code/DevInterfaces/ package |
wbeaumont | 0:d829b4edd564 | 18 | * This includes the "virtual" I2CInterface class that is the interface to the I2C device |
wbeaumont | 0:d829b4edd564 | 19 | * An implementation of the I2Cinterface class for the MBED can be found at |
wbeaumont | 0:d829b4edd564 | 20 | * interrupt is not supported (yet ) |
wbeaumont | 1:9146d773a5a3 | 21 | * V 0.10 initial version |
wbeaumont | 1:9146d773a5a3 | 22 | * V 0.20 added outp_status , tested with hardware. |
wbeaumont | 0:d829b4edd564 | 23 | * (C) Wim Beaumont Universiteit Antwerpen 2016 |
wbeaumont | 0:d829b4edd564 | 24 | * |
wbeaumont | 0:d829b4edd564 | 25 | */ |
wbeaumont | 0:d829b4edd564 | 26 | class MCP23009 : public virtual getVersion { |
wbeaumont | 0:d829b4edd564 | 27 | |
wbeaumont | 0:d829b4edd564 | 28 | public: |
wbeaumont | 0:d829b4edd564 | 29 | |
wbeaumont | 0:d829b4edd564 | 30 | |
wbeaumont | 0:d829b4edd564 | 31 | |
wbeaumont | 0:d829b4edd564 | 32 | |
wbeaumont | 0:d829b4edd564 | 33 | |
wbeaumont | 0:d829b4edd564 | 34 | /** Create an mcp23009 I2C interface |
wbeaumont | 0:d829b4edd564 | 35 | * |
wbeaumont | 0:d829b4edd564 | 36 | * @param i2cinterface pointer to the I2C interface (proto type) |
wbeaumont | 0:d829b4edd564 | 37 | * @param device_address_bits The 3bit address bits of the device ( set via an analogue value on the addresspin) |
wbeaumont | 0:d829b4edd564 | 38 | */ |
wbeaumont | 0:d829b4edd564 | 39 | MCP23009(I2CInterface* i2cinterface, int device_address_bits ); |
wbeaumont | 0:d829b4edd564 | 40 | |
wbeaumont | 0:d829b4edd564 | 41 | |
wbeaumont | 0:d829b4edd564 | 42 | /** set IO pin to input or all , also set the pullup and poloarity ( default = no change |
wbeaumont | 0:d829b4edd564 | 43 | * @param pinnr range 0 .. 8 if 8 all pins are set as input |
wbeaumont | 0:d829b4edd564 | 44 | * @param pullup if positive pull up will be enabled 0 pull up will be disabled if negative no change |
wbeaumont | 0:d829b4edd564 | 45 | * @param polarity if positive polaritye will be inverted 0 std polarity if negative no change |
wbeaumont | 0:d829b4edd564 | 46 | * @return none zero in case of error |
wbeaumont | 0:d829b4edd564 | 47 | */ |
wbeaumont | 0:d829b4edd564 | 48 | int set_as_input(int pinnr,int pullup, int polarity=-1 ); |
wbeaumont | 0:d829b4edd564 | 49 | |
wbeaumont | 0:d829b4edd564 | 50 | /** set IO pin to output |
wbeaumont | 0:d829b4edd564 | 51 | * @param pinnr range 0 .. if 8 all pins are set as output |
wbeaumont | 0:d829b4edd564 | 52 | * @param pullup if true pull up is enabled |
wbeaumont | 0:d829b4edd564 | 53 | * @param polarity if true inverted polarity not yet tested if the polarity bit has effect on the output |
wbeaumont | 0:d829b4edd564 | 54 | * @return none zero in case of error |
wbeaumont | 0:d829b4edd564 | 55 | */ |
wbeaumont | 0:d829b4edd564 | 56 | int set_as_output(int pinnr ,int pullup,int polarity=-1 ); |
wbeaumont | 1:9146d773a5a3 | 57 | |
wbeaumont | 1:9146d773a5a3 | 58 | /** read the status of all IO pins and indicate if the pin, with pinnr is 1 or 0 |
wbeaumont | 1:9146d773a5a3 | 59 | * if pinnr= 8 the status of all pins is returned |
wbeaumont | 1:9146d773a5a3 | 60 | * the status is the level of the port not the "output status" ( open drain so output status can be different) |
wbeaumont | 1:9146d773a5a3 | 61 | * @param pinnr : range 0 .. 8 , if 8 the return value is the status of all pins ,if 0 ..7 it returns 0 or 1 |
wbeaumont | 1:9146d773a5a3 | 62 | * @return 0 or 1 if pinnr 0..7 , byte ( 0 ..255 ) if pinnr =8 neg value in case of I2C error |
wbeaumont | 1:9146d773a5a3 | 63 | */ |
wbeaumont | 0:d829b4edd564 | 64 | int status( int pinnr=8); |
wbeaumont | 1:9146d773a5a3 | 65 | /** read the status of output register all IO pins and indicate if the pin, with pinnr is 1 or 0 |
wbeaumont | 1:9146d773a5a3 | 66 | * if pinnr= 8 the status of all pins is returned |
wbeaumont | 1:9146d773a5a3 | 67 | * the outp_status is the status of the output latch!! not the actual levels on the IO lines |
wbeaumont | 1:9146d773a5a3 | 68 | * @param pinnr : range 0 .. 8 , if 8 the return value is the status of all pins ,if 0 ..7 it returns 0 or 1 |
wbeaumont | 1:9146d773a5a3 | 69 | * @return 0 or 1 if pinnr 0..7 , byte ( 0 ..255 ) if pinnr =8 neg value in case of I2C error |
wbeaumont | 1:9146d773a5a3 | 70 | */ |
wbeaumont | 1:9146d773a5a3 | 71 | int outp_status( int pinnr=8); |
wbeaumont | 1:9146d773a5a3 | 72 | |
wbeaumont | 0:d829b4edd564 | 73 | int set( int pinnr , int value); |
wbeaumont | 0:d829b4edd564 | 74 | protected: |
wbeaumont | 0:d829b4edd564 | 75 | /** pointer to the I2C interface driver. */ |
wbeaumont | 0:d829b4edd564 | 76 | I2CInterface* _i2c_interface; |
wbeaumont | 0:d829b4edd564 | 77 | /** The full i2c device address. */ |
wbeaumont | 0:d829b4edd564 | 78 | int _device_address; |
wbeaumont | 0:d829b4edd564 | 79 | private : |
wbeaumont | 0:d829b4edd564 | 80 | char data[5]; |
wbeaumont | 0:d829b4edd564 | 81 | int set_io_pin(bool input ,int pinnr,int pullup, int polarity ); |
wbeaumont | 0:d829b4edd564 | 82 | int pinmanipulation ( bool active , int pinnr ); |
wbeaumont | 1:9146d773a5a3 | 83 | int status_gen( int pinnr , char reg); |
wbeaumont | 0:d829b4edd564 | 84 | }; |
wbeaumont | 0:d829b4edd564 | 85 | |
wbeaumont | 1:9146d773a5a3 | 86 | #endif |