ttt ko
/
NRP2020_main_1_29
aa
MCP/MCP.h@1:5b0303768126, 2020-01-21 (annotated)
- Committer:
- M_souta
- Date:
- Tue Jan 21 11:10:33 2020 +0000
- Revision:
- 1:5b0303768126
- Parent:
- 0:db8d4af513c0
- Child:
- 2:32d2cd7d744b
lll
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
M_souta | 0:db8d4af513c0 | 1 | #ifndef MCP_H_ |
M_souta | 0:db8d4af513c0 | 2 | #define MCP_H_ |
M_souta | 0:db8d4af513c0 | 3 | |
M_souta | 0:db8d4af513c0 | 4 | #include "mbed.h" |
M_souta | 1:5b0303768126 | 5 | #include "MCP23017.h" |
M_souta | 0:db8d4af513c0 | 6 | |
M_souta | 0:db8d4af513c0 | 7 | #define SDA PB_7 |
M_souta | 0:db8d4af513c0 | 8 | #define SCL PB_6 |
M_souta | 0:db8d4af513c0 | 9 | |
M_souta | 0:db8d4af513c0 | 10 | #define MCP_ADDRESS 0x40 |
M_souta | 0:db8d4af513c0 | 11 | |
M_souta | 1:5b0303768126 | 12 | |
M_souta | 1:5b0303768126 | 13 | |
M_souta | 1:5b0303768126 | 14 | typedef enum { |
M_souta | 1:5b0303768126 | 15 | a0 = 0, |
M_souta | 1:5b0303768126 | 16 | a1 = 1, |
M_souta | 1:5b0303768126 | 17 | a2 = 2, |
M_souta | 1:5b0303768126 | 18 | a3 = 3, |
M_souta | 1:5b0303768126 | 19 | a4 = 4, |
M_souta | 1:5b0303768126 | 20 | a5 = 5, |
M_souta | 1:5b0303768126 | 21 | a6 = 6, |
M_souta | 1:5b0303768126 | 22 | a7 = 7, |
M_souta | 1:5b0303768126 | 23 | b0 = 8, |
M_souta | 1:5b0303768126 | 24 | b1 = 9, |
M_souta | 1:5b0303768126 | 25 | b2 = 10, |
M_souta | 1:5b0303768126 | 26 | b3 = 11, |
M_souta | 1:5b0303768126 | 27 | b4 = 12, |
M_souta | 1:5b0303768126 | 28 | b5 = 13, |
M_souta | 1:5b0303768126 | 29 | b6 = 14, |
M_souta | 1:5b0303768126 | 30 | b7 = 15, |
M_souta | 1:5b0303768126 | 31 | }pin_name; |
M_souta | 1:5b0303768126 | 32 | |
M_souta | 0:db8d4af513c0 | 33 | |
M_souta | 0:db8d4af513c0 | 34 | typedef enum { |
M_souta | 0:db8d4af513c0 | 35 | OUTPUT, |
M_souta | 0:db8d4af513c0 | 36 | INPUT, |
M_souta | 0:db8d4af513c0 | 37 | INPUT_PULLUP, |
M_souta | 0:db8d4af513c0 | 38 | }pin_mode; |
M_souta | 0:db8d4af513c0 | 39 | |
M_souta | 1:5b0303768126 | 40 | typedef struct byte{ |
M_souta | 1:5b0303768126 | 41 | uint8_t port_A; |
M_souta | 1:5b0303768126 | 42 | uint8_t port_B; |
M_souta | 1:5b0303768126 | 43 | }byte; |
M_souta | 1:5b0303768126 | 44 | |
M_souta | 0:db8d4af513c0 | 45 | typedef union { |
M_souta | 1:5b0303768126 | 46 | byte port; |
M_souta | 0:db8d4af513c0 | 47 | uint16_t all; |
M_souta | 1:5b0303768126 | 48 | }mcp_data; |
M_souta | 0:db8d4af513c0 | 49 | |
M_souta | 0:db8d4af513c0 | 50 | class MCP { |
M_souta | 0:db8d4af513c0 | 51 | public: |
M_souta | 0:db8d4af513c0 | 52 | /* MCP class constracter |
M_souta | 0:db8d4af513c0 | 53 | / deffult Input |
M_souta | 0:db8d4af513c0 | 54 | / */ |
M_souta | 0:db8d4af513c0 | 55 | MCP(PinName sda, PinName scl, uint8_t device_address); |
M_souta | 0:db8d4af513c0 | 56 | // MCP pin define * pin number is 0 ~ 15 |
M_souta | 1:5b0303768126 | 57 | void PinMode(uint8_t pin, pin_mode mode); |
M_souta | 0:db8d4af513c0 | 58 | // MCP DigitalWrite * pin number is 0 ~ 15 |
M_souta | 0:db8d4af513c0 | 59 | void Write(uint8_t pin, bool signal); |
M_souta | 0:db8d4af513c0 | 60 | // MCP DigitalRead * pin number is 0 ~ 15 |
M_souta | 1:5b0303768126 | 61 | bool Read(uint8_t pin); |
M_souta | 0:db8d4af513c0 | 62 | // MCP initialize |
M_souta | 0:db8d4af513c0 | 63 | void Initialize(void); |
M_souta | 0:db8d4af513c0 | 64 | // MCP register update and read new data |
M_souta | 0:db8d4af513c0 | 65 | void Update(void); |
M_souta | 0:db8d4af513c0 | 66 | // i2c error check |
M_souta | 0:db8d4af513c0 | 67 | bool ErrorOccurred(void); |
M_souta | 0:db8d4af513c0 | 68 | |
M_souta | 0:db8d4af513c0 | 69 | private: |
M_souta | 0:db8d4af513c0 | 70 | I2C i2c; |
M_souta | 1:5b0303768126 | 71 | MCP23017 mcp; |
M_souta | 0:db8d4af513c0 | 72 | char _read_opcode; |
M_souta | 0:db8d4af513c0 | 73 | char _write_opcode; |
M_souta | 0:db8d4af513c0 | 74 | |
M_souta | 1:5b0303768126 | 75 | mcp_data _iodir_data; |
M_souta | 1:5b0303768126 | 76 | mcp_data _pull_data; |
M_souta | 1:5b0303768126 | 77 | mcp_data _read_data; |
M_souta | 1:5b0303768126 | 78 | mcp_data _write_data; |
M_souta | 0:db8d4af513c0 | 79 | |
M_souta | 0:db8d4af513c0 | 80 | void _Write(uint8_t address, uint8_t data); |
M_souta | 1:5b0303768126 | 81 | void _Read(uint8_t address, uint8_t data); |
M_souta | 0:db8d4af513c0 | 82 | |
M_souta | 0:db8d4af513c0 | 83 | }; |
M_souta | 0:db8d4af513c0 | 84 | |
M_souta | 0:db8d4af513c0 | 85 | #endif //I2C_H_ |