/** * Writes a given register */ void RFduinoProXShield::writeRegister(uint8_t regAddr, uint8_t regValue){ // Write the register Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(regAddr); wiresend(regValue); Wire.endTransmission(); }
/** * Writes all the pins in one go. This method is very useful if you are implementing a multiplexed matrix and want to get a decent refresh rate. */ void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) { Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPIOA); wiresend(ba & 0xFF); wiresend(ba >> 8); Wire.endTransmission(); }
//----------------------------------------------------- // This function clear a single register // RegAdd = Register address // i2caddr = device address (0 to 7. See A0, A1 e A2 configurations) void MCP23017::ClearReg(uint8_t RegAdd, uint8_t i2caddr) { if (i2caddr > 7) { i2caddr = 7; } Wire.beginTransmission(MCP23017_HW_ADD | i2caddr); wiresend(RegAdd); wiresend(0x00); Wire.endTransmission(); }
//----------------------------------------------------- // This function writes a single register // RegAdd = Register address // RegData = Data to write into register selected // i2caddr = device address (0 to 7. See A0, A1 e A2 configurations) void MCP23017::WriteSingleReg(uint8_t RegAdd, uint8_t RegData, uint8_t i2caddr) { if (i2caddr > 7) { i2caddr = 7; } Wire.beginTransmission(MCP23017_HW_ADD | i2caddr); wiresend(RegAdd); wiresend(RegData); Wire.endTransmission(); }
/** * Writes a given register */ void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue){ // Write the register Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(regAddr); wiresend(regValue); Wire.endTransmission(); }
/** * Writes all the pins in one go. This method is very useful if you are implementing a multiplexed matrix and want to get a decent refresh rate. */ void RFduinoProXShield::writeGPIOAB(uint16_t ba) { Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPIOA); wiresend(ba & 0xFF); wiresend(ba >> 8); Wire.endTransmission(); }
void MCP23017::SetAllRegBank1(uint8_t i2caddr) { uint8_t Count = 0; if (i2caddr > 7) { i2caddr = 7; } UpdateArrayDataBank1(); Wire.beginTransmission(MCP23017_HW_ADD | i2caddr); wiresend(MCP23017_BNK1_IODIRA); do { wiresend(ConfReg[Count]); } while (Count++ < sizeof(ConfReg)); Wire.endTransmission(); }
void Adafruit_MCP23017::begin(uint8_t addr) { if (addr > 7) { addr = 7; } i2caddr = addr; Wire.begin(); // set defaults! Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IODIRA); wiresend(0xFF); // all inputs on port A Wire.endTransmission(); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IODIRB); wiresend(0xFF); // all inputs on port B Wire.endTransmission(); //default IOCON register pairs GPA and GPB interrupt outputs Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IOCONA); wiresend(0x40); Wire.endTransmission(); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IOCONB); wiresend(0x40); Wire.endTransmission(); }
/** * Read a single port, A or B, and return its current 8 bit value. * Parameter b should be 0 for GPIOA, and 1 for GPIOB. */ uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) { // read the current GPIO output latches Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); if (b == 0) wiresend(MCP23017_GPIOA); else { wiresend(MCP23017_GPIOB); } Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); return wirerecv(); }
void Adafruit_NFCShield_I2C::wiresendcommand(uint8_t* cmd, uint8_t cmdlen) { uint8_t checksum; cmdlen++; #ifdef PN532DEBUG Serial.print("\nSending: "); #endif delay(2); // or whatever the delay is for waking up the board // I2C START Wire.beginTransmission(PN532_I2C_ADDRESS); checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2; wiresend(PN532_PREAMBLE); wiresend(PN532_PREAMBLE); wiresend(PN532_STARTCODE2); wiresend(cmdlen); wiresend(~cmdlen + 1); wiresend(PN532_HOSTTOPN532); checksum += PN532_HOSTTOPN532; #ifdef PN532DEBUG Serial.print(" 0x"); Serial.print(PN532_PREAMBLE, HEX); Serial.print(" 0x"); Serial.print(PN532_PREAMBLE, HEX); Serial.print(" 0x"); Serial.print(PN532_STARTCODE2, HEX); Serial.print(" 0x"); Serial.print(cmdlen, HEX); Serial.print(" 0x"); Serial.print(~cmdlen + 1, HEX); Serial.print(" 0x"); Serial.print(PN532_HOSTTOPN532, HEX); #endif for (uint8_t i=0; i<cmdlen-1; i++) { wiresend(cmd[i]); checksum += cmd[i]; #ifdef PN532DEBUG Serial.print(" 0x"); Serial.print(cmd[i], HEX); #endif } wiresend(~checksum); wiresend(PN532_POSTAMBLE); // I2C STOP Wire.endTransmission(); #ifdef PN532DEBUG Serial.print(" 0x"); Serial.print(~checksum, HEX); Serial.print(" 0x"); Serial.print(PN532_POSTAMBLE, HEX); Serial.println(); #endif }
//Added to default library //From the Adafruit_RGBLCDShield library we learn that the //buttons are on bits 0..4, which are on GPIOA, //so we use GPINTENA, INTCONA and IOCONA. void Adafruit_MCP23017::enableButtonInterrupt() { uint8_t data; //Enable the interrupts on the button pins 0..4 Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPINTENA); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); data = wirerecv(); data |= 0x1F; //Bits 0..4 high, to enable IOC //Write the new value back Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPINTENA); wiresend(data); Wire.endTransmission(); //We set INTCONA bits 0..4 to 0 = State change interrupt //(instead of compare to state) Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_INTCONA); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); data = wirerecv(); data &= ~0x1F; //Force bits 0..4 to low //Write the new value back Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_INTCONA); wiresend(data); Wire.endTransmission(); //We set the INTA pin to Active-Low //first read current register value Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IOCONA); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); data = wirerecv(); //Bit 1 = INTPOL = Low = Active-Low //(When disabled or no interrupt, signal is high) data &= ~0x02; //Bit 2 = ODR = Low = Open Drain disabled data &= ~0x04; //Bit 6 = MIRROR = Low = INTA/INTB seperate data &= ~0x40; //Write the new value back Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IOCONA); wiresend(data); Wire.endTransmission(); }
/** * Reads a given register */ uint8_t Adafruit_MCP23017::readRegister(uint8_t addr){ // read the current GPINTEN Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(addr); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); return wirerecv(); }
//Added to default library void Adafruit_MCP23017::disableButtonInterrupt() { uint8_t data; //Disable the interrupts on the button pins 0..4 Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPINTENA); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); data = wirerecv(); data &= ~0x1F; //Bits 0..4 low, to disable IOC //Write the new value back Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPINTENA); wiresend(data); Wire.endTransmission(); }
//----------------------------------------------------- // This function read a single register // RegAdd = Register address // i2caddr = device address (0 to 7. See A0, A1 e A2 configurations) // This function return the value of selected register uint8_t MCP23017::ReadSingleReg(uint8_t RegAdd, uint8_t i2caddr) { if (i2caddr > 7) { i2caddr = 7; } Wire.beginTransmission(MCP23017_HW_ADD | i2caddr); wiresend(RegAdd); Wire.endTransmission(); Wire.requestFrom(MCP23017_HW_ADD | i2caddr, 1); return wirerecv(); }
//----------------------------------------------------- // This function sets a single bit into selected register // RegAdd = Register address // Bit = Bit to set // i2caddr = device address (0 to 7. See A0, A1 e A2 configurations) void MCP23017::SetSingleBit(uint8_t RegAdd, uint8_t Bit, uint8_t i2caddr) { uint8_t RegData; if (i2caddr > 7) { i2caddr = 7; } if (Bit > 7) { Bit = 7; } Wire.beginTransmission(MCP23017_HW_ADD | i2caddr); wiresend(RegAdd); Wire.endTransmission(); Wire.requestFrom(MCP23017_HW_ADD | i2caddr, 1); RegData = wirerecv(); RegData |= (0x01 << Bit); Wire.beginTransmission(MCP23017_HW_ADD | i2caddr); wiresend(RegAdd); wiresend(RegData); Wire.endTransmission(); }
void MCP23017::begin(uint8_t addr) { if (addr > 7) { addr = 7; } i2caddr = addr; Wire.begin(); // set defaults! Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IODIRA); wiresend(0xFF); // all inputs on port A Wire.endTransmission(); Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_IODIRB); wiresend(0xFF); // all inputs on port B Wire.endTransmission(); }
void MCP23017::digitalWrite(uint8_t p, uint8_t d) { uint8_t gpio; uint8_t gpioaddr, olataddr; // only 16 bits! if (p > 15) return; if (p < 8) { olataddr = MCP23017_OLATA; gpioaddr = MCP23017_GPIOA; } else { olataddr = MCP23017_OLATB; gpioaddr = MCP23017_GPIOB; p -= 8; } // read the current GPIO output latches Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(olataddr); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); gpio = wirerecv(); // set the pin and direction if (d == HIGH) { gpio |= 1 << p; } else { gpio &= ~(1 << p); } // write the new GPIO Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(gpioaddr); wiresend(gpio); Wire.endTransmission(); }
void MCP23017::pullUp(uint8_t p, uint8_t d) { uint8_t gppu; uint8_t gppuaddr; // only 16 bits! if (p > 15) return; if (p < 8) gppuaddr = MCP23017_GPPUA; else { gppuaddr = MCP23017_GPPUB; p -= 8; } // read the current pullup resistor set Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(gppuaddr); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); gppu = wirerecv(); // set the pin and direction if (d == HIGH) { gppu |= 1 << p; } else { gppu &= ~(1 << p); } // write the new GPIO Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(gppuaddr); wiresend(gppu); Wire.endTransmission(); }
void MCP23017::pinMode(uint8_t p, uint8_t d) { uint8_t iodir; uint8_t iodiraddr; // only 16 bits! if (p > 15) return; if (p < 8) iodiraddr = MCP23017_IODIRA; else { iodiraddr = MCP23017_IODIRB; p -= 8; } // read the current IODIR Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(iodiraddr); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); iodir = wirerecv(); // set the pin and direction if (d == INPUT) { iodir |= 1 << p; } else { iodir &= ~(1 << p); } // write the new IODIR Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(iodiraddr); wiresend(iodir); Wire.endTransmission(); }
/** * Reads all 16 pins (port A and B) into a single 16 bits variable. */ uint16_t Adafruit_MCP23017::readGPIOAB() { uint16_t ba = 0; uint8_t a; // read the current GPIO output latches Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(MCP23017_GPIOA); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 2); a = wirerecv(); ba = wirerecv(); ba <<= 8; ba |= a; return ba; }
uint8_t MCP23017::digitalRead(uint8_t p) { uint8_t gpioaddr; // only 16 bits! if (p > 15) return 0; if (p < 8) gpioaddr = MCP23017_GPIOA; else { gpioaddr = MCP23017_GPIOB; p -= 8; } // read the current GPIO Wire.beginTransmission(MCP23017_ADDRESS | i2caddr); wiresend(gpioaddr); Wire.endTransmission(); Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1); return (wirerecv() >> p) & 0x1; }
void TLC59116::writeRegister(uint8_t reg, uint8_t val) { Wire.beginTransmission(TLC59116_BASEADDR | (_addr & 0x0F)); wiresend(reg); wiresend(val); Wire.endTransmission(); }
void LiquidTWI2::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! // according to datasheet, we need at least 40ms after power rises above 2.7V // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 delay(50); Wire.begin(); uint8_t result; #if defined(MCP23017)&&defined(MCP23008) if (_mcpType == LTI_TYPE_MCP23017) { #endif #ifdef MCP23017 /* don't think we need this // set defaults! Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_IODIRA); wiresend(0xFF); // all inputs on port A result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_IODIRB); wiresend(0xFF); // all inputs on port B result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif */ // now set up input/output pins Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_IODIRA); wiresend(0x0F); // buttons input, all others output result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // set the button pullups Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_GPPUA); wiresend(0x0F); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // now set up input/output pins Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_GPIOA); wiresend(0x00); // buttons input, all others output result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // set button input polarity Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_IPOLA); wiresend(0x0F); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // set button input polarity Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_GPINTENA); wiresend(0x0F); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // set button input polarity Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_INTCONA); wiresend(0x00); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // set button input polarity Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_GPPUA); wiresend(0x0F); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // set button input polarity Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_INTFA); wiresend(0x0F); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_IODIRB); wiresend(0x00); // all pins output result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif Wire.beginTransmission(MCP23017_ADDRESS | _i2cAddr); wiresend(MCP23017_GPIOB); wiresend(0x00); // all pins output result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif #endif // MCP23017 #if defined(MCP23017)&&defined(MCP23008) } else { // MCP23008 #endif #ifdef MCP23008 // first thing we do is get the GPIO expander's head working straight, with a boatload of junk data. Wire.beginTransmission(MCP23008_ADDRESS | _i2cAddr); wiresend(MCP23008_IODIR); wiresend(0xFF); wiresend(0x00); wiresend(0x00); wiresend(0x00); wiresend(0x00); wiresend(0x00); wiresend(0x00); wiresend(0x00); wiresend(0x00); wiresend(0x00); result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif // now we set the GPIO expander's I/O direction to output since it's soldered to an LCD output. Wire.beginTransmission(MCP23008_ADDRESS | _i2cAddr); wiresend(MCP23008_IODIR); wiresend(0x00); // all output: 00000000 for pins 1...8 result = Wire.endTransmission(); #ifdef DETECT_DEVICE if (result) { if (_deviceDetected == 2) { _deviceDetected = 0; return; } } #endif #endif // MCP23008 #if defined(MCP23017)&&defined(MCP23008) } #endif #ifdef DETECT_DEVICE // If we haven't failed by now, then we pass if (_deviceDetected == 2) _deviceDetected = 1; #endif if (lines > 1) { _displayfunction |= LCD_2LINE; } _numlines = lines; _currline = 0; // for some 1 line displays you can select a 10 pixel high font if ((dotsize != 0) && (lines == 1)) { _displayfunction |= LCD_5x10DOTS; } //put the LCD into 4 bit mode // start with a non-standard command to make it realize we're speaking 4-bit here // per LCD datasheet, first command is a single 4-bit burst, 0011. //----- // we cannot assume that the LCD panel is powered at the same time as // the arduino, so we have to perform a software reset as per page 45 // of the HD44780 datasheet - (kch) //----- #if defined(MCP23017)&&defined(MCP23008) if (_mcpType == LTI_TYPE_MCP23017) { #endif // defined(MCP23017)&&defined(MCP23008) #ifdef MCP23017 _backlightBits = 0; // all backlight LED's on // bit pattern for the burstBits function is // // B7 B6 B5 B4 B3 B2 B1 B0 A7 A6 A5 A4 A3 A2 A1 A0 - MCP23017 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // RS RW EN D4 D5 D6 D7 B G R B4 B3 B2 B1 B0 for (uint8_t i=0;i < 3;i++) { burstBits8b((M17_BIT_EN|M17_BIT_D5|M17_BIT_D4) >> 8); burstBits8b((M17_BIT_D5|M17_BIT_D4) >> 8); } burstBits8b((M17_BIT_EN|M17_BIT_D5) >> 8); burstBits8b(M17_BIT_D5 >> 8); #endif // MCP23017 #if defined(MCP23017)&&defined(MCP23008) }