Lcd Interfacing To 8051

Discussion in 'Embedded Systems and Microcontrollers' started by Karandeep Singh, Dec 7, 2007.

  1. Karandeep Singh

    Thread Starter New Member

    Oct 30, 2007
    6
    0
    hello everyone,
    i have interfaced LCD (16*2) ,a 2 line lcd .iam able initialize and send data to first line but somehow not able to do the same with 2nd line can anybody explain me the way two initialize 2 nd line.iam using keil compiler and making program in c .ihave also gone through the values like 0x0c in controller to initialize 2nd line as 4th bit(db3) is used to initialize two line but unable to success for passing data to second 2nd line
    secondly also tell me the way to scroll the data on lcd. as i now only can write on it
    hope to get the contructive answers soon

    Karandeep Singh
     
  2. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    1) What is the controller used in the 16x2 LCD display?
    2) Which 8051-compatible microcontroller are you using?
    3) How do you have them connected together? (post your schematic)
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Take a look at the information present here. There are even a few example of routines for various micros including one for the 8051.

    Hope this helps you out.

    hgmjr
     
  4. Gopal2008

    New Member

    Sep 5, 2008
    1
    0
    Hi Karandeep Singh
    The major lcd interfacing problems are initialization sequence.Check the datasheet and www.lcdinterfacing.info has a lot of information on lcd interfacing with 8051 microcontrollers.
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Look at the date of the thread!

    Its over 2 years old.

    The OP would have sorted out his problems long ago or else well and truly lost any interest he might have had in the subject by now.
     
  6. walid el masry

    Active Member

    Mar 31, 2009
    132
    0
    code i wrote for LCD 20x4 in c

    Code ( (Unknown Language)):
    1.  
    2. /*LCD 8 bit example
    3.   rs => Register Select [Command : Data]
    4.         0 => Command
    5.         1 => Data
    6.   rw => Read/Write [Write : Read]
    7.         0 => Write
    8.         1 => Read
    9.   en => Enable signal [High to Low]
    10. */
    11. #define LCD_data PORTC
    12. //****************************************************
    13. //    These statements doesn't apply in Mikroc
    14. /*#define LCD_rs   PORTA.B0
    15. #define LCD_rw   PORTA.B1
    16. #define LCD_en   PORTA.B2
    17. #define LCD_D7   PORTC.B7*/
    18. //****************************************************
    19. void LCD_busy() {
    20.      //delay is in case you don't want to check the buzy flag
    21.      delay_ms(1);//it is very important to return to the manual for it
    22.      //in case of chicking the buzy flag
    23.      /*PORTC.B7 = 1;           //Make D7th bit of LCD as i/p
    24.      PORTA.B2 = 1;           //Make port pin as o/p
    25.      PORTA.B0 = 0;
    26.      PORTA.B1 = 1;
    27.      while (PORTC.B7 = 1) {
    28.            PORTA.B2 = 0;
    29.            PORTA.B2 = 1;
    30.      }*/
    31. }
    32. void LCD_write_command () {
    33.      PORTA.F0   = 0;        //Selected command register
    34.      PORTA.F1   = 0;        //We are writing in command register
    35.      PORTA.F2   = 1;        //Enable H->L
    36.      PORTA.F2   = 0;
    37.      LCD_busy();          //Wait for LCD to process the command
    38. }
    39. void LCD_clear_display () {
    40.      LCD_data = 0x01;     //Clear LCD
    41.      LCD_write_command();
    42. }
    43. void LCD_return_homee () {
    44.      LCD_data = 0x02;     //Return LCD to Home
    45.      LCD_write_command();
    46. }
    47. void LCD_entry_mode_set (unsigned int ID,unsigned int S) {
    48.      /*
    49.      ID => [Decrement : Increment]
    50.            0 => Decrement
    51.            1 => Increment
    52.      S  => accompanies display shift
    53.            0 => OFF
    54.            1 => ON
    55.      */
    56.      LCD_data = 0x00;
    57.      ID = ID << 1;
    58.      LCD_data = LCD_data + 0x04 + ID + S;
    59.      LCD_write_command();
    60. }
    61. void LCD_display_control (unsigned int D,unsigned int C,unsigned int B) {
    62.      /*
    63.      D  => Display ON/OFF
    64.            0 => OFF
    65.            1 => ON
    66.      C  => Cursor ON/OFF
    67.            0 => OFF
    68.            1 => ON
    69.      B  => Blink the character at the cursor position
    70.            0 => OFF
    71.            1 => ON
    72.      */
    73.      LCD_data = 0x00;
    74.      C = C << 1;
    75.      D = D << 2;
    76.      LCD_data = LCD_data + 0x08 + D + C + B;
    77.      LCD_write_command();
    78. }
    79. void LCD_cursor_display_shift (unsigned int SC,unsigned int RL) {
    80.      /*
    81.      Moves the cursor and shifts the display without changing the DDRAM contents
    82.      SC  => [Cursor : Display]
    83.            0 => Cursor
    84.            1 => Display
    85.      RL  => [Left : Right]
    86.            0 => Left
    87.            1 => Right
    88.      */
    89.      LCD_data = 0x00;
    90.      RL = RL << 2;
    91.      SC = SC << 3;
    92.      LCD_data = LCD_data + 0x10 + SC + RL;
    93.      LCD_write_command();
    94. }
    95. void LCD_function_set (unsigned int DL,unsigned int N,unsigned int F) {
    96.      /*
    97.      DL  => Data Width [4 bit : 8 bit]
    98.            0 => 4 bit
    99.            1 => 8 bit
    100.      N  => Number of lines in display[1 : 2]
    101.            0 => 1 Line
    102.            1 => 2 Lines
    103.      F  => Character font[5x7 : 5x10]
    104.            0 => 5x7
    105.            1 => 5x10
    106.      */
    107.      LCD_data = 0x00;
    108.      F = F << 2;
    109.      N = N << 3;
    110.      DL = DL << 4;
    111.      LCD_data = LCD_data + 0x20 + DL + N + F;
    112.      LCD_write_command();
    113. }
    114. void LCD_cgram_address (unsigned int cgadrs) {
    115.      /*
    116.      cgadrs => CGRAM Address Binary 6 bit
    117.      i didn't use it yet
    118.      */
    119.      LCD_data = 0x00;
    120.      LCD_data = cgadrs;
    121.      LCD_write_command();
    122. }
    123. void LCD_ddram_address (unsigned int ddadrs) {
    124.      /*
    125.      ddadrs => DDRAM Address Binary 7 bit
    126.                Line 1 Column 1
    127.                Binary Address : 0B10000000
    128.                HEX Address     : 0X80
    129.  
    130.                Line 1 Column 20
    131.                Binary Address : 0B10001111
    132.                HEX Address     : 0X93
    133.  
    134.                Line 2 Column 1
    135.                Binary Address : 0B11000000
    136.                HEX Address     : 0XC0
    137.  
    138.               Line 2 Column 20
    139.                Binary Address : 0B11001111
    140.                HEX Address     : 0XD3
    141.  
    142.                Line 3 Column 1
    143.                Binary Address : 0B11000000
    144.                HEX Address     : 0X94
    145.  
    146.               Line 3 Column 20
    147.                Binary Address : 0B11001111
    148.                HEX Address     : 0XA7
    149.  
    150.                Line 4 Column 1
    151.                Binary Address : 0B11000000
    152.                HEX Address     : 0XD4
    153.  
    154.               Line 4 Column 20
    155.                Binary Address : 0B11001111
    156.                HEX Address     : 0XE7
    157.                [][][][][][][][][][][][][][][][][][][][]
    158.                [][][][][][][][][][][][][][][][][][][][]
    159.                [][][][][][][][][][][][][][][][][][][][]
    160.                [][][][][][][][][][][][][][][][][][][][]
    161.                     C1     C2     C3     C4     C5     C6      C7     C8     C9    C10     C11    C12    C13    C14   C15    C16    C17    C18     C19   C20
    162.                L1 [0X80]-[0X81]-[0X82]-[0X83]-[0X84]-[0X85]-[0X86]-[0X87]-[0X88]-[0X89]-[0X8A]-[0X8B]-[0X8C]-[0X8D]-[0X8E]-[0X8F]-[0X90]-[0X91]-[0X92]-[0X93]
    163.                L2 [0XC0]-[0XC1]-[0XC2]-[0XC3]-[0XC4]-[0XC5]-[0XC6]-[0XC7]-[0XC8]-[0XC9]-[0XCA]-[0XCB]-[0XCC]-[0XCD]-[0XCE]-[0XCF]-[0XD0]-[0XD1]-[0XD2]-[0XD3]
    164.                L3 [0X94]-[0X95]-[0X96]-[0X97]-[0X98]-[0X99]-[0X9A]-[0X9B]-[0X9C]-[0X9D]-[0X9E]-[0X9F]-[0XA0]-[0XA1]-[0XA2]-[0XA3]-[0XA4]-[0XA5]-[0XA6]-[0XA7]
    165.                L4 [0XD4]-[0XD5]-[0XD6]-[0XD7]-[0XD8]-[0XD9]-[0XDA]-[0XDB]-[0XDC]-[0XDD]-[0XDE]-[0XDF]-[0XE0]-[0XE1]-[0XE2]-[0XE3]-[0XE4]-[0XE5]-[0XE6]-[0XE7]
    166.      */
    167.      LCD_data = 0x00;
    168.      LCD_data = ddadrs;
    169.      LCD_write_command();
    170. }
    171. void LCD_write_data(unsigned int output) {
    172.      LCD_data = output;     //
    173.      PORTA.F0   = 1;        //Selected data register
    174.      PORTA.F1   = 0;        //We are writing in data register
    175.      PORTA.F2   = 1;        //Enable H->L
    176.      PORTA.F2   = 0;
    177.      LCD_busy();
    178. }
    179. /*void LCD_write_string(unsigned char output) {
    180.      LCD_data = output;     //Entry mode, auto increment with no shift
    181.      PORTA.B0   = 1;        //Selected data register
    182.      PORTA.B1   = 0;        //We are writing in data register
    183.      PORTA.B2   = 1;        //Enable H->L
    184.      PORTA.B2   = 0;
    185.      LCD_busy();
    186. }*/
    187. void LCD_initialize() {
    188.      LCD_function_set (1,1,0);//Function set: 2 Line, 8-bit, 5x7 dots
    189.      LCD_display_control (1,1,1);//Display on, Curson blinking command
    190.      LCD_clear_display();
    191.      LCD_entry_mode_set (1,0);//Entry mode, auto increment with no shift
    192.      LCD_ddram_address(0X80);//Line 1 Column 1
    193. }
    194. void main() {
    195.  
    196.      ADCON1 = 0X06;
    197.      TRISA = 0X00;
    198.      TRISC = 0x00;
    199.      LCD_initialize();
    200.      LCD_ddram_address(0X80);
    201.      LCD_write_data('H');
    202.      LCD_ddram_address(0XC0);
    203.      LCD_write_data('o');
    204.      LCD_ddram_address(0X94);
    205.      LCD_write_data('l');
    206.      LCD_ddram_address(0XD4);
    207.      LCD_write_data('a');
    208. }
    209.  
    to be honest this code is working in simulation but in real world not but i think it's matter of increasing the crystal frequency and i never tested it yet


     
  7. walid el masry

    Active Member

    Mar 31, 2009
    132
    0
    and that is the equivalent in assembly for microchip mcu pic16f877

    Code ( (Unknown Language)):
    1.  
    2. ;LCD 8 bit example
    3. ;****************************************************
    4. ;    rs => Register Select [Command : Data]
    5. ;        0 => Command
    6. ;        1 => Data
    7. ;                                                       LCD_rs   PORTA.B0
    8. ;****************************************************
    9. ;    rw => Read/Write [Write : Read]
    10. ;        0 => Write
    11. ;        1 => Read
    12. ;                                                       LCD_rw   PORTA.B1
    13. ;****************************************************
    14. ;    en => Enable signal [High to Low]
    15. ;                                                       LCD_en   PORTA.B2
    16. ;****************************************************
    17. ;                                                       LCD_D7   PORTC.B7
    18. ;****************************************************
    19. ;                                                       LCD_port PORTC
    20. ;****************************************************
    21.     LIST P=16F877A
    22.     #INCLUDE<P16F877A.INC>
    23.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    24. TIME   EQU 0X20
    25. TIMES EQU 0X21
    26. LCD_data EQU 0X22
    27.     ORG 0X00
    28. ;**************************
    29. ; intialize the mcu
    30. intialize
    31. ;**************************
    32. ;initialization for delay subrutine & interrupt
    33.     MOVLW 0X90
    34.     MOVWF INTCON           ;Enable External Interrupt & Enable Global Interrupt Gate
    35.     BSF STATUS,RP0         ;goto Bank1
    36.     MOVLW 0X47
    37.     MOVWF OPTION_REG  ;Prescaler Rate = 1:256 for TMR0 & Intrrupt Start At Falling Edge
    38.     BCF STATUS,RP0         ;return to Bank0
    39. ;**************************
    40. ;initialization for ports
    41.     CLRF PORTA
    42.     BSF STATUS,RP0
    43.     MOVLW 0X06
    44.     MOVWF ADCON1
    45.     BCF STATUS,RP0
    46.     MOVLW 0X00
    47.     TRIS PORTA;PORTA is output
    48.     TRIS PORTC;PORTC is output
    49. ;**************************
    50. ;initialization for LCD
    51.     CALL DELAY
    52.     CALL LCD_initialize
    53. ;**************************
    54. START
    55.     MOVLW 0X80
    56.     CALL LCD_ddram_address
    57.     MOVLW 'H'
    58.     CALL LCD_write_data
    59.     MOVLW 0XC0
    60.     CALL LCD_ddram_address
    61.     MOVLW 'o'
    62.     CALL LCD_write_data
    63.     MOVLW 0X94
    64.     CALL LCD_ddram_address
    65.     MOVLW 'l'
    66.     CALL LCD_write_data
    67.     MOVLW 0XD4
    68.     CALL LCD_ddram_address
    69.     MOVLW 'a'
    70.     CALL LCD_write_data
    71. STOP
    72.     GOTO STOP
    73. ;**********************************************************************************
    74. LCD_busy
    75. ;BCF PORTA,0
    76. ;BSF PORTA,1
    77. ;MOVLW B'10000000'
    78. ;TRIS PORTC
    79. ;AGAIN
    80. ;BSF PORTA,2
    81. ;BCF PORTA,2
    82. ;BTFSC PORTC,7
    83. ;GOTO AGAIN;NO
    84. ;MOVLW 0X00;YES
    85. ;TRIS PORTC
    86. ;RETURN
    87.     ;MOVLW D'1'
    88.     ;MOVWF TIMES
    89.     ;CALL DELAY_TIMES
    90.     ;CALL DELAY
    91.     CALL DELAY
    92.     RETURN
    93. ;**********************************************************************************
    94. LCD_write_command
    95.     MOVF LCD_data
    96.     MOVWF PORTC
    97.     BCF PORTA,0;Selected command register
    98.     BCF PORTA,1;We are writing in data register
    99.     BSF PORTA,2;Enable H->L
    100.     BCF PORTA,2;
    101.     CALL LCD_busy;
    102.     RETURN
    103. ;**********************************************************************************
    104. LCD_clear_display
    105.     MOVLW 0X01
    106.     MOVWF LCD_data;Clear LCD
    107.     CALL LCD_write_command;
    108.     RETURN
    109. ;**********************************************************************************
    110. LCD_return_home
    111.     MOVLW 0X02
    112.     MOVWF LCD_data;Return LCD to Home
    113.     CALL LCD_write_command;
    114.     RETURN
    115. ;**********************************************************************************
    116. LCD_entry_mode_set
    117. ;     ID => [Decrement : Increment]
    118. ;           0 => Decrement
    119. ;           1 => Increment
    120. ;     S  => accompanies display shift
    121. ;           0 => OFF
    122. ;           1 => ON
    123. ;LCD_data = B'00000+1+ID+S'
    124.     MOVWF LCD_data
    125.     CALL LCD_write_command;
    126.     RETURN
    127. ;**********************************************************************************
    128. LCD_display_control
    129. ;     D  => Display ON/OFF
    130. ;           0 => OFF
    131. ;           1 => ON
    132. ;     C  => Cursor ON/OFF
    133. ;           0 => OFF
    134. ;           1 => ON
    135. ;     B  => Blink the character at the cursor position
    136. ;           0 => OFF
    137. ;           1 => ON
    138. ;LCD_data = B'0000+1+D+C+B'
    139.     MOVWF LCD_data
    140.     CALL LCD_write_command;
    141.     RETURN
    142. ;**********************************************************************************
    143. LCD_cursor_display_shift
    144. ;     Moves the cursor and shifts the display without changing the DDRAM contents
    145. ;     SC  => [Cursor : Display]
    146. ;           0 => Cursor
    147. ;           1 => Display
    148. ;     RL  => [Left : Right]
    149. ;           0 => Left
    150. ;           1 => Right
    151. ;LCD_data = B'000+1+SC+RL+0+0'
    152.     MOVWF LCD_data
    153.     CALL LCD_write_command;
    154.     RETURN
    155. ;**********************************************************************************
    156. LCD_function_set
    157. ;     DL  => Data Width [4 bit : 8 bit]
    158. ;           0 => 4 bit
    159. ;           1 => 8 bit
    160. ;     N  => Number of lines in display[1 : 2]
    161. ;           0 => 1 Line
    162. ;           1 => 2 Lines
    163. ;     F  => Character font[5x7 : 5x10]
    164. ;           0 => 5x7
    165. ;           1 => 5x10
    166. ;LCD_data = B'00+1+DL+N+F+0+0'
    167.     MOVWF LCD_data
    168.     CALL LCD_write_command;
    169.     RETURN
    170. ;**********************************************************************************
    171. LCD_cgram_address
    172. ;     cgadrs => CGRAM Address Binary 6 bit
    173. ;     i didn't use it yet
    174.     MOVWF LCD_data;
    175.     CALL LCD_write_command;
    176.     RETURN
    177. ;**********************************************************************************
    178. LCD_ddram_address
    179. ;     ddadrs => DDRAM Address Binary 7 bit
    180. ;               Line 1 Column 1
    181. ;               Binary Address : 0B10000000
    182. ;               HEX Address     : 0X80
    183. ;
    184. ;               Line 1 Column 20
    185. ;               Binary Address : 0B10001111
    186. ;               HEX Address     : 0X93
    187. ;
    188. ;               Line 2 Column 1
    189. ;               Binary Address : 0B11000000
    190. ;               HEX Address     : 0XC0
    191. ;
    192. ;              Line 2 Column 20
    193. ;               Binary Address : 0B11001111
    194. ;               HEX Address     : 0XD3
    195. ;
    196. ;               Line 3 Column 1
    197. ;               Binary Address : 0B11000000
    198. ;               HEX Address     : 0X94
    199. ;
    200. ;              Line 3 Column 20
    201. ;               Binary Address : 0B11001111
    202. ;               HEX Address     : 0XA7
    203. ;
    204. ;               Line 4 Column 1
    205. ;               Binary Address : 0B11000000
    206. ;               HEX Address     : 0XD4
    207. ;
    208. ;              Line 4 Column 20
    209. ;               Binary Address : 0B11001111
    210. ;               HEX Address     : 0XE7
    211. ;               [][][][][][][][][][][][][][][][][][][][]
    212. ;               [][][][][][][][][][][][][][][][][][][][]
    213. ;               [][][][][][][][][][][][][][][][][][][][]
    214. ;               [][][][][][][][][][][][][][][][][][][][]
    215. ;                        C1        C2       C3       C4        C5       C6        C7       C8       C9        C10     C11      C12     C13      C14     C15      C16     C17     C18     C19     C20
    216. ;               L1 [0X80]-[0X81]-[0X82]-[0X83]-[0X84]-[0X85]-[0X86]-[0X87]-[0X88]-[0X89]-[0X8A]-[0X8B]-[0X8C]-[0X8D]-[0X8E]-[0X8F]-[0X90]-[0X91]-[0X92]-[0X93]
    217. ;               L2 [0XC0]-[0XC1]-[0XC2]-[0XC3]-[0XC4]-[0XC5]-[0XC6]-[0XC7]-[0XC8]-[0XC9]-[0XCA]-[0XCB]-[0XCC]-[0XCD]-[0XCE]-[0XCF]-[0XD0]-[0XD1]-[0XD2]-[0XD3]
    218. ;               L3 [0X94]-[0X95]-[0X96]-[0X97]-[0X98]-[0X99]-[0X9A]-[0X9B]-[0X9C]-[0X9D]-[0X9E]-[0X9F]-[0XA0]-[0XA1]-[0XA2]-[0XA3]-[0XA4]-[0XA5]-[0XA6]-[0XA7]
    219. ;               L4 [0XD4]-[0XD5]-[0XD6]-[0XD7]-[0XD8]-[0XD9]-[0XDA]-[0XDB]-[0XDC]-[0XDD]-[0XDE]-[0XDF]-[0XE0]-[0XE1]-[0XE2]-[0XE3]-[0XE4]-[0XE5]-[0XE6]-[0XE7]
    220.     MOVWF LCD_data;
    221.     CALL LCD_write_command;    
    222.     RETURN
    223. ;**********************************************************************************
    224. LCD_write_data
    225.     MOVF LCD_data
    226.     MOVWF PORTC
    227.     BSF PORTA,0;Selected data register
    228.     BCF PORTA,1;We are writing in data register
    229.     BSF PORTA,2;Enable H->L
    230.     BCF PORTA,2;
    231.     CALL LCD_busy;
    232.     RETURN
    233. ;**********************************************************************************
    234. LCD_initialize
    235.     ;B'00+1+DL+N+F+0+0'
    236.     MOVLW B'00111000'
    237.     CALL LCD_function_set ;(1,1,0);//Function set: 2 Line, 8-bit, 5x7 dots
    238.     ;B'0000+1+D+C+B'
    239.     MOVLW B'00001111'
    240.     CALL LCD_display_control ;(1,1,1);//Display on, Curson blinking command
    241.     CALL LCD_clear_display;
    242.     CALL DELAY
    243.     CALL DELAY
    244.     ;B'00000+1+ID+S'
    245.     MOVLW B'00000110'
    246.     CALL LCD_entry_mode_set ;(1,0);//Entry mode, auto increment with no shift
    247.     ;Line 1
    248.     MOVLW 0X80
    249.     CALL LCD_ddram_address;(0X80);//Line 1 Column 1
    250.     RETURN
    251. ;**********************************************************************************
    252. DELAY_TIMES;delay looping subrutine depend on "TIMES" value
    253.     CALL DELAY
    254.     DECF TIMES,1
    255.     CLRF W
    256.     XORWF TIMES,W
    257.     BTFSC STATUS,Z
    258.     GOTO DELAY_TIMES;NO loop is not finished yet
    259.     RETURN;YES loop is finished
    260. ;**********************************************************************************
    261. DELAY;delay 1ms subrutine
    262.     CLRF TIME
    263.     MOVLW 0XFE
    264.     MOVWF TMR0
    265. WAIT
    266.     BTFSS INTCON,TMR0IF
    267.     GOTO WAIT;NO
    268.     BCF INTCON,TMR0IF;YES
    269.     INCF TIME
    270.     MOVLW 0X01
    271.     XORWF TIME,W
    272.     BTFSS STATUS,Z
    273.     GOTO WAIT;NO
    274.     RETURN;YES
    275. ;**********************************************************************************
    276. DELAY_SEC;delay 1 SEC subrutine
    277.     CLRF TIME
    278.     MOVLW 0XF6
    279.     MOVWF TMR0
    280. WAIT_SEC
    281.     BTFSS INTCON,TMR0IF
    282.     GOTO WAIT_SEC;NO
    283.     BCF INTCON,TMR0IF;YES
    284.     INCF TIME
    285.     MOVLW 0X10
    286.     XORWF TIME,W
    287.     BTFSS STATUS,Z
    288.     GOTO WAIT_SEC;NO
    289.     RETURN;YES
    290. END
    291.  
    note that it is in 8-bit mode
     
Loading...