0

I have wired a display to the raspberry pi, which uses an HD44780 controller. The wiring was verified by using this python script:

https://www.raspberrypi-spy.co.uk/2012/08/20x4-lcd-module-control-using-python/#prettyPhoto

The output is correct. I am trying right now to convert this to C++ using the wiringPi library. Here is my code so far:

#include <wiringPi.h>
#include <iostream>
#include <unistd.h>
#include <errno.h>

class display_t
{
private:
    enum mode_t
    {
        MODE_CMD,
        MODE_CHR
    };
public:
    display_t() :
        initialized_(false)
    {
        /*LCD_RS = 11;
        LCD_E = 10;
        LCD_D4 = 6;
        LCD_D5 = 5;
        LCD_D6 = 4;
        LCD_D7 = 1;*/
        LCD_RS = 7;
        LCD_E = 8;
        LCD_D4 = 25;
        LCD_D5 = 24;
        LCD_D6 = 23;
        LCD_D7 = 18;
        LCD_LINE1 = 0x80;
        LCD_LINE2 = 0xC0;
        LCD_LINE3 = 0x94;
        LCD_LINE4 = 0xD4;
        E_PULSE = 5000;
        E_DELAY = 5000;
    }

    virtual ~display_t()
    {

    }

    bool initialize()
    {
        if (wiringPiSetupGpio() == -1)
        {
            return false;
        }
        pinMode(LCD_E, OUTPUT);
        pinMode(LCD_RS, OUTPUT);
        pinMode(LCD_D4, OUTPUT);
        pinMode(LCD_D5, OUTPUT);
        pinMode(LCD_D6, OUTPUT);
        pinMode(LCD_D7, OUTPUT);

        lcd_byte(0x33, MODE_CMD);
        lcd_byte(0x32, MODE_CMD);
        lcd_byte(0x06, MODE_CMD);
        lcd_byte(0x0C, MODE_CMD);
        lcd_byte(0x28, MODE_CMD);
        lcd_byte(0x01, MODE_CMD);

        usleep(E_DELAY);
        return true;
    }

    void test()
    {
        lcd_string("Blubber", LCD_LINE1);
        lcd_string("Blibber", LCD_LINE2);
        lcd_string("Blobber", LCD_LINE3);
        lcd_string("Blabber", LCD_LINE4);
    }

private:
    int LCD_RS;
    int LCD_E;
    int LCD_D4;
    int LCD_D5;
    int LCD_D6;
    int LCD_D7;
    int LCD_WIDTH;
    char LCD_LINE1;
    char LCD_LINE2;
    char LCD_LINE3;
    char LCD_LINE4;
    int E_PULSE;
    int E_DELAY;
    bool initialized_;

    void lcd_byte(char bits, mode_t mode)
    {
        digitalWrite(LCD_RS, mode == MODE_CMD ? 0 : 1);

        digitalWrite(LCD_D4, bits & 0x10 == 0x10 ? 1 : 0);
        digitalWrite(LCD_D5, bits & 0x20 == 0x20 ? 1 : 0);
        digitalWrite(LCD_D6, bits & 0x40 == 0x40 ? 1 : 0);
        digitalWrite(LCD_D7, bits & 0x80 == 0x80 ? 1 : 0);
        toggle_enable();

        digitalWrite(LCD_D4, bits & 0x01 == 0x01 ? 1 : 0);
        digitalWrite(LCD_D5, bits & 0x02 == 0x02 ? 1 : 0);
        digitalWrite(LCD_D6, bits & 0x04 == 0x04 ? 1 : 0);
        digitalWrite(LCD_D7, bits & 0x08 == 0x08 ? 1 : 0);
        toggle_enable();
    }

    void toggle_enable()
    {
        usleep(E_DELAY);
        digitalWrite(LCD_E, 1);
        usleep(E_PULSE);
        digitalWrite(LCD_E, 0);
        usleep(E_DELAY);
    }

    void lcd_string(const std::string &message, uint8_t line)
    {
        int written = 0;
        lcd_byte(line, MODE_CMD);
        for (char c : message)
        {
            lcd_byte(c, MODE_CHR);
            if (written == 20)
            {
                return;
            }
        }
        /*for (; written < 20; ++written)
        {
            lcd_byte(' ', MODE_CHR);
        }*/
    }
};

int main()
{
    display_t display;
    if (!display.initialize())
    {
        std::cout << "Failed..." << std::endl;
        return -1;
    }
    display.test();
    return 0;
}

Its very rugh right now and I know there is many things you can improve (e.g. the constants), it was hacked very quickly.

My problem is: It is not working. I get gibberish on the display. Not even the Clear command (0x01) is working propery. I tried everything and I am not able to get any further. Are there some suggestions on what I am doing wrong?

  • E_PULSE and E_DELAY ... 5000 vs 0.0005 ... I think they need to be 500 – Jaromanda X Mar 20 at 22:05
  • to expand above, I take it time.sleep(0.0005) (in the python code in the link) means sleep 5/10000th second (500us) - but you usleep(5000) - 10 times as long – Jaromanda X Mar 20 at 22:15
  • also for (char c : message) doesn't look C++ at all – Jaromanda X Mar 20 at 22:18
  • @JaromandaX you are right, but it does not change the issue unfortunately. – Nidhoegger Mar 21 at 7:33
  • @JaromandaX this is C++11, its a foreach loop like in Java. It works on everything that implements iterators (which std::string does) – Nidhoegger Mar 21 at 7:33

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.