/* Program 3-1 Flash "HELLO" on LCD with Arduino board * * The LCD controller is connected parallel in 4-bit data mode. * The connections are below. If the Arduino Nano is plugged in * the Edubase V2 board, the connections can be made by installing * jumper wires between the Energia pins as in the third column. * * Arduino Function Energia Pin connection * pin * Pin 12 Register select J2-14 - J2-18 * Pin 11 Enable J2-15 - J4-35 * Pin 5 data 4 J4-40 - J2-11 * Pin 4 data 5 J4-36 - J2-12 * Pin 3 data 6 J1-4 - J2-13 * Pin 2 data 7 J1-3 - J1-8 * */ const int REG_SEL = 12; /* R/S pin */ const int ENABLE = 11; /* EN pin */ const int D4 = 5; const int D5 = 4; const int D6 = 3; const int D7 = 2; const int dataBus[] = {D4, D5, D6, D7}; const int RS = 1; /* R/S bit mask */ void setup() { LCD_init(); } void loop() { /* Write "HELLO" on LCD */ LCD_data('H'); LCD_data('E'); LCD_data('L'); LCD_data('L'); LCD_data('O'); delay(1000); /* clear LCD display */ LCD_command(1); delay(1000); } void LCD_init(void) { pinMode (REG_SEL, OUTPUT); pinMode (ENABLE, OUTPUT); digitalWrite(ENABLE, LOW); /* Enable idles low */ pinMode (D4, OUTPUT); pinMode (D5, OUTPUT); pinMode (D6, OUTPUT); pinMode (D7, OUTPUT); /* LCD controller reset sequence */ delay(20); LCD_nibble_write(0x30, 0); delay(5); LCD_nibble_write(0x30, 0); delay(1); LCD_nibble_write(0x30, 0); delay(1); LCD_nibble_write(0x20, 0); /* use 4-bit data mode */ delay(1); LCD_command(0x28); /* set 4-bit data, 2-line, 5x7 font */ LCD_command(0x06); /* move cursor right */ LCD_command(0x01); /* clear screen, move cursor to home */ LCD_command(0x0F); /* turn on display, cursor blinking */ } /* write 4 bits to LCD controller */ void LCD_nibble_write(char data, unsigned char select) { digitalWrite(REG_SEL, select ? HIGH : LOW); digitalGroupWrite(dataBus, 4, data >> 4); digitalWrite(ENABLE, HIGH); delayMicroseconds(1); digitalWrite(ENABLE, LOW); } /* write a command to LCD controller */ void LCD_command(unsigned char command) { LCD_nibble_write(command & 0xF0, 0); /* upper nibble first */ LCD_nibble_write(command << 4, 0); /* then lower nibble */ if (command < 4) delay(2); /* command 1 and 2 needs up to 1.64ms */ else delay(1); /* all others 40 us */ } /* write a byte of data to LCD controller */ /* since the connection is 4-bit, it takes two writes for a byte */ void LCD_data(char data) { LCD_nibble_write(data & 0xF0, RS); /* upper nibble first */ LCD_nibble_write(data << 4, RS); /* then lower nibble */ delay(1); } /* write to a group of output pins */ void digitalGroupWrite(const int* group, int length, int data) { for (int i = 0; i < length; i++) digitalWrite(group[i], data & (1 << i) ? HIGH : LOW); }