Here is what I’ve been working with. I have had many iterations, so it may not be 100%. It started as you can see from Matt Dobbs New Haven LCD code. I used it as a base and have been tinkering with it since. I’m pretty sure the first part is working correctly as I get a return value of 0. It must be in what I’m trying to write to it, the format or something. I’ve banged my head on the desk for days on it and am just not getting it. Thanks for any help you can provide omri!
This is the output I get:
2013-11-26 19:12:08 UTC-6: [Status] Downloading new code
2013-11-26 19:12:08 UTC-6: [Status] Device configured to be "NewHaven_LCD test code"
2013-11-26 19:12:08 UTC-6: [Device] initializing LCD with return value: 0(0 means successful I2C communication.)
2013-11-26 19:12:08 UTC-6: [Device] 2013/10/27 1:12:8 UTC, using impee xxxx
`/* Basic code to write text to New Haven LCD display via I2C */
// Matt.Dobbs@mcgill.ca June 2013
// New Haven NHD-C0216CIZ-FSW-FBW-3V3 is I2C LCD display, 3V, 2 lines of 16 characters, $11 @ Digikey
//
// Resources:
// http://www.newhavendisplay.com/specs/NHD-C0216CiZ-FSW-FBW-3V3.pdf (datasheet)
// http://dbindner.freeshell.org/msp430/lcd_i2c.html (example for a similar LCD)
// http://www.newhavendisplay.com/app_notes/ST7032.pdf (app note for LCD controller used by New Haven, see font table on last pages)
// font table: http://akizukidenshi.com/download/ds/sitronix/st7032.pdf (last pages)
//-----------------------------------------------------------------------------------------
class NewHaven_LCD {
// Data Members
i2cPort = null;
// This device has a manufacturer hardwired I2C address of 0b01111100=0x7C for write
// change the last bit to one, 0b01111101=0x7D for read.
i2c_writeAddress = 0x40;
i2c_readAddress = 0x41;
//-------------------
constructor( i2c_port ) {
// example: local myLCD = NewHaven_LCD(I2C_12);
if(i2c_port == I2C_12)
{
// Configure I2C bus on pins 1 & 2
hardware.configure(I2C_12);
hardware.i2c12.configure(CLOCK_SPEED_100_KHZ);
i2cPort = hardware.i2c12;
}
else if(i2c_port == I2C_89)
{
// Configure I2C bus on pins 8 & 9
hardware.configure(I2C_89);
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ);
i2cPort = hardware.i2c89;
}
else
{
server.log("Invalid I2C port " + i2c_port + " specified in NewHaven_LCD::constructor.");
}
// initialize the LCD communications:
server.log( "Initializing LCD with return value: " + init_LCD() + " (0 means successful I2C communication.)" );
}
function init_LCD() {
// These initialization parameters follow page 11 of datasheet http://www.newhavendisplay.com/specs/NHD-C0216CiZ-FSW-FBW-3V3.pdf
// Datasheet first sets the function to 0x38= 8-bit bus, 2-line display, extension instruction mode,
// then immediately follows this by the 0x39 function. I can't see any reason to do this, so I removed the first function set.
imp.sleep(0.01); // Wait 10 milliseconds
local result = i2cPort.write(i2c_writeAddress,
format("%c%c",0x80, // Control byte: one instrution word will follow
0x39) ); // function: 8-bit bus, 2-line display, normal instruction mode.
imp.sleep(0.01); // Wait a while,
// This configuration string taken directly from datasheet, page 11.
// 0x01=0b0000.0001 Clear Display
// 0x14=0b0001.0100 Move cursor right one.
// 0x10=0b0001.0000 Move cursor left one.
// 0x1C=0b0001.1100 Scroll right
// 0x18=0b0001.1000 Scroll left
// 0x0C=0b0000.1100 Turn display on
// 0x08=0b0000.1000 Turn display off
// 0x80+=0b1000.0000 Set cursor position
// 0x71=0b0111.0001 Contrast set (datasheet recommeded 0x78, but the contrast was unreadable. 0x71 works best)
// 0x5E=0b0101.1110 Icon display on, booster on, contrast set.
// 0x6D=0b0110.1101 Follower circuit on, amplifier=1?
// 0x0C=0b0000.1100 Display on, cursor off.
// 0x01=0b0000.0001 Clear display.
// 0x06=0b0000.0110 Entry mode set to cursor-moves-right.
/* result = result | i2cPort.write(i2c_writeAddress,
format("%c%c%c%c%c%c%c%c",0x00, // Control byte: indicates many instruction bytes will follow.
0x14,0x71,0x5E,0x6D,0x0C,0x01,0x06) );
*/
result = result | i2cPort.write(i2c_writeAddress,
format("%c%c%c%c",0x00 // Control byte: indicates many instruction bytes will follow.
0x7c157,0x7c4,0x01) ); // Clear display.
imp.sleep(0.01); // Wait 10 milliseconds
return result;
}
function clear_display() {
return i2cPort.write(i2c_writeAddress, format("%c%c",0x80, // Control byte: one instruction word will follow
0x01) ); // Clear display.
}
function goto_line1start() { // returns home to the start of the 1st line, without clearing.
return i2cPort.write(i2c_writeAddress, format("%c%c",0x80, // Control byte: one instruction word will follow
0x02) ); // set cursor to home position
}
function goto_line2start() { // returns to the start of the 2nd line, without clearing.
return i2cPort.write(i2c_writeAddress, format("%c%c",0x80, // Control byte: one instruction word will follow
0xc0) ); // set cursor to position 0x40, set command is 0x80. 0x80+0x40 = 0xC0
}
function show( text ) {
// Each line allows for 40 characters, of which only the first 16 show.
return i2cPort.write(i2c_writeAddress, format("%c",0x40)+ // Control byte: data bytes follow, data is RAM data.
text );
}
function test( text ) {
// Each line allows for 40 characters, of which only the first 16 show.
return i2cPort.write(i2c_writeAddress, format("%c%c",0x0c,0x81));
return i2cPort.write(i2c_writeAddress, format("%c"0x07) "157");
return i2cPort.write(i2c_writeAddress, format("%c"0x81)+"blah");
}
//-------------------
function get_date_string() {
local d = date(); // the time() function would return seconds since 1970.
return ( d["year"] + "/" + d["month"] + "/" + d["day"] + " " +
d["hour"] + ":" + d["min"] + ":" + d["sec"] + " UTC" );
}
}
imp.configure(“NewHaven_LCD test code”, [], []);
// uses i2c on pins 1, 2
local myLCD = NewHaven_LCD(I2C_12);
local counter = 0;
function bigLoop() {
counter = counter +1;
server.log( myLCD.get_date_string() + ", using impee " + hardware.getimpeeid() );
myLCD.test("blah");
/*
myLCD.clear_display();
myLCD.show( “Hello world” + myLCD.get_date_string() );
myLCD.goto_line2start();
myLCD.show(“go Habs, go”);
*/
imp.wakeup(5, bigLoop); // sleep for 10 seconds
}
bigLoop();`