Here is the display class:
using System.Threading;
using Microsoft.SPOT.Hardware;
namespace _510_Microcontroller
{
public class STU7066U
{
private OutputPort lcdRS;
private OutputPort lcdE;
private OutputPort lcdD4;
private OutputPort lcdD5;
private OutputPort lcdD6;
private OutputPort lcdD7;
private const byte DISP_ON = 0x0C; //Turn visible LCD on
private const byte CLR_DISP = 1; //Clear display
private const byte CUR_HOME = 2; //Move cursor home and clear screen memory
private const byte SET_CURSOR = 0x80; //SET_CURSOR + X : Sets cursor position to X
private void WriteNibble(byte b)
{
lcdD7.Write((b & 0x8) != 0);
lcdD6.Write((b & 0x4) != 0);
lcdD5.Write((b & 0x2) != 0);
lcdD4.Write((b & 0x1) != 0);
lcdE.Write(true); lcdE.Write(false); //Toggle the Enable Pin
Thread.Sleep(1);
}
///<summary>
/// Sends an LCD command.
/// </summary>
private void SendCommand(byte command)
{
lcdRS.Write(false); //set LCD to data mode
WriteNibble((byte)(command >> 4));
WriteNibble(command);
Thread.Sleep(2);
lcdRS.Write(true); //set LCD to data mode
}
public void initialize()
{
Thread.Sleep(100);
lcdRS = new OutputPort(GHI.Pins.G30.Gpio.PC7,true);
lcdE = new OutputPort(GHI.Pins.G30.Gpio.PC6, true);
lcdD4 = new OutputPort(GHI.Pins.G30.Gpio.PB12, false);
lcdD5 = new OutputPort(GHI.Pins.G30.Gpio.PB15, false);
lcdD6 = new OutputPort(GHI.Pins.G30.Gpio.PB13, false);
lcdD7 = new OutputPort(GHI.Pins.G30.Gpio.PB14, false);
SendCommand(0x30); //Wake up
Thread.Sleep(100);
SendCommand(0x02); //Function Set 4bit mode
SendCommand(DISP_ON);
SendCommand(CLR_DISP);
Thread.Sleep(3);
}
/// <summary>
/// Prints the passed in string to the screen at the current cursor position.
/// </summary>
/// <param name="value">The string to print.</param>
public void Print(string value)
{
for (int i = 0; i < value.Length; i++)
Print(value[i]);
}
/// <summary>
/// Prints a character to the screen at the current cursor position.
/// </summary>
/// <param name="value">The character to display.</param>
public void Print(char value)
{
WriteNibble((byte)(value >> 4));
WriteNibble((byte)value);
}
/// <summary>
/// Clears the screen.
/// </summary>
public void Clear()
{
SendCommand(CLR_DISP);
Thread.Sleep(2);
}
/// <summary>
/// Places the cursor at the top left of the screen.
/// </summary>
public void CursorHome()
{
SendCommand(CUR_HOME);
Thread.Sleep(2);
}
/// <summary>
/// Moves the cursor to given position.
/// </summary>
/// <param name="row">The new row.</param>
/// <param name="column">The new column.</param>
public virtual void SetCursorPosition(byte row, byte column)
{
byte[] row_offsets = new byte[4] { 0x00, 0x40, 0x14, 0x54 };
SendCommand((byte)(SET_CURSOR | row_offsets[row] | column));
}
}
}