Advertisement
DrAungWinHtut

lcderror.ino

May 22nd, 2024
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // C++ code
  2. //
  3. const int db4 = 13;
  4. const int db5 = 12;
  5. const int db6 = 11;
  6. const int db7 = 10;
  7. const int en = 9;
  8. const int rs = 8;
  9. void setup()
  10. {
  11.   pinMode(db4, OUTPUT);
  12.   pinMode(db5, OUTPUT);
  13.   pinMode(db6, OUTPUT);
  14.   pinMode(db7, OUTPUT);
  15.   pinMode(rs, OUTPUT);
  16.   pinMode(en, OUTPUT);
  17.  
  18.   //send4bits(0,0,0,1,0);//4 bits mode enable
  19.   //sendMachineCode(0,0,0,0,0,0,0,0,1);//clear screen
  20.   //sendMachineCode(0,0,0,0,0,0,0,1,0);//return home
  21.   //sendMachineCode(0,0,0,0,0,1,1,0,0);//display on, no cursor
  22.   //sendMachineCode(1,0,1,0,0,1,0,0,0);//clear screen
  23.   //sendMachineCode(1,0,1,0,0,1,0,0,1);//clear screen
  24.  
  25.   initlcd();
  26.  
  27.  
  28. }
  29.  
  30. void loop()
  31. {
  32.  
  33.  
  34. }
  35.  
  36. void sendMachineCode(int rrs,int b7,int b6,int b5,int b4,int b3,int b2,int b1,int b0)
  37. {
  38.    
  39.   send4bits(rrs,b0,b1,b2,b3);
  40.   delay(3000);
  41.   send4bits(rrs,b7,b6,b5,b4);
  42.   delay(3000);
  43.  
  44. }
  45.  
  46. void send4bits(int rrs,int b7,int b6,int b5,int b4)
  47. {
  48.   digitalWrite(rs,rrs);
  49.   digitalWrite(db4,b4);
  50.   digitalWrite(db5,b5);
  51.   digitalWrite(db6,b6);
  52.   digitalWrite(db7,b7);
  53.  
  54.   digitalWrite(en,LOW);
  55.   delay(10);
  56.   digitalWrite(en,HIGH);
  57.   delay(10);
  58.   digitalWrite(en,LOW);
  59. }
  60.  
  61.  
  62.  
  63. void initlcd1()
  64. {
  65.   //4 bits mode
  66.   digitalWrite(rs,0);
  67.   digitalWrite(db5,1);
  68.   digitalWrite(en,LOW);
  69.   delay(10);
  70.   digitalWrite(en,HIGH);
  71.   delay(10);
  72.   digitalWrite(en,LOW);
  73.   delay(10);
  74.  
  75.   //clear display
  76.   digitalWrite(rs,0);
  77.   digitalWrite(db4,0);
  78.   digitalWrite(db5,0);
  79.   digitalWrite(db6,0);
  80.   digitalWrite(db7,0);
  81.   digitalWrite(en,LOW);
  82.   delay(10);
  83.   digitalWrite(en,HIGH);
  84.   delay(10);
  85.   digitalWrite(en,LOW);
  86.   delay(10);  
  87.  
  88.   digitalWrite(rs,0);
  89.   digitalWrite(db4,1);
  90.   digitalWrite(en,LOW);
  91.   delay(10);
  92.   digitalWrite(en,HIGH);
  93.   delay(10);
  94.   digitalWrite(en,LOW);
  95.   delay(10);
  96.  
  97.   //return home
  98.   digitalWrite(rs,0);
  99.   digitalWrite(db4,0);
  100.   digitalWrite(db5,0);
  101.   digitalWrite(db6,0);
  102.   digitalWrite(db7,0);
  103.   digitalWrite(en,LOW);
  104.   delay(10);
  105.   digitalWrite(en,HIGH);
  106.   delay(10);
  107.   digitalWrite(en,LOW);
  108.   delay(10);  
  109.  
  110.   digitalWrite(rs,0);
  111.   digitalWrite(db5,1);
  112.   digitalWrite(en,LOW);
  113.   delay(10);
  114.   digitalWrite(en,HIGH);
  115.   delay(10);
  116.   digitalWrite(en,LOW);
  117.   delay(10);
  118.  
  119.   //display on no cursor
  120.   digitalWrite(rs,0);
  121.   digitalWrite(db4,0);
  122.   digitalWrite(db5,0);
  123.   digitalWrite(db6,0);
  124.   digitalWrite(db7,0);
  125.   digitalWrite(en,LOW);
  126.   delay(10);
  127.   digitalWrite(en,HIGH);
  128.   delay(10);
  129.   digitalWrite(en,LOW);
  130.   delay(10);  
  131.  
  132.   digitalWrite(rs,0);
  133.   digitalWrite(db6,1);
  134.   digitalWrite(db7,1);
  135.   digitalWrite(en,LOW);
  136.   delay(10);
  137.   digitalWrite(en,HIGH);
  138.   delay(10);
  139.   digitalWrite(en,LOW);
  140.   delay(10);
  141.  
  142.   //print H
  143.   digitalWrite(rs,1);
  144.   digitalWrite(db6,1);  
  145.   digitalWrite(en,LOW);
  146.   delay(10);
  147.   digitalWrite(en,HIGH);
  148.   delay(10);
  149.   digitalWrite(en,LOW);
  150.   delay(10);
  151.  
  152.   digitalWrite(rs,1);
  153.   digitalWrite(db7,1);  
  154.   digitalWrite(en,LOW);
  155.   delay(10);
  156.   digitalWrite(en,HIGH);
  157.   delay(10);
  158.   digitalWrite(en,LOW);
  159.   delay(10);
  160.  
  161.   //print I
  162.   digitalWrite(rs,1);
  163.   digitalWrite(db6,1);  
  164.   digitalWrite(en,LOW);
  165.   delay(10);
  166.   digitalWrite(en,HIGH);
  167.   delay(10);
  168.   digitalWrite(en,LOW);
  169.   delay(10);
  170.  
  171.   digitalWrite(rs,1);
  172.   digitalWrite(db4,1);  
  173.   digitalWrite(db7,1);
  174.   digitalWrite(en,LOW);
  175.   delay(10);
  176.   digitalWrite(en,HIGH);
  177.   delay(10);
  178.   digitalWrite(en,LOW);
  179.   delay(10);
  180. }
  181.  
  182.  
  183. void initlcd()
  184. {
  185.     // Initialize in 4-bit mode
  186.     send4bits(0, 0, 1, 0, 0); // Function set: 4-bit mode, 2 lines, 5x8 font
  187.     delay(5); // Wait for initialization
  188.  
  189.     // Display control: Display on, no cursor, no blinking
  190.     sendMachineCode(0, 0, 0, 0, 1, 1, 0, 0, 0);
  191.  
  192.     // Clear display
  193.     sendMachineCode(0, 0, 0, 0, 0, 0, 0, 0, 1);
  194.  
  195.     // Set entry mode: Increment cursor position, no display shift
  196.     sendMachineCode(0, 0, 0, 0, 0, 0, 1, 1, 0);
  197.  
  198.   // Set cursor position to the first row, second column (0-based)
  199. sendMachineCode(0, 0, 0, 0, 0, 1, 1, 0, 0);
  200.  
  201. // Print 'H'
  202. sendMachineCode(1, 0, 1, 0, 0, 1, 0, 0, 0);
  203.  
  204. // Set cursor position to the first row, third column
  205. sendMachineCode(0, 0, 0, 0, 0, 1, 1, 0, 1);
  206.  
  207. // Print 'I'
  208. sendMachineCode(1, 0, 1, 0, 0, 1, 0, 0, 1);
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement