sources and files for the bombatuino project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
5.0 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /**
  2. * @file bombatuino_INPUT_MCP23017.h
  3. *
  4. * @author Lukas Haubaum (lukas@haubaum.de)
  5. *
  6. * @date February, 2013
  7. *
  8. * @brief arduino library for reading inputs from MCP23017 port Expander
  9. *
  10. * library is for specialiced use: all I/O ports are used as digital inputs with internal pullup resistor active, values are stored and a callback function is called, when a value changes.
  11. * ATTETION: Wire.h must be included in sketch #include <Wire.h>
  12. *
  13. * */
  14. #ifndef bombatuino_INPUT_MCP23017_h
  15. #define bombatuino_INPUT_MCP23017_h
  16. #if !defined(CallbackFunction)
  17. /**
  18. * callback function
  19. *
  20. * @param address
  21. * @param pin
  22. * @param value
  23. */
  24. typedef void (*CallbackFunction)(int,int,int);
  25. #endif
  26. class INPUT_MCP23017
  27. {
  28. public:
  29. /**
  30. * initalize the class, should be called in setup() function
  31. *
  32. * @param hardware address (0-7)
  33. * @param callback function
  34. */
  35. void begin(uint8_t addr,CallbackFunction cbF);
  36. /**
  37. * read values and call callback function on change, should be called in loop()
  38. */
  39. void loop(void);
  40. /**
  41. * get value of specific pin (0-15)
  42. *
  43. * @param pin
  44. * @return value of pin
  45. */
  46. int getSpecificValue(uint8_t pin);
  47. private:
  48. uint8_t _addr; /**< hardware address (0-7) */
  49. int _value[16]; /**< read values */
  50. CallbackFunction _callbackFunction; /**< callback function */
  51. };
  52. #define MCP23017_ADDRESS 0x20 /**< hardware address */
  53. /**
  54. * registers of MCP23017 (BANK = 0)
  55. *
  56. * default is 0 except for I/O DIRECTION REGISTERS
  57. *
  58. * for detailed description see http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf
  59. *
  60. * */
  61. #define MCP23017_IODIR_A 0x00 /**< I/O DIRECTION REGISTER PORT A - Controls the direction of the data I/O. */
  62. #define MCP23017_IODIR_B 0x01 /**< I/O DIRECTION REGISTER PORT B - Controls the direction of the data I/O. */
  63. #define MCP23017_IPOL_A 0x02 /**< INPUT POLARITY REGISTER PORT A - This register allows the user to configure the polarity on the corresponding GPIO port bits. */
  64. #define MCP23017_IPOL_B 0x03 /**< INPUT POLARITY REGISTER PORT B - This register allows the user to configure the polarity on the corresponding GPIO port bits. */
  65. #define MCP23017_GPINTEN_A 0x04 /**< INTERRUPT-ON-CHANGE CONTROL REGISTER PORT A - The GPINTEN register controls the interrupt-on-change feature for each pin. */
  66. #define MCP23017_GPINTEN_B 0x05 /**< INTERRUPT-ON-CHANGE CONTROL REGISTER PORT B - The GPINTEN register controls the interrupt-on-change feature for each pin. */
  67. #define MCP23017_DEFVAL_A 0x06 /**< DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE PORT A - The default comparison value is configured in the DEFVAL register. */
  68. #define MCP23017_DEFVAL_B 0x07 /**< DEFAULT COMPARE REGISTER FOR INTERRUPT-ON-CHANGE PORT B - The default comparison value is configured in the DEFVAL register.. */
  69. #define MCP23017_INTCON_A 0x08 /**< INTERRUPT CONTROL REGISTER PORT A - The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature. */
  70. #define MCP23017_INTCON_B 0x09 /**< INTERRUPT CONTROL REGISTER PORT B - The INTCON register controls how the associated pin value is compared for the interrupt-on-change feature.*/
  71. #define MCP23017_IOCON_A 0x0A /**< CONFIGURATION REGISTER - The IOCON register contains several bits for configuring the device. BANK/MIRROR/SEQOP/DISSLW/HAEN/ODR/INTPOL/— */
  72. #define MCP23017_IOCON_B 0x0B /**< CONFIGURATION REGISTER - The IOCON register contains several bits for configuring the device. BANK/MIRROR/SEQOP/DISSLW/HAEN/ODR/INTPOL/— */
  73. #define MCP23017_GPPU_A 0x0C /**< PULL-UP RESISTOR CONFIGURATION REGISTER PORT A - The GPPU register controls the pull-up resistors for the port pins. */
  74. #define MCP23017_GPPU_B 0x0D /**< PULL-UP RESISTOR CONFIGURATION REGISTER PORT B - The GPPU register controls the pull-up resistors for the port pins. */
  75. #define MCP23017_INTF_A 0x0E /**< INTERRUPT FLAG REGISTER PORT A - The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register. */
  76. #define MCP23017_INTF_B 0x0F /**< INTERRUPT FLAG REGISTER PORT B - The INTF register reflects the interrupt condition on the port pins of any pin that is enabled for interrupts via the GPINTEN register. */
  77. #define MCP23017_INTCAP_A 0x10 /**< INTERRUPT CAPTURE REGISTER PORT A - The INTCAP register captures the GPIO port value at the time the interrupt occurred. */
  78. #define MCP23017_INTCAP_B 0x11 /**< INTERRUPT CAPTURE REGISTER PORT B - The INTCAP register captures the GPIO port value at the time the interrupt occurred. */
  79. #define MCP23017_GPIO_A 0x12 /**< PORT REGISTER PORT A - The GPIO register reflects the value on the port. */
  80. #define MCP23017_GPIO_B 0x13 /**< PORT REGISTER PORT B - The GPIO register reflects the value on the port. */
  81. #define MCP23017_OLAT_A 0x14 /**< OUTPUT LATCH REGISTER PORT A - The OLAT register provides access to the output latches. */
  82. #define MCP23017_OLAT_B 0x15 /**< OUTPUT LATCH REGISTER PORT B - The OLAT register provides access to the output latches. */
  83. #endif