2 Replies Latest reply on Jun 16, 2015 3:40 PM by Stan_Gifford

    How to make edison to drive LCD

    CAN_TIN

      I use eclipse to control GPIO to drive LCD, It's common 12864. This is my program

       

      #include <signal.h>

      #include <stdio.h>

      #include <stdlib.h>

      #include <unistd.h>

      #include <string.h>

      #include<math.h>

       

       

      #include "mraa.hpp"

      mraa::Gpio* CS;

      mraa::Gpio* SID;

      mraa::Gpio* SCLK;

      mraa::Gpio* RST;

       

       

      #define PAGE0       0x80

      #define PAGE1       0x90

      #define PAGE2       0x88

      #define PAGE3       0x98

      #define DispClr     0x01

      #define DispON      0x0C

      #define FuncSet     0x30

      #define ModeSet     0x06

       

       

      void delay(unsigned int t)

      {

        unsigned int i,j;

        for(i=0; i<t;  i++)

        for(j=0; j<10; j++);

      }

       

      void sendbyte(unsigned char zdata)

      {

          unsigned int i;

          CS->dir(mraa::DIR_OUT);

          SID->dir(mraa::DIR_OUT);

          SCLK->dir(mraa::DIR_OUT);

          for(i=0; i<8; i++)

          {

              if((zdata << i) & 0x80)

              {

                  SID->write(1);

              }

              else

              {

              SID->write(0);

              }

              SCLK->write(0);

              SCLK->write(1);

          }

          delay(50);       

      }

       

      void writeCmd(unsigned char cmd_dat)

      {

          sendbyte(0xF8);

          sendbyte(cmd_dat & 0xF0);

          sendbyte((cmd_dat << 4) & 0xF0);

      }

       

      void writeData(unsigned char dat_dat)

      {

          sendbyte(0xFA);

          sendbyte(dat_dat & 0xF0);

          sendbyte((dat_dat << 4) & 0xF0);

      }

       

      void lcdInit()

      {

        CS->dir(mraa::DIR_OUT);

        SID->dir(mraa::DIR_OUT);

        SCLK->dir(mraa::DIR_OUT);

        RST->dir(mraa::DIR_OUT);

          RST->write(1);

          writeCmd(FuncSet);  //Function Set

          delay(1);

          writeCmd(FuncSet);  //Function Set

          delay(1);

          writeCmd(DispON);   //Display ON

          delay(1);

          writeCmd(DispClr);  //Display Clear

          delay(50);

          writeCmd(ModeSet);  //Enter mode set

          delay(50);

      }

       

      void strDisp(unsigned char *s)

      {

         /* writeCmd(page + column);  */

          while(*s > 0)

          {

              writeData(*s);

              s++;

          }

      }

       

       

      void Test()

      {

        unsigned char a[]={"HOT-51"};

        unsigned char b[]={"************"};

        unsigned char c[]={"            "};

        unsigned char d[]={"************"};

        writeCmd(0x03);

        delay(50);

        writeCmd(0x81);

        strDisp(a);

        writeCmd(0x91);

        strDisp(b);

        delay(50);

        writeCmd(0x89);

        strDisp(c);

        delay(50);

        writeCmd(0x98);

        strDisp(d);

      }

       

      int main (int argc, char **argv)

      {

        CS = new mraa::Gpio(8);

        SID = new mraa::Gpio(9);

        SCLK = new mraa::Gpio(3);

        RST = new mraa::Gpio(7);

          while (1)

          {

          lcdInit();

          Test();

          }

          return 0;

      }

       

      but  LCD can't show me anything. It's almost drive me crazy

        • 1. Re: How to make edison to drive LCD
          CMata_Intel

          Hi CAN_TIN

           

          I just compiled your code without problems; I ran this line for it:

          root@edison:~# g++ lcd.cpp -o out -lmraa
          

          Are you getting errors while compiling the code, if you do  could you post the error you are getting?

           

          If the issue is with the LCD, which LCD are you using and how are you setting the connections?

          Which MRAA version are you using? Have you tested the LCD before?

           

          Regards;

          CMata

          • 2. Re: How to make edison to drive LCD
            Stan_Gifford

            I would be twitchy about the delay routine. Firstly not a good idea to rely on spinning in code - there are many easier ways to delay for a specific interval - and also if I was a compiler I would possibly optimise the entire routine away - leading to no delay.

             

            If your LCD relies on the delays (which as you appear to be performing a S/W SPI emulation), I would look at perhaps removing the delay and using the MRAA h/w SPI (which has it's own set of issues in particular with interbyte delays).

             

            Stan