Skip to content

Instantly share code, notes, and snippets.

@CRImier
Created May 13, 2015 23:03
Show Gist options
  • Select an option

  • Save CRImier/02075e42b7cbe636fb1e to your computer and use it in GitHub Desktop.

Select an option

Save CRImier/02075e42b7cbe636fb1e to your computer and use it in GitHub Desktop.
This Arduino script is used to communicate to HD44780 LCD attached to WIDE.HK small I2C LCD backpack using MCP23008 IO expander. It's been taken from http://playground.arduino.cc/Code/I2CPortExpanderAndLCDs , seemingly modified by WIDE.HK and then finished by me. I had to concatenate it all in one script (stupid Arduino IDE cannot find .h files …
#include <Wire.h>
class LCDI2C4Bit {
public:
LCDI2C4Bit(int devI2CAddress, int num_lines, int lcdwidth);
void commandWrite(int command);
void init();
void print(int value);
void printIn(char value[]);
void clear();
void backLight( bool turnOn );
void cursorTo(int line_num, int x);
private:
int myNumLines;
int myWidth;
int myAddress;
};
//command bytes for LCD
#define CMD_CLR 0x01
#define CMD_RIGHT 0x1C
#define CMD_LEFT 0x18
#define CMD_HOME 0x02
#define CMD_SETDDRAMADDR 0x80
byte dataPlusMask = 0; // TODO!!!
LCDI2C4Bit::LCDI2C4Bit( int devI2CAddress, int num_lines, int lcdwidth) {
myNumLines = num_lines;
myWidth = lcdwidth;
myAddress = devI2CAddress;
}
void SetMCPReg( byte deviceAddr, byte reg, byte val ) {
Wire.beginTransmission(deviceAddr);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}
void SendToLCD( byte deviceAddr, byte data ) {
data |= dataPlusMask;
SetMCPReg(deviceAddr,0x0A,data);
data ^= 0x80; // E
delayMicroseconds(1);
SetMCPReg(deviceAddr,0x0A,data);
data ^= 0x80; // E
delayMicroseconds(1);
SetMCPReg(deviceAddr,0x0A,data);
delay(1);
}
void WriteLCDByte( byte deviceAddr, byte bdata ) {
Serial.println(bdata, HEX);
SendToLCD(deviceAddr,bdata >> 4);
SendToLCD(deviceAddr,bdata & 0x0F);
}
void LCDI2C4Bit::init( void ) {
Wire.begin();
dataPlusMask = 0; // initial: 0
SetMCPReg(myAddress,0x05,0x0C); // set CONFREG (0x05) to 0
SetMCPReg(myAddress,0x00,0x00); // set IOREG (0x00) to 0
//
delay(50);
SendToLCD(myAddress,0x03);
delay(5);
SendToLCD(myAddress,0x03);
delayMicroseconds(100);
SendToLCD(myAddress,0x03);
delay(5);
SendToLCD(myAddress,0x02);
WriteLCDByte(myAddress,0x28);
WriteLCDByte(myAddress,0x08);
WriteLCDByte(myAddress,0x0C); // turn on, cursor off, no blinking
delayMicroseconds(60);
WriteLCDByte(myAddress,0x01); // clear display
delay(3);
}
void LCDI2C4Bit::backLight( bool turnOn ) {
dataPlusMask |= 0x40; // Lights mask
if (!turnOn) dataPlusMask ^= 0x40;
SetMCPReg(myAddress,0x0A,dataPlusMask);
}
void LCDI2C4Bit::print( int value ) {
dataPlusMask |= 0x10; // RS
WriteLCDByte(myAddress,(byte)value);
dataPlusMask ^= 0x10; // RS
}
void LCDI2C4Bit::printIn( char value[] ) {
for ( char *p = value; *p != 0; p++ )
print(*p);
}
void LCDI2C4Bit::clear() {
commandWrite(CMD_CLR);
}
void LCDI2C4Bit::cursorTo(int line_num, int x) {
int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
commandWrite(CMD_SETDDRAMADDR | (x + row_offsets[line_num]));
}
void LCDI2C4Bit::commandWrite( int command ) {
// RS - leave low
WriteLCDByte(myAddress,command);
delayMicroseconds(100);
}
int ADDR = 0x27;
LCDI2C4Bit lcd = LCDI2C4Bit(ADDR,2,16);
void setup()
{
Serial.begin(115200);
//Wire.begin(); // join i2c bus (address optional for master)
lcd.init();
lcd.clear();
}
void loop()
{
delay(1000);
lcd.cursorTo(0,0);
lcd.printIn("5432109876543210");
lcd.cursorTo(1,0);
lcd.printIn("5432109876543210");
delay(1000);
lcd.cursorTo(0,0);
lcd.printIn("0123456789012345");
lcd.cursorTo(1,0);
lcd.printIn("0123456789012345");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment