ttt ko
/
NRP2020_main_1_29
aa
MCP/MCP.h@0:db8d4af513c0, 2020-01-20 (annotated)
- Committer:
- M_souta
- Date:
- Mon Jan 20 08:46:24 2020 +0000
- Revision:
- 0:db8d4af513c0
- Child:
- 1:5b0303768126
jjjjjj
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 | 0:db8d4af513c0 | 5 | |
M_souta | 0:db8d4af513c0 | 6 | #define SDA PB_7 |
M_souta | 0:db8d4af513c0 | 7 | #define SCL PB_6 |
M_souta | 0:db8d4af513c0 | 8 | |
M_souta | 0:db8d4af513c0 | 9 | #define MCP_ADDRESS 0x40 |
M_souta | 0:db8d4af513c0 | 10 | |
M_souta | 0:db8d4af513c0 | 11 | // MCP register address |
M_souta | 0:db8d4af513c0 | 12 | #define IODIRA 0x00 |
M_souta | 0:db8d4af513c0 | 13 | #define IODIRB 0x01 |
M_souta | 0:db8d4af513c0 | 14 | #define IPOLA 0x02 |
M_souta | 0:db8d4af513c0 | 15 | #define IPOLB 0x03 |
M_souta | 0:db8d4af513c0 | 16 | #define GPINTENA 0x04 |
M_souta | 0:db8d4af513c0 | 17 | #define GPINTENB 0x05 |
M_souta | 0:db8d4af513c0 | 18 | #define DEFVALA 0x06 |
M_souta | 0:db8d4af513c0 | 19 | #define DEFVALB 0x07 |
M_souta | 0:db8d4af513c0 | 20 | #define INTCONA 0x08 |
M_souta | 0:db8d4af513c0 | 21 | #define INTCONB 0x09 |
M_souta | 0:db8d4af513c0 | 22 | #define IOCONA 0x0A |
M_souta | 0:db8d4af513c0 | 23 | #define IOCONB 0x0B |
M_souta | 0:db8d4af513c0 | 24 | #define GPPUA 0x0C |
M_souta | 0:db8d4af513c0 | 25 | #define GPPUB 0x0D |
M_souta | 0:db8d4af513c0 | 26 | #define INTFA 0x0E |
M_souta | 0:db8d4af513c0 | 27 | #define INTFB 0x0F |
M_souta | 0:db8d4af513c0 | 28 | #define INTCAPA 0x10 |
M_souta | 0:db8d4af513c0 | 29 | #define INTCAPB 0x11 |
M_souta | 0:db8d4af513c0 | 30 | #define GPIOA 0x12 |
M_souta | 0:db8d4af513c0 | 31 | #define GPIOB 0x13 |
M_souta | 0:db8d4af513c0 | 32 | #define OLATA 0x14 |
M_souta | 0:db8d4af513c0 | 33 | #define OLATB 0x15 |
M_souta | 0:db8d4af513c0 | 34 | |
M_souta | 0:db8d4af513c0 | 35 | typedef enum { |
M_souta | 0:db8d4af513c0 | 36 | OUTPUT, |
M_souta | 0:db8d4af513c0 | 37 | INPUT, |
M_souta | 0:db8d4af513c0 | 38 | INPUT_PULLUP, |
M_souta | 0:db8d4af513c0 | 39 | }pin_mode; |
M_souta | 0:db8d4af513c0 | 40 | |
M_souta | 0:db8d4af513c0 | 41 | typedef union { |
M_souta | 0:db8d4af513c0 | 42 | uint8_t port_A, port_B; |
M_souta | 0:db8d4af513c0 | 43 | uint16_t all; |
M_souta | 0:db8d4af513c0 | 44 | }mcp_register; |
M_souta | 0:db8d4af513c0 | 45 | |
M_souta | 0:db8d4af513c0 | 46 | class MCP { |
M_souta | 0:db8d4af513c0 | 47 | public: |
M_souta | 0:db8d4af513c0 | 48 | /* MCP class constracter |
M_souta | 0:db8d4af513c0 | 49 | / deffult Input |
M_souta | 0:db8d4af513c0 | 50 | / */ |
M_souta | 0:db8d4af513c0 | 51 | MCP(PinName sda, PinName scl, uint8_t device_address); |
M_souta | 0:db8d4af513c0 | 52 | // MCP pin define * pin number is 0 ~ 15 |
M_souta | 0:db8d4af513c0 | 53 | void PinMode(uint8_t pin, uint8_t mode); |
M_souta | 0:db8d4af513c0 | 54 | // MCP DigitalWrite * pin number is 0 ~ 15 |
M_souta | 0:db8d4af513c0 | 55 | void Write(uint8_t pin, bool signal); |
M_souta | 0:db8d4af513c0 | 56 | // MCP DigitalRead * pin number is 0 ~ 15 |
M_souta | 0:db8d4af513c0 | 57 | bool Read(uint8_t pin, bool signal); |
M_souta | 0:db8d4af513c0 | 58 | // MCP initialize |
M_souta | 0:db8d4af513c0 | 59 | void Initialize(void); |
M_souta | 0:db8d4af513c0 | 60 | // MCP register update and read new data |
M_souta | 0:db8d4af513c0 | 61 | void Update(void); |
M_souta | 0:db8d4af513c0 | 62 | // i2c error check |
M_souta | 0:db8d4af513c0 | 63 | bool ErrorOccurred(void); |
M_souta | 0:db8d4af513c0 | 64 | |
M_souta | 0:db8d4af513c0 | 65 | private: |
M_souta | 0:db8d4af513c0 | 66 | I2C i2c; |
M_souta | 0:db8d4af513c0 | 67 | char _read_opcode; |
M_souta | 0:db8d4af513c0 | 68 | char _write_opcode; |
M_souta | 0:db8d4af513c0 | 69 | |
M_souta | 0:db8d4af513c0 | 70 | uint16_t _pull_data; |
M_souta | 0:db8d4af513c0 | 71 | uint16_t _read_data; |
M_souta | 0:db8d4af513c0 | 72 | uint16_t _write_data; |
M_souta | 0:db8d4af513c0 | 73 | |
M_souta | 0:db8d4af513c0 | 74 | void _Write(uint8_t address, uint8_t data); |
M_souta | 0:db8d4af513c0 | 75 | void _Read(uint8_t address, uint8_t data); |
M_souta | 0:db8d4af513c0 | 76 | |
M_souta | 0:db8d4af513c0 | 77 | }; |
M_souta | 0:db8d4af513c0 | 78 | |
M_souta | 0:db8d4af513c0 | 79 | #endif //I2C_H_ |