This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Need help!!!

Hi I am new in C language... I am doing a project that requires me to program 8051 to LCD in C language...So I am trying to display "HELLO WORLD" to the LCD in C language...I am using a C8051226 and a PIC0802-A 8X2 LCD...

I have gone online to search for sources on programming LCD...I have found a few sources in C and asm...Like www.8052.com/tutlcd.phtml and http://www.keil.com/appnotes/docs/apnt_161.asp...

I have tried to program myself based on http://www.keil.com/appnotes/docs/apnt_161.asp but the LCD does not display anything instead it displays "black pixels"...
I am also having problem configuring the ports...

I will be grateful if any of you guys can tell me how do i write these LCD data bus to the 8051 ports in C language??


DB0 EQU P3.0
DB1 EQU P3.1
DB2 EQU P3.2
DB3 EQU P3.3
DB4 EQU P3.4
DB5 EQU P3.5
DB6 EQU P3.6
DB7 EQU P3.7
DATA EQU P3

  • but the LCD does not display anything instead it displays "black pixels"...
    adjust the voltage on the contrast input (see LCD datasheet).

    Erik

  • adjust the voltage on the contrast input (see LCD datasheet).

    Also ...

    If you only get black pixels on the characters of the top one line, it can be an indication that the display has not been correctly initialised.

    If that is the case, check the code.

  • Below is the code that i tried to program based on http://www.keil.com/appnotes/docs/apnt_161.asp

    I do not know whether the way i initialise the Input/Output Ports correctly??

    
    #include "C8051F200.h"
    #include <stdio.h>
    #include <intrins.h>
    
    void Reset_Sources_Init()
    {
        WDTCN     = 0xDE;
        WDTCN     = 0xAD;
    }
    
    void Port_IO_Init()
    {
        // P0.0  -  Unassigned,  Open-Drain  Digital
        // P0.1  -  Unassigned,  Open-Drain  Digital
        // P0.2  -  Unassigned,  Open-Drain  Digital
        // P0.3  -  Unassigned,  Open-Drain  Digital
        // P0.4  -  Unassigned,  Open-Drain  Digital
        // P0.5  -  Unassigned,  Open-Drain  Digital
        // P0.6  -  Unassigned,  Open-Drain  Digital
        // P0.7  -  Unassigned,  Open-Drain  Digital
    
        // P1.0  -  Unassigned,  Open-Drain  Digital
        // P1.1  -  Unassigned,  Open-Drain  Digital
        // P1.2  -  Unassigned,  Open-Drain  Digital
        // P1.3  -  Unassigned,  Open-Drain  Digital
        // P1.4  -  Unassigned,  Open-Drain  Digital
        // P1.5  -  Unassigned,  Open-Drain  Digital
        // P1.6  -  Unassigned,  Open-Drain  Digital
        // P1.7  -  Unassigned,  Open-Drain  Digital
    
        // P2.0  -  Unassigned,  Push-Pull   Digital
        // P2.1  -  Unassigned,  Push-Pull   Digital
        // P2.2  -  Unassigned,  Push-Pull   Digital
        // P2.3  -  Unassigned,  Push-Pull   Digital
        // P2.4  -  Unassigned,  Push-Pull   Digital
        // P2.5  -  Unassigned,  Push-Pull   Digital
        // P2.6  -  Unassigned,  Push-Pull   Digital
        // P2.7  -  Unassigned,  Push-Pull   Digital
    
        PRT2CF    = 0xFF;
        PRT3CF    = 0xFF;
    }
    
    // Initialization function for device,
    // Call Init_Device() from your main program
    void Init_Device(void)
    {
        Reset_Sources_Init();
        Port_IO_Init();
    }
    
    sbit RS = P2^5; // RS for P2.5
    sbit RW = P2^6; // RW for P2.6
    sbit E = P2^7; // E for P2.7
    
    #define LINE1_ADDR 0x00 // Start of line 1 in the DD-Ram
    #define LINE2_ADDR 0x40 // Start of line 2 in the DD-Ram
    
    
    /* Display-Commands */
    
    #define CLEAR_DISPLAY 0x01 // Clear entire display and set Display Data Address to 0
    #define DD_RAM_PTR 0x80 // Address Display Data RAM pointer
    #define DISP_INIT 0x38 // 8 bit 2 lines
    #define INC_MODE 0x06 // Display Data RAM pointer incremented after write
    #define LCD_ON 0x0C //Turn on LCD
    
    /*
    * ReadInstrReg: Read from Instruction Register of LCD Display Device
    */
    
    static unsigned char ReadInstrReg (void)
    {
            unsigned char Instr;
            P3 = 0xff; // DATA PORT is input
            RS = 0; // select instruction register
            RW = 1; // read operation
            E = 1; // give operation start signal
            _nop_ (); _nop_ (); // wait
            Instr = P0; // read Instruction
            E = 0;
            return(Instr);
    }
    
    
    /*
    * WriteInstrReg: Write to Instruction Register of LCD Display Device
    */
    
    static void WriteInstrReg (unsigned char Instr)
    {
            RS = 0; // select instruction register
            RW = 0; // write operation
            E = 1; // give operation start signal
            _nop_ (); _nop_ (); // wait
            P3 = Instr; // write instruction
            E = 0;
            P3 = 0xff; // DATA_PORT is input [prevent I/O-Port from damage]
    }
    
    
    /*
    * ReadDataReg: Read from Data Register of LCD Display Device
    */
    
    static unsigned char ReadDataReg (void)
    {
            unsigned char val;
            P3 = 0xff; // DATA_PORT is input
            RS = 1; // select data register
            RW = 1; // read-operation
            E = 1; // give operation start signal
            _nop_ (); _nop_ (); // wait
            val = P0; // read Instruction
            E = 0;
            return (val);
    }
    
    
    /*
     * WriteDataReg: Write to Data Register of LCD Display Device
     */
    
    static void WriteDataReg (unsigned char val)
    {
            RS = 1;          // select data register
            RW = 0;          // write operation
            E = 1;          // give operation start signal
            _nop_ (); _nop_ ();   // wait
            P3 = val;             // write value
            E = 0;
            P3 = 0xff;            // DATA_PORT is input [prevent I/O-Port from demage]
    }
    
    char putchar (char c)
    {
              unsigned char line;
              if (c == '\n')
              {
                line = ReadInstrReg ();
                if (line & LINE2_ADDR)
                    {       // is already in line 2
                  WriteInstrReg (LINE1_ADDR | DD_RAM_PTR);   // place to start of line 1
                    }
                else
                    {
                  WriteInstrReg (LINE2_ADDR | DD_RAM_PTR);   // place to start of line 2
                }
              }
              else
              {
                    WriteDataReg (c);
              }
              return (c);
    }
    
    void wait(unsigned int time)
    {
              int i;
                for (i = 0; i < time ;i++)
                    {
                    }
    }
    
    /*
     * Clear_Display: Write 0x20 to Display RAM
     */
    static void Clear_Display (void)
    {
              WriteInstrReg (CLEAR_DISPLAY);
              wait (1);
    }
    
    void main (void)
    {
      unsigned char c;
    
      for (c = 'A'; c < 'H'; c++)
      {
        putchar (c);
      }
      printf ("Hello \n");
    
      Clear_Display ();
      for (c = 0; c < 10; c++)
      {
        printf ("This is Display Line %bd\n", c);
      }
      while (1);
    }
    
    /*void main (void)
    {
            unsigned char c;
            for (c = 'A'; c < 'H'; c++)
            { // Put the characters A to H to the display
            putchar (c);
            }
            printf ("Hello \n"); // Puts ‘Hello’ to the display using printf()
            Clear_Display (); //Clear the display and reset cursor
            for (c = 0; c < 10; c++)
            {
            printf ("This is Display Line %bd\n", c); // another printf() testing
            }
            //while (1); //never stop
    */