1

My professor wants me to implement a display to an old project prototype we had laying around on the lab. But he wants me NOT to use LiquidCrystal.h library or any other to achieve that.

I read HD44780 datasheet and tried lots of tweaking and hacking stuff to finally write my name "AUGUSTO" on the screen. The problem is: "lots of tweaking and hacking stuff" which turns my code into trash and kind of impossible to implement it on other project. Eg: when I accidentally short circuit arduino, display "freezes" emtpty and does not work anymore so I have to upload a generic LiquidCrystal.h example code in order to "reset" the whole thing.

My code:

/*
 * Driving LCD Display without library
 * Augusto
 */

//PINS(RS to D7): (2,3,4,6,7,8,9,10,11,12,13);


// WRITE HITACHI (datasheet)

/*
 *     RS RW D7 D6 D5 D4 D3 D2 D1 D0
 * 2.  0  0  0  0  1  1  0  0  *  * -> Function set
 * 3.  0  0  0  0  0  0  1  1  1  0 -> Display ON/OFF control
 * 4.  0  0  0  0  0  0  0  1  1  0 -> Entry Mode set (INCREMENT)
 * 5.  1  0  0  1  0  0  1  0  0  0 -> H
 * 6.  1  0  0  1  0  0  1  0  0  1 -> I
 * 7.  ...
 * 8.  1  0  0  1  0  0  1  0  0  1 -> I (HITACHI) 
 * 9.  0  0  0  0  0  0  0  1  1  1 -> Entry Mode set (INCREMENT) - Display Shift
 * 10. 1  0  0  0  1  0  0  0  0  0 -> Escreve um espaco
 * 
 */


/*  0000 0000 01 LIMPA
    0000 0000 1— RETORNA HOME
    0000 0 0 0 1 I/D S ENTRY MODE SET
    000 0 0 0 1 DCB SETS ENTIRE DISPLAY ON/OFF
*/
 
/* 
 *  Instruction RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description fOSC is 270 kHz)
                1  0                                   Write data Writes data into DDRAM or

                1  1                                   Read data Reads data from DDRAM or

    I/D = 1: Increment
    I/D = 0: Decrement
    S = 1: Accompanies display shift
    S/C = 1: Display shift
    S/C = 0: Cursor move
    R/L = 1: Shift to the right
    R/L = 0: Shift to the left
    DL = 1: 8 bits, DL = 0: 4 bits
    N = 1: 2 lines, N = 0: 1 line
    F = 1: 5 × 10 dots, F = 0: 5 × 8 dots
    BF = 1: Internally operating
    BF = 0: Instructions acceptable

*/


#define RS 2
#define RW 3
#define E 4
#define DB0 6
#define DB1 7
#define DB2 8
#define DB3 9
#define DB4 10
#define DB5 11
#define DB6 12
#define DB7 13


void setup() {

    for (int i = 2; i <= 4; i++) {
        pinMode(i, OUTPUT);
    }

    for (int i = 6; i <= 13; i++) {
        pinMode(i, OUTPUT);
    }

    
    
    digitalWrite(E, 0);
    returnHome();
    delay(100);
    limpa();
    delay(100);
}

void loop() {
    
    functionSet();
    displayOnOff();
    entryModeSet(HIGH, LOW);
    //A
    escreve(1, 0, 0, 1, 0, 0, 0, 0, 0, 1); //Writes A
    //piscaE();
    //U
    escreve(1, 0, 0, 1, 0, 1, 0, 1, 0, 1); //Writes U and so on...
    //G
    escreve(1, 0, 0, 1, 0, 0, 0, 1, 1, 1);
    //U
    escreve(1, 0, 0, 1, 0, 1, 0, 1, 0, 1);
    //S
    escreve(1, 0, 0, 1, 0, 1, 0, 0, 1, 1);
    //T
    escreve(1, 0, 0, 1, 0, 1, 0, 1, 0, 0);
    //O
    escreve(1, 0, 0, 1, 0, 0, 1, 1, 1, 1);
    piscaE();   

    entryModeSet(HIGH, HIGH);

    //escreveEspaco();
    
    while(1) {} //infinite loop to "stop" void loop()

}


void escreve(int rs, int rw, int db7, int db6, int db5, int db4, int db3, int db2, int db1, int db0) { // Function to WRITE something on the display
    
    piscaE();
    digitalWrite(RS, rs);
    digitalWrite(RW, rw);
    digitalWrite(DB7, db7);
    digitalWrite(DB6, db6);
    digitalWrite(DB5, db5);
    digitalWrite(DB4, db4);
    digitalWrite(DB3, db3);
    digitalWrite(DB2, db2);
    digitalWrite(DB1, db1);
    digitalWrite(DB0, db0);
    //piscaE();
    delay(10);

}


void escreve(int db7, int db6, int db5, int db4, int db3, int db2, int db1, int db0) {
    digitalWrite(RS, HIGH);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, db7);
    digitalWrite(DB6, db6);
    digitalWrite(DB5, db5);
    digitalWrite(DB4, db4);
    digitalWrite(DB3, db3);
    digitalWrite(DB2, db2);
    digitalWrite(DB1, db1);
    digitalWrite(DB0, db0);
}


void piscaE() { //Function to blink ENABLE pin

    // NOT shure how enable pin works
    digitalWrite(E, 1);
    delay(11);
    digitalWrite(E, 0);
}


void limpa() { //Function to clear display
    piscaE();
    digitalWrite(RS, LOW);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, LOW);
    digitalWrite(DB6, LOW);
    digitalWrite(DB5, LOW);
    digitalWrite(DB4, LOW);
    digitalWrite(DB3, LOW);
    digitalWrite(DB2, LOW);
    digitalWrite(DB1, LOW);
    digitalWrite(DB0, HIGH);
    returnHome();
}


void entryModeSet(int id, int s) {

    //0 0 0 0 0 0 0 1 I/D S   -   ENTRY MODE SET
    
    digitalWrite(RS, LOW);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, LOW);
    digitalWrite(DB6, LOW);
    digitalWrite(DB5, LOW);
    digitalWrite(DB4, LOW);
    digitalWrite(DB3, LOW);
    digitalWrite(DB2, HIGH);
    digitalWrite(DB1, id);
    digitalWrite(DB0, s);
}


void functionSet() {
    // 0  0  0  0  1  1  0  0  *  * -> Function set

    digitalWrite(RS, LOW);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, LOW);
    digitalWrite(DB6, LOW);
    digitalWrite(DB5, HIGH);
    digitalWrite(DB4, HIGH);
    digitalWrite(DB3, LOW);
    digitalWrite(DB2, LOW);
    
}


void displayOnOff() {
    //0  0  0  0  0  0  1  1  1  0 -> Display ON/OFF control

    digitalWrite(RS, LOW);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, LOW);
    digitalWrite(DB6, LOW);
    digitalWrite(DB5, LOW);
    digitalWrite(DB4, LOW);
    digitalWrite(DB3, HIGH);
    digitalWrite(DB2, HIGH);
    digitalWrite(DB1, HIGH);
    digitalWrite(DB0, LOW);
}


void escreveEspaco() { //Function to write a space
    //1  0  0  0  1  0  0  0  0  0 -> Writes a space
    piscaE();
    digitalWrite(RS, HIGH);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, LOW);
    digitalWrite(DB6, LOW);
    digitalWrite(DB5, HIGH);
    digitalWrite(DB4, LOW);
    digitalWrite(DB3, LOW);
    digitalWrite(DB2, LOW);
    digitalWrite(DB1, LOW);
    digitalWrite(DB0, LOW);
    piscaE();
}


void escreveEspaco(int quant) { //Function to write a specific number of spaces
    //1  0  0  0  1  0  0  0  0  0 -> Writes a space

    for (int i = 1; i <= quant; i++) { 
        piscaE();
        digitalWrite(RS, HIGH);
        digitalWrite(RW, LOW);
        digitalWrite(DB7, LOW);
        digitalWrite(DB6, LOW);
        digitalWrite(DB5, HIGH);
        digitalWrite(DB4, LOW);
        digitalWrite(DB3, LOW);
        digitalWrite(DB2, LOW);
        digitalWrite(DB1, LOW);
        digitalWrite(DB0, LOW);
        //piscaE();
    }
    piscaE();
}


void returnHome() { //Function to return cursor to adress 0 according to datasheet
    piscaE();
    digitalWrite(RS, LOW);
    digitalWrite(RW, LOW);
    digitalWrite(DB7, LOW);
    digitalWrite(DB6, LOW);
    digitalWrite(DB5, LOW);
    digitalWrite(DB4, LOW);
    digitalWrite(DB3, LOW);
    digitalWrite(DB2, LOW);
    digitalWrite(DB1, HIGH);
    digitalWrite(DB0, LOW);
    piscaE();
}

4
  • why would you habe to upload LiquidCrystal.h example code? ... LED blink code should also work, if the arduino code gets corrupted
    – jsotola
    Aug 30, 2020 at 1:58
  • you are not thinking clearly ... you use a function escreve() and you call it like escreve(1, 0, 0, 1, 0, 0, 0, 0, 0, 1);, which is the proper way to reuse code .... but then you go on to define pointless functions such as returnHome() and escreveEspaco(), which do the same thing as escreve()
    – jsotola
    Aug 30, 2020 at 2:11
  • I'm not used to work with bytes or pointers and that's why I said this code is totally messed up because of "lots of tweaking and hacking stuff" like writing a function to write something and another function to write a SPACE instead of creating a variable to then split it or so. Aug 30, 2020 at 19:31
  • start by reducing the code ... for instance, escreveEspaco() is same as escreve(1, 0, 0, 0, 1, 0, 0, 0, 0, 0); ... work with bytes is no different than work with integers... a byte is just a number ... it is like saying not used to working with numbers that are less than 256
    – jsotola
    Aug 30, 2020 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.