Miya Miyagawa
/
NRP2020_main
Outher...Miura Souta
MCP/MCP.cpp@4:15547d07f8e5, 2020-01-25 (annotated)
- Committer:
- Ryosei
- Date:
- Sat Jan 25 00:40:35 2020 +0000
- Revision:
- 4:15547d07f8e5
- Parent:
- 2:32d2cd7d744b
This is the program, used in NRP.
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 | 2:32d2cd7d744b | 9 | _pull_data.all = 0xffff; |
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 | 2:32d2cd7d744b | 40 | void MCP::Update(void) { |
M_souta | 2:32d2cd7d744b | 41 | char data[2] = {GPPUA, 0xff}; |
M_souta | 2:32d2cd7d744b | 42 | int lost = i2c.write(0x40, data, 2); |
M_souta | 2:32d2cd7d744b | 43 | if(lost) { |
M_souta | 2:32d2cd7d744b | 44 | i2c.start(); |
M_souta | 2:32d2cd7d744b | 45 | } |
M_souta | 1:5b0303768126 | 46 | mcp.direction(PORT_A, _iodir_data.port.port_A); |
M_souta | 1:5b0303768126 | 47 | mcp.direction(PORT_B, _iodir_data.port.port_B); |
M_souta | 1:5b0303768126 | 48 | mcp.configurePullUps(PORT_A, _pull_data.port.port_A); |
M_souta | 1:5b0303768126 | 49 | mcp.configurePullUps(PORT_B, _pull_data.port.port_B); |
M_souta | 1:5b0303768126 | 50 | _read_data.port.port_A = mcp.read(PORT_A); |
M_souta | 1:5b0303768126 | 51 | _read_data.port.port_B = mcp.read(PORT_B); |
M_souta | 2:32d2cd7d744b | 52 | mcp.write(PORT_A, _write_data.port.port_A); |
M_souta | 2:32d2cd7d744b | 53 | mcp.write(PORT_B, _write_data.port.port_B); |
M_souta | 2:32d2cd7d744b | 54 | |
M_souta | 1:5b0303768126 | 55 | } |
M_souta | 2:32d2cd7d744b | 56 |