Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#endif

// TODO: declare mcp23017 object

Mcp23017 obj1(0x20);
void setup() {
// initialize Serial for printouts
Serial.begin(115200);
Expand All @@ -23,8 +23,17 @@ void setup() {
//Make sure to check if it is an Active-High or Active-Low reset
#endif
// TODO: initialize I2C and mcp23017 object
Wire.begin();
uint8_t numbers[8] = {0, 0, 0, 0, 0, 1, 0, 0};
obj1.begin(numbers);
}

void loop() {
// TODO: Write tests here
obj1.set_state(7, 1);
Serial.println(obj1.get_state(5));
delay(1000);
obj1.set_state(7, 0);
delay(1000);
Serial.println(obj1.get_state(5));
}
94 changes: 90 additions & 4 deletions src/mcp23017.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,95 @@

// TODO: Initialize i2cBus member
Mcp23017::Mcp23017(int addr) {

this->addr = addr;
}

uint8_t Mcp23017::get_dir(int pin) {
return 0;

//gets information from Mcp23017 register 0x00
Wire.beginTransmission(addr);
Wire.write(0x00);
Wire.endTransmission();

//requests 1 byte from register 0x00 in Mcp23017
Wire.requestFrom(addr, 1);

//reads from the register and shifts the bit position to the LSB, then ands with 1 to get a 1 or 0
return (Wire.read() >> pin) & 1;

}


// TODO: Read from state register
uint8_t Mcp23017::get_state(int pin) {
return 0;

Wire.beginTransmission(addr);
Wire.write(0x12);
Wire.endTransmission();

Wire.requestFrom(addr, 1);

//reads from the register and shifts the bit position to the LSB, then ands with 1 to get a 1 or 0
return (Wire.read() >> pin) & 1;



}

// TODO: Write to directions register
// 1: input 0: output
int Mcp23017::set_dir(int pin, uint8_t dir) {
// calls IODIRA for the read
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x00); // writes the register offset
Wire.endTransmission();

//reads the byte value from the register
uint8_t number;
Wire.requestFrom(addr, 1);
number = Wire.read();

// changes the byte from the register that will convert the correct pin to a 1 or 0
if (dir == 1) {
number = number | (1 << pin); // number is all 0s except a 1 in the input pin position
} else {
number = number & ~(1 << pin); // number is all 1s except a 0 in the input pin position
}

//writes the new register data including the changed pin back to the register
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x00); // writes the register offset
Wire.write(number); // writes the new data
Wire.endTransmission();

return 0;
}

// TODO: Write to state register
int Mcp23017::set_state(int pin, uint8_t val) {
// calls GPIO for the read
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x12); // writes the register offset
Wire.endTransmission();

//reads the byte value from the register
uint8_t number;
Wire.requestFrom(addr, 1);
number = Wire.read();

// changes the byte from the register that will convert the correct pin to a 1 or 0
if (val == 1) {
number = number | (1 << pin); // number is all 0s except a 1 in the input pin position
} else {
number = number & ~(1 << pin); // number is all 1s except a 0 in the input pin position
}

//writes the new register data including the changed pin back to the register
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x12); // writes the register offset
Wire.write(number); // writes the new data
Wire.endTransmission();

return 0;
}

Expand All @@ -34,6 +103,23 @@ int Mcp23017::begin(uint8_t directions[8]) {
int rc;

// TODO: Add device ID check
Wire.beginTransmission(addr); //addr << 1 makes addr bit with LSB = 0
Wire.write(0x00); // writes the register offset
Wire.endTransmission();

// sets each bit in IODIRA according to the bits in directions
Wire.requestFrom(addr, 1);
uint8_t s = Wire.read();
Serial.printf("%d, %d\n",s, addr);
if (s == 0xFF) { // if the read is all 1s then the default value is correct
for (int i = 0; i < 8; i++) {
this->set_dir(i, directions[i]);
}
return 0;
} else {
return 1; // returns 1 if the default value is wrong
}


return 0;

}