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
39 changes: 36 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
#include <Arduino.h>
#include "mcp23017.h"
#include <Wire.h>

//Ignore the compiler instruction related code (the ones that start with #) if you are not attempting the optional section


//Uncomment the statement below this line to work on the optional section
// #define EXTERNAL_RESET_ENABLED
//#define EXTERNAL_RESET_ENABLED

#ifdef EXTERNAL_RESET_ENABLED
#define RESET_PIN ______ //TODO: Add the pin thats doing the reset
#endif

// TODO: declare mcp23017 object
Mcp23017 mcp(0x20); // Example declaration, modify as needed

void setup() {
// initialize Serial for printouts
Serial.begin(115200);
printf("Test begins\n");


#ifdef EXTERNAL_RESET_ENABLED
//TODO: Configure the digital pin acting as a reset an output pin and reset the IOExpander registers to their default values.
//Make sure to check if it is an Active-High or Active-Low reset
#endif
// TODO: initialize I2C and mcp23017 object
Wire.begin(); // Initialize I2C
// Initialize MCP23017
uint8_t directions[8] = {
1, // pin 0 = input
1, // pin 1 = input (switch)
1,
1,
1,
1, // pin 5 = input
1,
0 // pin 7 = output (LED)
};
mcp.begin(directions); // Set pin directions
}


void loop() {
// TODO: Write tests here
}
mcp.set_state(7, HIGH);
Serial.println("LED ON");
delay(1000);

mcp.set_state(7, LOW);
Serial.println("LED OFF");
delay(1000);

// Read switch on pin 1
int switchState = mcp.get_state(5);
Serial.print("Switch state: ");
Serial.println(switchState);

delay(500);
}



73 changes: 70 additions & 3 deletions src/mcp23017.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,68 @@
#include "mcp23017.h"

// TODO (optional): Define macros for useful register below:

#define IODIRREG 0x00 // I/O Direction Register
#define GPIOREG 0x12 // GPIO Register

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

this->addr = addr;


}

uint8_t Mcp23017::get_dir(int pin) {
return 0;
Wire.beginTransmission(addr);
Wire.write(IODIRREG); // Point to IODIR register
Wire.endTransmission();
Wire.requestFrom(addr, 1); // Request 1 byte
uint8_t diReg = Wire.read();

return (diReg >> pin) & 0x01;
}


// TODO: Read from state register
uint8_t Mcp23017::get_state(int pin) {
return 0;
Wire.beginTransmission(addr); // Point to GPIO register
Wire.write(GPIOREG);
Wire.endTransmission();
Wire.requestFrom(addr, 1); // Request 1 byte
uint8_t gpioreg = Wire.read();
return (gpioreg >> pin) & 0x01;
}


// TODO: Write to directions register
int Mcp23017::set_dir(int pin, uint8_t dir) {
uint8_t diReg = get_dir(pin); // read current direction register
if(dir ==1) {
diReg |= (1 << pin); // set pin to input
} else {
diReg &= ~(1 << pin); // set pin to output
}
Wire.beginTransmission(addr);
Wire.write(IODIRREG); // Point to IODIR register
Wire.write(diReg); // Write updated direction
Wire.endTransmission();

return 0;
}

// TODO: Write to state register
int Mcp23017::set_state(int pin, uint8_t val) {
uint8_t gpioreg = get_state(pin); // read current state register
if(val ==1) {
gpioreg |= (1 << pin); // set pin high
} else {
gpioreg &= ~(1 << pin); // set pin low

}
Wire.beginTransmission(addr);
Wire.write(GPIOREG); // Point to GPIO register
Wire.write(gpioreg); // Write updated state
Wire.endTransmission();
return 0;
}

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

// TODO: Add device ID check
uint8_t iodira_val = 0;
for(int i=0; i<8; i++) {
iodira_val |= (get_dir(i) << i);
//iodira_val = (iodira_val << 1) | (get_dir(i));
}

Serial.printf("IODIRA register value: 0x%02X\n", iodira_val);

if(iodira_val != 0xFF) {
return 1; // Device ID mismatch
}

uint8_t dirByte = 0;
for(int i=0; i<8; i++) {
dirByte |= (directions[i] << i);
}
Serial.printf("Direction byte: 0x%02X\n", dirByte);
Wire.beginTransmission(addr);
Wire.write(IODIRREG); // Point to IODIR register
Wire.write(dirByte); // Set directions
Wire.endTransmission();


iodira_val = 0;
for(int i=0; i<8; i++) {
iodira_val |= (get_dir(i) << i);
//iodira_val = (iodira_val << 1) | (get_dir(i));
}
Serial.printf("MCP23017 initialized at address 0x%02X\n", iodira_val);

return 0;
}
1 change: 1 addition & 0 deletions tft-display-tutorial
Submodule tft-display-tutorial added at 3173dd