Skip to content

Instantly share code, notes, and snippets.

@speters
Last active December 23, 2015 05:09
Show Gist options
  • Save speters/6585149 to your computer and use it in GitHub Desktop.
Save speters/6585149 to your computer and use it in GitHub Desktop.
jbroadwell tried to simulate an hd44780 lcd in http://www.microchip.com/forums/m227487.aspx This is the indented code posted therein.
/*
* hd44780 simulator
* from
* http://www.microchip.com/forums/m227487-print.aspx
* 2/2007 by jbroadwell
*/
#define FIRST_NIBBLE 0
#define SECOND_NIBBLE 1
#define DDRAM_SIZE 128
typedef struct hd44780_n
{
uint8 Epin;
uint8 RSpin;
uint8 datapin[4];
uint8 memory[DDRAM_SIZE];
uint8 lines;
uint8 width;
uint8 linestart[4];
uint8 rxstate;
uint8 E_last_state;
uint8 display_on;
uint8 data_in;
int16 cursorpos;
int8 autoincdir;
uint8 cursor_on;
uint8 cursor_blink;
uint8 interface_length;
uint8 interface_lines;
uint8 interface_characters;
} hd44780_t;
hd44780_t hd44780_display[4];
#define h hd44780_display[instance]
void init_hd44780(uint8 instance, uint8 Epin, uint8 RSpin, uint8 datapin7, uint8 datapin6, uint8 datapin5, uint8 datapin4, uint8 lines, uint8 width, uint8 linestart0, uint8 linestart1, uint8 linestart2, uint8 linestart3)
{
int i;
h.Epin = Epin;
h.RSpin = RSpin;
h.datapin[0] = datapin7;
h.datapin[1] = datapin6;
h.datapin[2] = datapin5;
h.datapin[3] = datapin4;
hd44780_display[instance].lines = lines;
h.width = width;
h.linestart[0] = linestart0;
h.linestart[1] = linestart1;
h.linestart[2] = linestart2;
h.linestart[3] = linestart3;
for (i = 0; i < DDRAM_SIZE; ++i)
{
h.memory[i] = '}';
}
h.E_last_state = LOW;
h.display_on = 0;
}
void print_hd44780(uint8 instance, uint8 top, uint8 bottom)
{
int line, i;
if (h.display_on)
{
if (top)
{
printf(" ");
for (i = 0; i < h.width; ++i)
{
printf("%1d", i % 10);
}
printf("n");
printf("+=");
for (i = 0; i < h.width; ++i)
{
printf("=");
}
printf("=+n");
printf("| ");
for (i = 0; i < h.width; ++i)
{
printf(" ");
}
printf(" |n");
}
for (line = 0; line < h.lines; ++line)
{
printf("| ");
for (i = 0; i < h.width; ++i)
{
printf("%c", h.memory[i + h.linestart[line]]);
}
printf(" |n");
}
if (bottom)
{
printf("| ");
for (i = 0; i < h.width; ++i)
{
printf(" ");
}
printf(" |n");
printf("+=");
for (i = 0; i < h.width; ++i)
{
printf("=");
}
printf("=+n");
}
}
else
{
printf("nnDISPLAY IS TURNED OFFn");
}
}
void hd44780_update(uint8 instance)
{
int i;
if (h.E_last_state == 1 && read_pin(hd44780_display[instance].Epin) == 0)
{
if (h.rxstate == FIRST_NIBBLE)
{
h.data_in = (read_pin(h.datapin[0]) > 0);
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[1]) > 0);
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[2]) > 0);
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[3]) > 0);
h.rxstate = SECOND_NIBBLE;
}
else
{
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[0]) > 0);
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[1]) > 0);
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[2]) > 0);
h.data_in <<= 1;
h.data_in += (read_pin(h.datapin[3]) > 0);
h.rxstate = FIRST_NIBBLE;
//printf ("Processing data 0x%02X, RS pin is %dn",h.data_in,read_pin(h.RSpin));
if (read_pin(h.RSpin))
{
//Data Entry
//printf ("Processing data entryn");
h.memory[h.cursorpos] = h.data_in;
h.cursorpos += h.autoincdir;
if (h.cursorpos >= DDRAM_SIZE)
{
h.cursorpos = 0;
}
if (h.cursorpos < 0)
{
h.cursorpos = DDRAM_SIZE - 1;
}
}
else
{
//command entry
if (h.data_in == 1)
{
//printf ("Processing clear displayn");
//Clear display
for (i = 0; i < 80; ++i)
{
h.memory[i] = ' ';
}
h.cursorpos = 0;
}
else if ((h.data_in & 0xFE) == 0x02)
{
//printf("Processing cursor homen");
h.cursorpos = 0;
}
else if ((h.data_in & 0xFC) == 0x04)
{
//printf ("Processing Entry mode Setn");
if (h.data_in & 0x02)
{
//printf("Shift Cursorn");
if (h.data_in & 0x01)
{
h.autoincdir = -1;
}
else
{
h.autoincdir = 1;
}
}
else
{
h.autoincdir = 0;
}
}
else if ((h.data_in & 0xF8) == 0x08)
{
//Display on/off control
if (h.data_in & 0x04)
{
h.display_on = 1;
}
else
{
h.display_on = 0;
}
if (h.data_in & 0x02)
{
h.cursor_on = 1;
}
else
{
h.cursor_on = 0;
}
if (h.data_in & 0x01)
{
h.cursor_blink = 1;
}
else
{
h.cursor_blink = 0;
}
}
else if ((h.data_in & 0xE0) == 0x02)
{
// INTERFACE control
if (h.data_in & 0x08)
{
h.interface_length = 8;
}
else
{
h.interface_length = 4;
}
if (h.data_in & 0x04)
{
h.interface_lines = 2;
}
else
{
h.interface_lines = 1;
}
if (h.data_in & 0x02)
{
h.interface_characters = 1;
}
else
{
h.interface_characters = 0;
}
}
else if (h.data_in & 0x80)
{
// Set DDRAM address
//printf ("Setting DDRAM address to 0x%02Xn", (h.data_in & 0x7F));
h.cursorpos = (h.data_in & 0x7F);
}
}
}
}
hd44780_display[instance].E_last_state = read_pin(hd44780_display[instance].Epin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment