Skip to content

Instantly share code, notes, and snippets.

@jadonk

jadonk/lcd1.js

Created Apr 3, 2013
Embed
What would you like to do?
//******************************************************************
var b = require ('bonescript');
var LCD_RS = "P8_12"; //LCD Register Set (RS) control
var LCD_E = "P8_14"; //LCD Enable (E) control
var LCD_DB0 = "P8_16"; //LCD Data line DB0
var LCD_DB1 = "P8_18"; //LCD Data line DB1
var LCD_DB2 = "P8_20"; //LCD Data line DB2
var LCD_DB3 = "P8_22"; //LCD Data line DB3
var LCD_DB4 = "P8_24"; //LCD Data line DB4
var LCD_DB5 = "P8_26"; //LCD Data line DB5
var LCD_DB6 = "P8_28"; //LCD Data line DB6
var LCD_DB7 = "P8_30"; //LCD Data line DB7
var counter = 1; //counter to be displayed
b.pinMode(LCD_RS, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_E, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB0, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB1, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB2, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB3, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB4, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB5, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB6, b.OUTPUT); //set pin to digital output
b.pinMode(LCD_DB7, b.OUTPUT); //set pin to digital output
LCD_init(LCD_update); //call LCD initialize
//******************************************************************
//LCD_update
//******************************************************************
function LCD_update()
{
LCD_putchar(counter, next); //write 'counter' value to LCD
//When LCD_putchar completes, schedule the next run of it
function next()
{
counter++; //update counter
if(counter > 9) //Re-init after 9
{
counter = 1;
LCD_init(LCD_update);
}
else
setTimeout(LCD_update, 500); //update again in 500 ms
}
}
//******************************************************************
//LCD_init
//******************************************************************
function LCD_init(callback)
{
//LCD Enable (E) pin low
b.digitalWrite(LCD_E, b.LOW);
//Start at the beginning of the list of steps to perform
var i = 0;
//List of steps to perform
var steps =
[
function(){ setTimeout(next, 15); }, //delay 15ms
function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation
function(){ setTimeout(next, 5); }, //delay 5ms
function(){ LCD_putcommand(0x38, next); }, //set for 8-bit operation
function(){ LCD_putcommand(0x38, next); }, //set for 5 x 7 character
function(){ LCD_putcommand(0x0E, next); }, //display on
function(){ LCD_putcommand(0x01, next); }, //display clear
function(){ LCD_putcommand(0x06, next); }, //entry mode set
function(){ LCD_putcommand(0x00, next); }, //clear display, cursor at home
function(){ LCD_putcommand(0x00, callback); } //clear display, cursor at home
];
next(); //Execute the first step
//Function for executing the next step
function next()
{
i++;
steps[i-1]();
}
}
//******************************************************************
//LCD_putcommand
//******************************************************************
function LCD_putcommand(cmd, callback)
{
//parse command variable into individual bits for output
//to LCD
if((cmd & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH);
else b.digitalWrite(LCD_DB7, b.LOW);
if((cmd & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH);
else b.digitalWrite(LCD_DB6, b.LOW);
if((cmd & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH);
else b.digitalWrite(LCD_DB5, b.LOW);
if((cmd & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH);
else b.digitalWrite(LCD_DB4, b.LOW);
if((cmd & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH);
else b.digitalWrite(LCD_DB3, b.LOW);
if((cmd & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH);
else b.digitalWrite(LCD_DB2, b.LOW);
if((cmd & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH);
else b.digitalWrite(LCD_DB1, b.LOW);
if((cmd & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH);
else b.digitalWrite(LCD_DB0, b.LOW);
//LCD Register Set (RS) to logic zero for command input
b.digitalWrite(LCD_RS, b.LOW);
//LCD Enable (E) pin high
b.digitalWrite(LCD_E, b.HIGH);
//End the write after 1ms
setTimeout(endWrite, 1);
function endWrite()
{
//LCD Enable (E) pin low
b.digitalWrite(LCD_E, b.LOW);
//delay 1ms before calling 'callback'
setTimeout(callback, 1);
}
}
//******************************************************************
//LCD_putchar
//******************************************************************
function LCD_putchar(chr1, callback)
{
//Convert chr1 variable to UNICODE (ASCII)
var chr = chr1.toString().charCodeAt(0);
//parse character variable into individual bits for output
//to LCD
if((chr & 0x0080)== 0x0080) b.digitalWrite(LCD_DB7, b.HIGH);
else b.digitalWrite(LCD_DB7, b.LOW);
if((chr & 0x0040)== 0x0040) b.digitalWrite(LCD_DB6, b.HIGH);
else b.digitalWrite(LCD_DB6, b.LOW);
if((chr & 0x0020)== 0x0020) b.digitalWrite(LCD_DB5, b.HIGH);
else b.digitalWrite(LCD_DB5, b.LOW);
if((chr & 0x0010)== 0x0010) b.digitalWrite(LCD_DB4, b.HIGH);
else b.digitalWrite(LCD_DB4, b.LOW);
if((chr & 0x0008)== 0x0008) b.digitalWrite(LCD_DB3, b.HIGH);
else b.digitalWrite(LCD_DB3, b.LOW);
if((chr & 0x0004)== 0x0004) b.digitalWrite(LCD_DB2, b.HIGH);
else b.digitalWrite(LCD_DB2, b.LOW);
if((chr & 0x0002)== 0x0002) b.digitalWrite(LCD_DB1, b.HIGH);
else b.digitalWrite(LCD_DB1, b.LOW);
if((chr & 0x0001)== 0x0001) b.digitalWrite(LCD_DB0, b.HIGH);
else b.digitalWrite(LCD_DB0, b.LOW);
//LCD Register Set (RS) to logic one for character input
b.digitalWrite(LCD_RS, b.HIGH);
//LCD Enable (E) pin high
b.digitalWrite(LCD_E, b.HIGH);
//End the write after 1ms
setTimeout(endWrite, 1);
function endWrite()
{
//LCD Enable (E) pin low and call scheduleCallback when done
b.digitalWrite(LCD_E, b.LOW);
//delay 1ms before calling 'callback'
setTimeout(callback, 1);
}
}
//******************************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.