toshi mura
/
NRP2020_main
gjyrjyykkudt
MCP/MCP.cpp@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 | #include "MCP.h" |
M_souta | 0:db8d4af513c0 | 2 | #include "mbed.h" |
M_souta | 1:5b0303768126 | 3 | #include "MCP23017.h" |
M_souta | 0:db8d4af513c0 | 4 | |
M_souta | 0:db8d4af513c0 | 5 | MCP::MCP(PinName sda, PinName scl, uint8_t device_address) |
M_souta | 1:5b0303768126 | 6 | :i2c(sda, scl), mcp(i2c, device_address) |
M_souta | 0:db8d4af513c0 | 7 | { |
M_souta | 1:5b0303768126 | 8 | _iodir_data.all = 0xffff; |
M_souta | 1:5b0303768126 | 9 | _pull_data.all = 0x0000; |
M_souta | 1:5b0303768126 | 10 | _read_data.all = 0x0000; |
M_souta | 1:5b0303768126 | 11 | _write_data.all = 0x0000; |
M_souta | 1:5b0303768126 | 12 | } |
M_souta | 1:5b0303768126 | 13 | |
M_souta | 1:5b0303768126 | 14 | void MCP::PinMode(uint8_t pin, pin_mode mode) |
M_souta | 1:5b0303768126 | 15 | { |
M_souta | 1:5b0303768126 | 16 | if(mode == OUTPUT) { |
M_souta | 1:5b0303768126 | 17 | _iodir_data.all &= ~(0x0001 << pin); |
M_souta | 1:5b0303768126 | 18 | } else if(mode == INPUT) { |
M_souta | 1:5b0303768126 | 19 | _iodir_data.all |= (0x0001 << pin); |
M_souta | 1:5b0303768126 | 20 | } else if(mode == INPUT_PULLUP) { |
M_souta | 1:5b0303768126 | 21 | _iodir_data.all |= (0x0001 << pin); |
M_souta | 1:5b0303768126 | 22 | _pull_data.all |= (0x0001 << pin); |
M_souta | 1:5b0303768126 | 23 | } |
M_souta | 0:db8d4af513c0 | 24 | } |
M_souta | 0:db8d4af513c0 | 25 | |
M_souta | 1:5b0303768126 | 26 | void MCP::Write(uint8_t pin, bool signal) |
M_souta | 1:5b0303768126 | 27 | { |
M_souta | 1:5b0303768126 | 28 | if(signal == 1) { |
M_souta | 1:5b0303768126 | 29 | _write_data.all |= (0x0001 << pin); |
M_souta | 1:5b0303768126 | 30 | } else { |
M_souta | 1:5b0303768126 | 31 | _write_data.all &= ~(0x0001 << pin); |
M_souta | 1:5b0303768126 | 32 | } |
M_souta | 1:5b0303768126 | 33 | } |
M_souta | 1:5b0303768126 | 34 | |
M_souta | 1:5b0303768126 | 35 | bool MCP::Read(uint8_t pin) |
M_souta | 0:db8d4af513c0 | 36 | { |
M_souta | 1:5b0303768126 | 37 | return (_read_data.all >> pin) & 0x01; |
M_souta | 1:5b0303768126 | 38 | } |
M_souta | 1:5b0303768126 | 39 | |
M_souta | 1:5b0303768126 | 40 | void MCP::Update(void) |
M_souta | 1:5b0303768126 | 41 | { |
M_souta | 1:5b0303768126 | 42 | mcp.direction(PORT_A, _iodir_data.port.port_A); |
M_souta | 1:5b0303768126 | 43 | mcp.direction(PORT_B, _iodir_data.port.port_B); |
M_souta | 1:5b0303768126 | 44 | mcp.configurePullUps(PORT_A, _pull_data.port.port_A); |
M_souta | 1:5b0303768126 | 45 | mcp.configurePullUps(PORT_B, _pull_data.port.port_B); |
M_souta | 1:5b0303768126 | 46 | _read_data.port.port_A = mcp.read(PORT_A); |
M_souta | 1:5b0303768126 | 47 | _read_data.port.port_B = mcp.read(PORT_B); |
M_souta | 1:5b0303768126 | 48 | // mcp.write(PORT_A, _write_data.port_A); |
M_souta | 1:5b0303768126 | 49 | // mcp.write(PORT_B, _write_data.port_B); |
M_souta | 1:5b0303768126 | 50 | } |