#include "LCDi2c4bit.h" #include #include //not needed yet #include //needed for strlen() #include #include "WConstants.h" //all things wiring / arduino LCDI2C4Bit::LCDI2C4Bit( uint8_t devI2CAddress, uint8_t num_lines, uint8_t lcdwidth, uint8_t backlightPin ) { myNumLines = num_lines; myWidth = lcdwidth; myAddress = lcd_i2c_address = devI2CAddress; myBacklightPin = backlightPin; /* * setup and define our pins. usually a good idea toward the top of each program. */ pinMode(myBacklightPin, OUTPUT); // pwm backlight } void LCDI2C4Bit::SetMCPReg( uint8_t reg, uint8_t val ) { Wire.beginTransmission(lcd_i2c_address); Wire.send(reg); Wire.send(val); Wire.endTransmission(); } void LCDI2C4Bit::SendToLCD( uint8_t data ) { data |= dataPlusMask; SetMCPReg(0x0A, data); data ^= 0x80; // E delayMicroseconds(1); SetMCPReg(0x0A, data); data ^= 0x80; // E delayMicroseconds(1); SetMCPReg(0x0A, data); delay(1); } void LCDI2C4Bit::WriteLCDByte( uint8_t bdata ) { SendToLCD( bdata >> 4 ); SendToLCD( bdata & 0x0F ); } void LCDI2C4Bit::init( void ) { dataPlusMask = 0; SetMCPReg(0x05, 0x0C); // set CONFREG (0x05) to 0 SetMCPReg(0x00, 0x00); // set IOREG (0x00) to 0 delay(50); SendToLCD(0x03); delay(5); SendToLCD(0x03); delayMicroseconds(100); SendToLCD(0x03); delay(5); SendToLCD(0x02); WriteLCDByte(0x28); WriteLCDByte(0x08); WriteLCDByte(0x0C); // turn on, cursor off, no blinking delayMicroseconds(60); WriteLCDByte(0x01); // clear display delay(3); } void LCDI2C4Bit::backLight( uint8_t value ) { analogWrite(myBacklightPin, value); // 0..255 gets us 'dark' to 'very bright' } void LCDI2C4Bit::write( uint8_t value ) { dataPlusMask |= 0x10; // RS WriteLCDByte(value); dataPlusMask ^= 0x10; // RS } void LCDI2C4Bit::print( char value[] ) { for ( char *p = value; *p != 0; p++ ) write(*p); } void LCDI2C4Bit::clear() { command(CMD_CLR); } void LCDI2C4Bit::cursorTo( uint8_t row, uint8_t col ) { int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; command(0x80 | (col + row_offsets[row])); } void LCDI2C4Bit::command( uint8_t command ) { // RS - leave low WriteLCDByte(command); delay(1); }