Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ code
- //
- const int db4 = 13;
- const int db5 = 12;
- const int db6 = 11;
- const int db7 = 10;
- const int en = 9;
- const int rs = 8;
- void setup()
- {
- pinMode(db4, OUTPUT);
- pinMode(db5, OUTPUT);
- pinMode(db6, OUTPUT);
- pinMode(db7, OUTPUT);
- pinMode(rs, OUTPUT);
- pinMode(en, OUTPUT);
- //send4bits(0,0,0,1,0);//4 bits mode enable
- //sendMachineCode(0,0,0,0,0,0,0,0,1);//clear screen
- //sendMachineCode(0,0,0,0,0,0,0,1,0);//return home
- //sendMachineCode(0,0,0,0,0,1,1,0,0);//display on, no cursor
- //sendMachineCode(1,0,1,0,0,1,0,0,0);//clear screen
- //sendMachineCode(1,0,1,0,0,1,0,0,1);//clear screen
- initlcd();
- }
- void loop()
- {
- }
- void sendMachineCode(int rrs,int b7,int b6,int b5,int b4,int b3,int b2,int b1,int b0)
- {
- send4bits(rrs,b0,b1,b2,b3);
- delay(3000);
- send4bits(rrs,b7,b6,b5,b4);
- delay(3000);
- }
- void send4bits(int rrs,int b7,int b6,int b5,int b4)
- {
- digitalWrite(rs,rrs);
- digitalWrite(db4,b4);
- digitalWrite(db5,b5);
- digitalWrite(db6,b6);
- digitalWrite(db7,b7);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- }
- void initlcd1()
- {
- //4 bits mode
- digitalWrite(rs,0);
- digitalWrite(db5,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- //clear display
- digitalWrite(rs,0);
- digitalWrite(db4,0);
- digitalWrite(db5,0);
- digitalWrite(db6,0);
- digitalWrite(db7,0);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(rs,0);
- digitalWrite(db4,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- //return home
- digitalWrite(rs,0);
- digitalWrite(db4,0);
- digitalWrite(db5,0);
- digitalWrite(db6,0);
- digitalWrite(db7,0);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(rs,0);
- digitalWrite(db5,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- //display on no cursor
- digitalWrite(rs,0);
- digitalWrite(db4,0);
- digitalWrite(db5,0);
- digitalWrite(db6,0);
- digitalWrite(db7,0);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(rs,0);
- digitalWrite(db6,1);
- digitalWrite(db7,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- //print H
- digitalWrite(rs,1);
- digitalWrite(db6,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(rs,1);
- digitalWrite(db7,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- //print I
- digitalWrite(rs,1);
- digitalWrite(db6,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(rs,1);
- digitalWrite(db4,1);
- digitalWrite(db7,1);
- digitalWrite(en,LOW);
- delay(10);
- digitalWrite(en,HIGH);
- delay(10);
- digitalWrite(en,LOW);
- delay(10);
- }
- void initlcd()
- {
- // Initialize in 4-bit mode
- send4bits(0, 0, 1, 0, 0); // Function set: 4-bit mode, 2 lines, 5x8 font
- delay(5); // Wait for initialization
- // Display control: Display on, no cursor, no blinking
- sendMachineCode(0, 0, 0, 0, 1, 1, 0, 0, 0);
- // Clear display
- sendMachineCode(0, 0, 0, 0, 0, 0, 0, 0, 1);
- // Set entry mode: Increment cursor position, no display shift
- sendMachineCode(0, 0, 0, 0, 0, 0, 1, 1, 0);
- // Set cursor position to the first row, second column (0-based)
- sendMachineCode(0, 0, 0, 0, 0, 1, 1, 0, 0);
- // Print 'H'
- sendMachineCode(1, 0, 1, 0, 0, 1, 0, 0, 0);
- // Set cursor position to the first row, third column
- sendMachineCode(0, 0, 0, 0, 0, 1, 1, 0, 1);
- // Print 'I'
- sendMachineCode(1, 0, 1, 0, 0, 1, 0, 0, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement