148 lines
4.3 KiB
C++
Executable File
148 lines
4.3 KiB
C++
Executable File
int DI = 12;
|
||
int RW = 11;
|
||
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};// use array to select pin for bus
|
||
int Enable = 2;
|
||
|
||
void LcdCommandWrite(int value) {
|
||
int i = 0;
|
||
// assign value for bus
|
||
for (i=DB[0]; i <= DI; i++) {
|
||
// for 1602 LCD, it uses D7-D0( not D0-D7) for signal identification;
|
||
// here, it’s used for signal inversion.
|
||
digitalWrite(i,value & 01);
|
||
value >>= 1;
|
||
}
|
||
digitalWrite(Enable,LOW);
|
||
delayMicroseconds(1);
|
||
digitalWrite(Enable,HIGH);
|
||
delayMicroseconds(1); // wait for 1ms
|
||
digitalWrite(Enable,LOW);
|
||
delayMicroseconds(1); // wait for 1ms
|
||
}
|
||
|
||
void LcdDataWrite(int value) {
|
||
// initialize all pins
|
||
int i = 0;
|
||
digitalWrite(DI, HIGH);
|
||
digitalWrite(RW, LOW);
|
||
for (i=DB[0]; i <= DB[7]; i++) {
|
||
digitalWrite(i,value & 01);
|
||
value >>= 1;
|
||
}
|
||
digitalWrite(Enable,LOW);
|
||
delayMicroseconds(1);
|
||
digitalWrite(Enable,HIGH);
|
||
delayMicroseconds(1);
|
||
digitalWrite(Enable,LOW);
|
||
delayMicroseconds(1); // wait for 1ms
|
||
}
|
||
|
||
void setup (void) {
|
||
int i = 0;
|
||
for (i=Enable; i <= DI; i++) {
|
||
pinMode(i,OUTPUT);
|
||
}
|
||
delay(100);
|
||
// initialize LCD after a brief pause
|
||
// for LCD control
|
||
LcdCommandWrite(0x38); // select as 8-bit interface, 2-line display, 5x7 character size
|
||
delay(64);
|
||
LcdCommandWrite(0x38); // select as 8-bit interface, 2-line display, 5x7 character size
|
||
delay(50);
|
||
LcdCommandWrite(0x38); // select as 8-bit interface, 2-line display, 5x7 character size
|
||
delay(20);
|
||
LcdCommandWrite(0x06); // set input mode
|
||
// auto-increment, no display of shifting
|
||
delay(20);
|
||
LcdCommandWrite(0x0E); // display setup
|
||
// turn on the monitor, cursor on, no flickering
|
||
delay(20);
|
||
LcdCommandWrite(0x01); // clear the scree, cursor position returns to 0
|
||
delay(100);
|
||
LcdCommandWrite(0x80); // display setup
|
||
// turn on the monitor, cursor on, no flickering
|
||
|
||
delay(20);
|
||
}
|
||
|
||
void loop (void) {
|
||
LcdCommandWrite(0x01); // clear the scree, cursor position returns to 0
|
||
delay(10);
|
||
LcdCommandWrite(0x80+3);
|
||
delay(10);
|
||
// write in welcome message
|
||
LcdDataWrite('F');
|
||
delay(50);
|
||
LcdDataWrite('u');
|
||
delay(50);
|
||
LcdDataWrite('z');
|
||
delay(50);
|
||
LcdDataWrite('a');
|
||
delay(50);
|
||
LcdDataWrite('n');
|
||
delay(50);
|
||
LcdDataWrite(' ');
|
||
delay(50);
|
||
LcdDataWrite('T');
|
||
delay(50);
|
||
LcdDataWrite('o');
|
||
delay(50);
|
||
LcdDataWrite('k');
|
||
delay(50);
|
||
LcdDataWrite('o');
|
||
delay(50);
|
||
LcdCommandWrite(0xc0+0); // set cursor position at second line, second position
|
||
delay(10);
|
||
LcdDataWrite('*');
|
||
LcdDataWrite('*');delay(70);
|
||
LcdDataWrite('A');delay(90);
|
||
LcdDataWrite('R');delay(110);
|
||
LcdDataWrite('D');delay(130);
|
||
LcdDataWrite('U');delay(150);
|
||
LcdDataWrite('I');delay(170);
|
||
LcdDataWrite('N');delay(200);
|
||
LcdDataWrite('O');delay(170);
|
||
LcdDataWrite('-');delay(150);
|
||
LcdDataWrite('L');delay(130);
|
||
LcdDataWrite('E');delay(110);
|
||
LcdDataWrite('A');delay(90);
|
||
LcdDataWrite('R');delay(70);
|
||
LcdDataWrite('N');delay(70);
|
||
LcdDataWrite('*');
|
||
LcdDataWrite('*');
|
||
delay(3000);
|
||
LcdCommandWrite(0x01); // clear the screen, cursor returns to 0
|
||
delay(10);
|
||
LcdDataWrite('A');
|
||
LcdDataWrite('p');
|
||
LcdDataWrite('r');
|
||
LcdDataWrite('e');
|
||
LcdDataWrite('n');
|
||
LcdDataWrite('d');
|
||
LcdDataWrite('e');
|
||
LcdDataWrite(' ');
|
||
LcdDataWrite(' ');
|
||
LcdDataWrite('c');
|
||
LcdDataWrite('r');
|
||
LcdDataWrite('e');
|
||
LcdDataWrite('a');
|
||
LcdDataWrite(' ');
|
||
LcdDataWrite(' ');
|
||
LcdCommandWrite(0xc0);
|
||
delay(2000);
|
||
// set mode as new characters replay old ones,
|
||
// where there is no new ones remain the same
|
||
LcdCommandWrite(0xc02);
|
||
delay(10);
|
||
// set cursor position at first line, sixth position
|
||
LcdCommandWrite(0xc80+6);
|
||
delay(10);
|
||
LcdDataWrite('o');
|
||
LcdDataWrite('p');
|
||
LcdDataWrite('e');
|
||
LcdDataWrite('n');
|
||
LcdDataWrite(' ');
|
||
LcdDataWrite('s');
|
||
delay(3000);
|
||
}
|