RuiViana

LCD_Meu

Aug 1st, 2016
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(6, 8, 9, 10, 11, 12);    //Ordem dos pinos RS EN D4 D5 D6 D7
  3.  
  4. #define  RS 6
  5. #define  RW  7
  6. #define  ENA 8
  7. #define  DB7  9
  8. #define  DB6  10
  9. #define  DB5  11
  10. #define  DB4  12
  11. //------------------------------
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.   pinMode( RS , OUTPUT);
  16.   pinMode( RW , OUTPUT);
  17.   pinMode(ENA, OUTPUT);
  18.   pinMode( DB7 , OUTPUT);
  19.   pinMode( DB6 , OUTPUT);
  20.   pinMode( DB5 , OUTPUT);
  21.   pinMode( DB4 , OUTPUT);
  22. //    lcd.begin(16, 2);  
  23.   initLCD();
  24. }
  25. //----------------------------
  26. void initLCD()
  27. {
  28.   delay(15);
  29.   digitalWrite( ENA, LOW);
  30.   digitalWrite( RS , LOW);
  31.   digitalWrite( RW , LOW);
  32.  
  33.   write4bits(3);
  34.   delayMicroseconds(4500); // wait min 4.1ms
  35.   write4bits(3);
  36.   delayMicroseconds(40); // wait min 4.1ms
  37.   write4bits(3);
  38.   delayMicroseconds(40);
  39.   write4bits(2);
  40.   delayMicroseconds(40);
  41.  
  42.   // 28
  43.   write4bits(2);
  44.   write4bits(8);
  45.   delayMicroseconds(60); // wait min 4.1ms
  46.  
  47.   // 2E
  48.   write4bits(0);
  49.   write4bits(8);
  50.   delayMicroseconds(60); // wait min 4.1ms
  51.  
  52.   // 2E
  53.   write4bits(0);
  54.   write4bits(1);
  55.    delayMicroseconds(3000); // wait min 4.1ms
  56.  
  57.   // 2E
  58.   write4bits(0);
  59.   write4bits(6);
  60.  
  61.   // clear  0C
  62.   write4bits(0);
  63.   write4bits(0x0C);
  64.  
  65.   delay(500);
  66.  
  67. }
  68. //------------------------
  69. void loop()
  70. {
  71.  //     lcd.begin(16, 2);
  72.  //    lcd.print("hello, world!");
  73.  
  74.   write4bits(3);
  75.   write4bits(1);
  76.   delay(500);
  77.    write4bits(3);
  78.   write4bits(2);
  79.   delay(500);
  80.   //  initLCD();
  81. }
  82. //-----------------------------------
  83. void write4bits(byte value)
  84. {
  85.   digitalWrite( DB7 , (value ) & 0x01);
  86.   digitalWrite( DB6 , (value >> 1) & 0x01);
  87.   digitalWrite( DB5 , (value >> 2) & 0x01);
  88.   digitalWrite( DB4 , (value >> 3) & 0x01);
  89.   pulseEnable();
  90. }
  91. //-----------------------------------
  92. void pulseEnable()
  93. {
  94.   digitalWrite(ENA, LOW);
  95.   delayMicroseconds(1);
  96.   digitalWrite(ENA, HIGH);
  97.   delayMicroseconds(1);    // enable pulse must be >450ns
  98.   digitalWrite(ENA, LOW);
  99.   delayMicroseconds(100);   // commands need > 37us to settle
  100. }
RAW Paste Data