Advertisement
DrAungWinHtut

lcdOK.ino

May 29th, 2024
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // Define LCD pin connections
  4. const int rs = 8;
  5. const int en = 9;
  6. const int db4 = 13;
  7. const int db5 = 12;
  8. const int db6 = 11;
  9. const int db7 = 10;
  10.  
  11. void pulseEnable() {
  12.   digitalWrite(en, LOW);
  13.   delayMicroseconds(1);
  14.   digitalWrite(en, HIGH);
  15.   delayMicroseconds(1);    // Enable pulse must be >450ns
  16.   digitalWrite(en, LOW);
  17.   delayMicroseconds(100);   // Commands need > 37us to settle
  18. }
  19.  
  20. void sendNibble(int value) {
  21.   digitalWrite(db4, (value >> 0) & 0x01);
  22.   digitalWrite(db5, (value >> 1) & 0x01);
  23.   digitalWrite(db6, (value >> 2) & 0x01);
  24.   digitalWrite(db7, (value >> 3) & 0x01);
  25.   pulseEnable();
  26. }
  27.  
  28. void sendByte(int value, int mode) {
  29.   digitalWrite(rs, mode);
  30.   sendNibble(value >> 4);
  31.   sendNibble(value & 0x0F);
  32. }
  33. void initLCD() {
  34.   pinMode(rs, OUTPUT);
  35.   pinMode(en, OUTPUT);
  36.   pinMode(db4, OUTPUT);
  37.   pinMode(db5, OUTPUT);
  38.   pinMode(db6, OUTPUT);
  39.   pinMode(db7, OUTPUT);
  40.  
  41.   delay(50);
  42.   sendNibble(0x03);
  43.   delay(5);
  44.   sendNibble(0x03);
  45.   delayMicroseconds(150);
  46.   sendNibble(0x03);
  47.   sendNibble(0x02); // Switch to 4-bit mode
  48.  
  49.   sendByte(0x28, 0); // 4-bit mode, 2 lines, 5x8 dots
  50.   sendByte(0x0C, 0); // Display on, cursor off, blink off
  51.   sendByte(0x01, 0); // Clear display
  52.   delay(2); // Clear command requires a delay
  53.   sendByte(0x06, 0); // Increment cursor
  54. }
  55.  
  56. void setup() {
  57.   initLCD();
  58.   sendByte(0x80, 0); // Set cursor to the beginning of the first line
  59.   sendByte('G', 1);  // Send 'H'
  60.   sendByte('R', 1);
  61.   sendByte('E', 1);
  62.   sendByte('E', 1);
  63.   sendByte('N', 1);
  64.   sendByte(' ', 1);
  65.   sendByte('H', 1);
  66.   sendByte('A', 1);
  67.   sendByte('C', 1);
  68.   sendByte('K', 1);
  69.   sendByte('E', 1);
  70.   sendByte('R', 1);
  71.   sendByte('S', 1);
  72. }
  73.  
  74. void loop() {
  75.   // Nothing to do here
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement