lcd problem using 8051.

Discussion in 'Embedded Systems and Microcontrollers' started by ahmed847, Jul 25, 2012.

  1. ahmed847

    Thread Starter New Member

    May 1, 2010
    1
    0
    Hi everyone,

    I am interfacing lcd (JHD 162A) using 8051(89c51). The lcd's lamp only lit up otherwise nothing is displaying on the lcd.

    here is the link to lcd datasheet:
    http://www.egochina.net.cn/eBay/Download/JHD162A.pdf


    i have checked all the connections to the lcd and they are good.

    I wrote the code on keil uvision4

    please tell me the problem!

    your help is greatly appriciated

    here is my code:


    Code ( (Unknown Language)):
    1.  
    2. #include<regx51.h>
    3. void lcdmd(unsigned char value);
    4. void lcddata(unsigned char value);
    5. void busyflag();
    6. void delay(unsigned char value);
    7.        
    8. sbit rs=P2^0;
    9. sbit rw=P2^1;
    10. sbit e=P2^2;
    11. sbit busy=P1^7;
    12. sfr ldata=0x90;
    13.  
    14. int main(){
    15.  
    16.  
    17.  
    18. P0_2=0;         //just for checking
    19.  
    20. //initilization
    21. //step 1
    22. delay(20);
    23. rs=0;
    24. rw=0;
    25. P1_7=0;
    26. P1_6=0;
    27. P1_5=1;
    28. P1_4=1;
    29.  
    30. e=1;
    31. delay(200);
    32. e=0;
    33.  
    34. //step 2
    35. delay(10);
    36. rs=0;
    37. rw=0;
    38. P1_7=0;
    39. P1_6=0;
    40. P1_5=1;
    41. P1_4=1;
    42.  
    43. e=1;
    44. delay(200);
    45. e=0;
    46.  
    47. //step 3
    48. delay(200);
    49. rs=0;
    50. rw=0;
    51. P1_7=0;
    52. P1_6=0;
    53. P1_5=1;
    54. P1_4=1;
    55.  
    56. e=1;
    57. delay(200);
    58. e=0;
    59.  
    60. //step 4
    61. delay(100);
    62. busyflag();
    63. rs=0;
    64. rw=0;
    65. P1_7=0;
    66. P1_6=0;
    67. P1_5=1;
    68. P1_4=0;
    69.  
    70. e=1;
    71. delay(200);
    72. e=0;
    73.  
    74. delay(100);
    75. rs=0;
    76. rw=0;
    77. P1_7=0;
    78. P1_6=0;
    79. P1_5=1;
    80. P1_4=0;
    81.  
    82. e=1;
    83. delay(200);
    84. e=0;
    85.  
    86.  // lines and font
    87.  delay(100);
    88. rs=0;
    89. rw=0;
    90. P1_7=1;
    91. P1_6=0;
    92. P1_5=0;
    93. P1_4=0;
    94.  
    95. e=1;
    96. delay(200);
    97. e=0;
    98.  
    99. delay(100);
    100. rs=0;
    101. rw=0;
    102. P1_7=0;
    103. P1_6=0;
    104. P1_5=0;
    105. P1_4=0;
    106.  
    107. e=1;
    108. delay(200);
    109. e=0;
    110.  
    111. delay(100);
    112. rs=0;
    113. rw=0;
    114. P1_7=1;
    115. P1_6=0;
    116. P1_5=0;
    117. P1_4=0;
    118.  
    119. e=1;
    120. delay(200);
    121. e=0;
    122.  
    123. delay(100);
    124. rs=0;
    125. rw=0;
    126. P1_7=0;
    127. P1_6=0;
    128. P1_5=0;
    129. P1_4=0;
    130.  
    131. e=1;
    132. delay(200);
    133. e=0;
    134.  
    135. delay(100);
    136. rs=0;
    137. rw=0;
    138. P1_7=0;
    139. P1_6=0;
    140. P1_5=0;
    141. P1_4=1;
    142.  
    143. e=1;
    144. delay(200);
    145. e=0;
    146.  
    147. delay(100);
    148. rs=0;
    149. rw=0;
    150. P1_7=0;
    151. P1_6=0;
    152. P1_5=0;
    153. P1_4=0;
    154.  
    155. e=1;
    156. delay(200);
    157. e=0;
    158.  
    159. // I/D  S
    160. delay(100);
    161. rs=0;
    162. rw=0;
    163. P1_7=0;
    164. P1_6=1;
    165. P1_5=1;
    166. P1_4=1;
    167.  
    168. e=1;
    169. delay(200);
    170. e=0;
    171.  
    172.  
    173. P0_3=0; //just for checking
    174.  
    175. //initilization ends
    176. lcdmd(0x0E);
    177. lcdmd(0x06);
    178. lcddata('P');
    179. lcddata('h');
    180. lcddata('a');
    181. lcddata('s');
    182. lcddata('e');
    183. lcddata('1');
    184. lcdmd(0xC0);
    185. lcddata('P');
    186. lcddata('h');
    187. lcddata('a');
    188. lcddata('s');
    189. lcddata('e');
    190. lcddata('2');
    191. while(1){
    192. if(P3_1==0){
    193. lcddata(' ');
    194. lcddata('o');
    195. lcddata('n');
    196. }
    197. else{
    198. lcddata(' ');
    199. lcddata('o');
    200. lcddata('f');
    201. lcddata('f');
    202. }
    203.  
    204. if(P3_0==0){
    205. lcdmd(0x86);
    206. lcddata(' ');
    207. lcddata('o');
    208. lcddata('n');
    209. }
    210. else{
    211. lcdmd(0x86);
    212. lcddata(' ');
    213. lcddata('o');
    214. lcddata('f');
    215. lcddata('f');
    216. }
    217. }
    218.  
    219.  
    220.  
    221.  
    222.  
    223.  
    224.  
    225.  
    226.  
    227.  
    228. return 0;
    229. }
    230.  
    231.  
    232. void lcdmd(unsigned char value){
    233. busyflag();
    234. ldata=value;
    235. rs=0;
    236. rw=0;
    237. e=1;
    238. delay(200);
    239. e=0;
    240.  
    241. return;
    242.  
    243. }
    244.  
    245.  
    246. void lcddata(unsigned char value){
    247. busyflag();
    248. ldata=value;
    249. rs=1;   //data register
    250. rw=0;   //write
    251.  
    252. e=1;
    253. delay(200);
    254. e=0;
    255.  
    256. return;
    257. }
    258.  
    259.  
    260.  
    261. void busyflag(){
    262.  
    263.              rs=0;
    264.              rw=1;
    265.              busy=1;    //input
    266.  
    267.              while(busy==1){
    268.              e=0;
    269.              delay(200);
    270.              e=1;
    271.              }
    272.  
    273.              return;
    274.              }
    275.  
    276.  
    277. void delay(unsigned char value){
    278. unsigned int i,j;
    279. for(i=0;i<value;++i)
    280. for(j=0;j<1275;++j);
    281.  
    282. }
    283.  
     
    Last edited by a moderator: Jul 25, 2012
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Wow, 281 lines of source code and not one single comment. Assuming you send the correct bits to the correct pins, delays with these things are vital, so make em painfully long until it works, then back off from there.

    The good news is as this is one of the most common applications using a standard device in a standard format I would suggest you spend a good 5 to 10 minutes using the Google page.

    Google is your friend.
     
  3. astrotom

    New Member

    Jul 14, 2012
    16
    0
    To help you you would need to supply details about about pin connections. Which are connected to which pins? And most importantly, have you connected a potentiometer of 10k ohms to pin 3, 2 and 1? And if you have, use the potentiometer to increase the voltage. The default voltage is too low and hence you wont see anything on the screen. You should then be able to see atleast a cursor o the screen. And I don't understand your need for a busy flag function. You actually never read from the lcd. Remove the function and and all calls to it. It's useless.

    On another note, the delays for latching is too long. Latching delay is the delay b/w e=1 and e=0. A delay of 1000 cpu cycles ie. 1000 loops is more than enough. And if it works, try decreasing it till a suitable value.
     
  4. shubham161

    Member

    Jul 22, 2012
    50
    3
    I think that the person who wrote the code is the most favorable person for its debugging. And in your case, it's you.

    I am lazy enough to go through all the code and I know that i'd not be able to find the error easily unless you give me a flow chart of the program.

    But something i'd like to recommend you

    Code ( (Unknown Language)):
    1.  
    2. lcddata('P'); lcddata('h'); lcddata('a'); lcddata('s'); lcddata('e'); lcddata('1');
    3.  
    instead of sending all letters independently, why don't your save them in a array and use one other function to pass them one by one. That would be a great time saver and will make your code clean.
     
Loading...