Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MCP.cpp
00001 #include "MCP.h" 00002 #include "mbed.h" 00003 #include "MCP23017.h" 00004 00005 MCP::MCP(PinName sda, PinName scl, uint8_t device_address) 00006 :i2c(sda, scl), mcp(i2c, device_address) 00007 { 00008 _iodir_data.all = 0xffff; 00009 _pull_data.all = 0xffff; 00010 _read_data.all = 0x0000; 00011 _write_data.all = 0x0000; 00012 } 00013 00014 void MCP::PinMode(uint8_t pin, pin_mode mode) 00015 { 00016 if(mode == OUTPUT) { 00017 _iodir_data.all &= ~(0x0001 << pin); 00018 } else if(mode == INPUT) { 00019 _iodir_data.all |= (0x0001 << pin); 00020 } else if(mode == INPUT_PULLUP) { 00021 _iodir_data.all |= (0x0001 << pin); 00022 _pull_data.all |= (0x0001 << pin); 00023 } 00024 } 00025 00026 void MCP::Write(uint8_t pin, bool signal) 00027 { 00028 if(signal == 1) { 00029 _write_data.all |= (0x0001 << pin); 00030 } else { 00031 _write_data.all &= ~(0x0001 << pin); 00032 } 00033 } 00034 00035 bool MCP::Read(uint8_t pin) 00036 { 00037 return (_read_data.all >> pin) & 0x01; 00038 } 00039 00040 void MCP::Update(void) { 00041 i2c.frequency(100000); 00042 char data[2] = {GPPUA, 0xff}; 00043 int lost = i2c.write(0x40, data, 2); 00044 if(lost) { 00045 i2c.start(); 00046 } 00047 mcp.direction(PORT_A, _iodir_data.port.port_A); 00048 mcp.direction(PORT_B, _iodir_data.port.port_B); 00049 mcp.configurePullUps(PORT_A, _pull_data.port.port_A); 00050 mcp.configurePullUps(PORT_B, _pull_data.port.port_B); 00051 _read_data.port.port_A = mcp.read(PORT_A); 00052 _read_data.port.port_B = mcp.read(PORT_B); 00053 mcp.write(PORT_A, _write_data.port.port_A); 00054 mcp.write(PORT_B, _write_data.port.port_B); 00055 00056 } 00057
Generated on Thu Jul 14 2022 04:34:33 by
1.7.2