Arduino LCD custom character code sample.
/*****************************************************************************
* Program for writing to Newhaven Display NHD-0216K1Z-FSW-FBW-L with ST7066U controller.
* This code is written for the Arduino Uno (ATmega328P) in 6800 Mode 8-Bit Parallel Interface
*
* Newhaven Display invests time and resources providing this open source code,
* Please support Newhaven Display by purchasing products from Newhaven Display!
*
* Copyright 2020, Newhaven Display International, Inc.
*
* This code is provided as an example only and without any warranty by Newhaven Display.
* Newhaven Display accepts no responsibility for any issues resulting from its use.
* The developer of the final application incorporating any parts of this
* sample code is responsible for ensuring its safe and correct operation
* and for any consequences resulting from its use.
* See the GNU General Public License for more details.
*****************************************************************************/
/*
UNO LCD
--------------------------
GND 1 (VSS)
5V 2 (VDD)
3 (V0)
10 RS 4 (RS)
11 RW 5 (RW)
12 E 6 (E)
2 7 (DB0)
3 8 (DB1)
4 9 (DB2)
5 10 (DB3)
6 11 (DB4)
7 12 (DB5)
8 13 (DB6)
9 14 (DB7)
5V 15 (LED+)
GROUND 16 (LED-)
*/
#define RS 10
#define RW 11
#define E 12
byte customChar[] = {
0x0A,
0x0A,
0x0A,
0x00,
0x11,
0x11,
0x0E,
0x00
};
void command(unsigned char c){ //Function that sends commands
for(int i=7;i>=0;i--){
if( (c&0x80) ==0x00){
digitalWrite(i+2,LOW);
}
else {
digitalWrite(i+2,HIGH);
}
c= c<<1;
delay(1);
}
delay(2);
digitalWrite(RS,LOW); //RS set to LOW for command
delay(1);
digitalWrite(RW,LOW); //R/W set to LOW for writing
digitalWrite(E, HIGH); //E set to HIGH for latching
delay(1);
digitalWrite(E,LOW); //E set to LOW for latching
delay(1);
}
void data(unsigned char d){ //Function that sends data
for(int i=7;i>=0;i--){
if( (d&0x80) ==0x00){
digitalWrite(i+2,LOW);
}
else {
digitalWrite(i+2,HIGH);
}
d= d<<1;
delayMicroseconds(2);
}
digitalWrite(RS,HIGH); //RS set to HIGH for data
delay(1);
digitalWrite(RW,LOW); //RW set to LOW writing
digitalWrite(E,HIGH); //E set to HIGH for latching
delayMicroseconds(1000);
digitalWrite(E,LOW); //E set to LOW for latching
delayMicroseconds(1000);
}
void Init_LCD(){
pinMode(2,OUTPUT); //Set DB0 as output
pinMode(3,OUTPUT); //Set DB1 as output
pinMode(4,OUTPUT); //Set DB2 as output
pinMode(5,OUTPUT); //Set DB3 as output
pinMode(6,OUTPUT); //Set DB4 as output
pinMode(7,OUTPUT); //Set DB5 as output
pinMode(8,OUTPUT); //Set DB6 as output
pinMode(9,OUTPUT); //Set DB7 as output
pinMode(10,OUTPUT); //Set RS as output
pinMode(11,OUTPUT); //Set RW as output
pinMode(12,OUTPUT); //Set E as output
digitalWrite(RS,LOW); //Set RS LOW
digitalWrite(RW,HIGH); //Set RS HIGH
digitalWrite(E,HIGH); //Set E HIGH
delay(10); //Delay for 10 miliseconds
digitalWrite(E, LOW); //Set E LOW
delay(100);
command(0x30); //command 0x30 = Wake up
delay(30);
command(0x30); //command 0x30 = Wake up #2
delay(10);
command(0x30); //command 0x30 = Wake up #3
delay(10);
command(0x38); //Function set: 8-bit/2-line
command(0x10); //Set cursor
command(0x0c); //Display ON; Cursor ON
command(0x06); //Entry mode set
}
void Write_Custom_Character(){
command(0x40); // Set CGRAM Address in address counter to 0x00
for(int i=0;i<8;i++){
data(customChar[i]); // Write data to CGRAM(Character Graphics Random Access Memory)
}
command(0x01); // Clear Display
data(0x00); // Write the Data to LCD stored on address 0x00
}
void setup() {
Init_LCD(); //Set port configurations. Run LCD Initialization sequence.
Write_Custom_Character();// Write custom character to LCD
}
void loop() {
}