1602 LCD Row & Col Question, 8051 LCD Demo test in C.

Discussion in 'Embedded Systems and Microcontrollers' started by Ashton, Dec 26, 2014.

  1. Ashton

    Thread Starter New Member

    Jun 10, 2013
    3
    0
    I'm trying to determine what's wrong with a simple demo C code to display two lines of text on a 1602 LCD display.

    Whatever (Col, Row) I select everything stays on Row 1 or 2. See section 'Display Text'.

    This is the ONLY project I can get to compile/build in Keil successfully without errors. Everything else I have using LCD.h has countless errors, and produces no output files. (strange characters? is STC 8051 dev board examples).

    Code below:
    Code (Text):
    1.  
    2. /********************** 8 bit    LCD1602 ***************************************
    3. //Connects
    4. //1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16
    5. //vss vcc vee rs  rw  e   d0  d1  d2  d3   d4   d5   d6   d7   led+ led-
    6. //gnd 5v  gnd p10 p11 p12 p00 p01 p02 p03  p04  p05  p06  p07  5v   gnd
    7. ******************************************************************************/
    8. #include "reg51.h"
    9. sbit LCD_RS=P1^0;
    10. sbit LCD_RW=P1^1;
    11. sbit LCD_E=P1^2;
    12. #define LCD_Data P0
    13. #define Busy    0x80
    14.  
    15. /********Êý¾Ý¶¨Òå*************************************************************/
    16. //unsigned char code user_string[] = {"Happy every day"};
    17. //unsigned char code net[] = {"[URL='http://www.taobao.com']www.taobao.com[/URL]"};
    18.  
    19. /********º¯ÊýÉùÃ÷*************************************************************/
    20. void WriteDataLCD(unsigned char WDLCD);            
    21. void WriteCommandLCD(unsigned char WCLCD,BuysC);    
    22. unsigned char ReadDataLCD(void);                    
    23. unsigned char ReadStatusLCD(void);                    
    24. void LCDInit(void);                                    
    25. void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);    
    26. void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
    27. void Delay5Ms(void);                                
    28. void Delay400Ms(void);                                
    29.  
    30. /*********** Display Text ********************************************************/
    31. void main(void)
    32. {
    33.     Delay400Ms();  
    34.     LCDInit();      
    35.     Delay5Ms();  
    36.     DisplayListChar(1, 0, "Line One");  // was (4, 0, "TaoBaoID"); (Row,Col ??)
    37.     DisplayListChar(1, 1, "Line Two");  // was (4, 1  "Line two"); Both items print on same row ????
    38.     ReadDataLCD();
    39. }
    40.  
    41. /***********дÊý¾Ý********************************************************/
    42. void WriteDataLCD(unsigned char WDLCD)
    43. {
    44.     ReadStatusLCD(); //
    45.     LCD_Data = WDLCD;
    46.     LCD_RS = 1;
    47.     LCD_RW = 0;
    48.     LCD_E = 0;         //
    49.     LCD_E = 0;         //
    50.     LCD_E = 1;
    51. }
    52.  
    53. /*********** Write LCD ********************************************************/
    54. void WriteCommandLCD(unsigned char WCLCD,BuysC)
    55. {
    56.     if (BuysC) ReadStatusLCD();
    57.     LCD_Data = WCLCD;
    58.     LCD_RS = 0;
    59.     LCD_RW = 0;
    60.     LCD_E = 0;
    61.     LCD_E = 0;
    62.     LCD_E = 1;
    63. }
    64.  
    65. /*********** Read LCD ********************************************************/
    66. unsigned char ReadDataLCD(void)
    67. {
    68.     LCD_RS = 1;
    69.     LCD_RW = 1;
    70.     LCD_E = 0;
    71.     LCD_E = 0;
    72.     LCD_E = 1;
    73.     return(LCD_Data);
    74. }
    75.  
    76. /*********** Status *******************************************************/
    77. unsigned char ReadStatusLCD(void)
    78. {
    79.     LCD_Data = 0xFF;
    80.     LCD_RS = 0;
    81.     LCD_RW = 1;
    82.     LCD_E = 0;
    83.     LCD_E = 0;
    84.     LCD_E = 1;
    85.     while (LCD_Data & Busy); //¼ì²âæÐźÅ
    86.     return(LCD_Data);
    87. }
    88.  
    89. /*********** Initialize LCD ********************************************************/
    90. void LCDInit(void)
    91. {
    92.     LCD_Data = 0;
    93.     WriteCommandLCD(0x38,0);     //
    94.     Delay5Ms();
    95.     WriteCommandLCD(0x38,0);
    96.     Delay5Ms();
    97.     WriteCommandLCD(0x38,0);
    98.     Delay5Ms();
    99.  
    100.     WriteCommandLCD(0x38,1);     //
    101.     WriteCommandLCD(0x08,1);     //¹Ø±ÕÏÔʾ
    102.     WriteCommandLCD(0x01,1);     //ÏÔʾÇåÆÁ
    103.     WriteCommandLCD(0x06,1);     //ÏÔʾ¹â±êÒƶ¯ÉèÖÃ
    104.     WriteCommandLCD(0x0C,1);     //ÏÔʾ¿ª¼°¹â±êÉèÖÃ
    105. }
    106.  
    107. /***********°´Ö¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û*******************************************/
    108. void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
    109. {
    110.     Y &= 0x1;
    111.     X &= 0xF;         //
    112.     if (Y) X |= 0x40;     //
    113.     X |= 0x80;         //
    114.     WriteCommandLCD(X, 0); //
    115.     WriteDataLCD(DData);
    116. }
    117.  
    118. /*********** Iterate through each X char of Y row *****************************************/
    119. void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
    120. {
    121.     unsigned char ListLength;
    122.  
    123.     ListLength = 0;
    124.     Y &= 0x1;
    125.     X &= 0xF;  
    126.     while (DData[ListLength]>=0x20){ //
    127.           if (X <= 0xF){         //
    128.             DisplayOneChar(X, Y, DData[ListLength]);
    129.             ListLength++;
    130.             X++;
    131.         }
    132.       }
    133. }
    134.  
    135. /***********¶ÌÑÓʱ********************************************************/
    136. void Delay5Ms(void)
    137. {
    138.     unsigned int TempCyc = 5552;
    139.     while(TempCyc--);
    140. }
    141.  
    142. /***********³¤ÑÓʱ********************************************************/
    143. void Delay400Ms(void)
    144. {
    145.     unsigned char TempCycA = 5;
    146.     unsigned int TempCycB;
    147.     while(TempCycA--){
    148.           TempCycB=7269;
    149.           while(TempCycB--);
    150.     }
    151. }
    152.  
    Any advice appreciated.
    Please try and be specific on recommended code changes.

    Moderators note: Please use code tags for pieces of code
     
    Last edited by a moderator: Dec 26, 2014
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    Toss your DisplayOneChar() routine and WriteCommandLCD() directly until you understand how the addressing works.
     
Loading...