728x90

728x90

Wednesday, October 13, 2021

AT89C52 and Character LCD in 4-bit mode Using Keil

Character LCD is commonly found in some digital electronics controlled system. The HD44780 is an easy-to-use character LCD controller so far. Currently, there are many comparable LCD controller manufactured by various companies.

AT89C52 and Character LCD in 4-bit mode Using Keil
A 16x2 LCD I use for prototyping

Its LCD controller could be controlled using 8-bit, or 4-bit mode. Controlling this module in 4-bit mode is very common due to microprocessor data pin saving. I don't show the steps of LCD interfacing in this post due to duplication. 

I draw its circuit in simulator.

AT89C52 and Character LCD in 4-bit mode Using Keil
Circuit Diagram
Source code is written using C in Keil uVision.

  1. /*
  2. AT89C52 8051 Interface to Character LCD
  3. Progamming in C using Keil
  4. */
  5.  
  6. #include <REG52.h>
  7.  
  8. #define DPORT P2
  9.  
  10. /*
  11. RS bit 0
  12. RW to GND
  13. EN bit 1
  14. */
  15.  
  16. void lcdInit();
  17. void lcdCmd(unsigned char cmd);
  18. void lcdDat(unsigned char dat);
  19. void lcdStr(unsigned char *str);
  20. void lcdXY(unsigned char x, unsigned char y);
  21. void _delay_us(unsigned int t);
  22. void _delay_ms(unsigned int T);
  23. void lcdClear();
  24.  
  25. unsigned char temp;
  26.  
  27. int main(void)
  28. {
  29.  
  30. lcdInit();
  31.  
  32. while(1)
  33. {
  34. lcdXY(1,1);
  35. lcdStr("Hello World!");
  36. lcdXY(1,2);
  37. lcdStr("AT89C52 LCD****");
  38. lcdXY(1,3);
  39. lcdStr("Interfacing....");
  40. lcdXY(1,4);
  41. lcdStr("Using C in Keil");
  42. lcdCmd(0x0F);
  43. _delay_ms(100);
  44. lcdClear();
  45. _delay_ms(100);
  46. }
  47. }
  48.  
  49. void lcdCmd(unsigned char cmd){
  50. temp=0x02;
  51. DPORT=temp|(cmd&0xF0);
  52. _delay_us(10);
  53. temp=0x00;
  54. DPORT=temp|(cmd&0xF0);
  55. _delay_us(100);
  56.  
  57. temp=0x02;
  58. DPORT=temp|(cmd<<4);
  59. _delay_us(10);
  60. temp=0x00;
  61. DPORT=temp|(cmd<<4);
  62. }
  63.  
  64. void lcdDat(unsigned char dat){
  65. temp=0x03;
  66. DPORT=temp|(dat&0xF0);
  67. _delay_us(10);
  68. temp=0x01;
  69. DPORT=temp|(dat&0xF0);
  70. _delay_us(100);
  71.  
  72. temp=0x03;
  73. DPORT=temp|(dat<<4);
  74. _delay_us(10);
  75. temp=0x01;
  76. DPORT=temp|(dat<<4);
  77. }
  78.  
  79. void lcdInit(){
  80.  
  81. DPORT=0x00;
  82. _delay_ms(2);
  83. lcdCmd(0x33);
  84. _delay_us(100);
  85. lcdCmd(0x32);
  86. _delay_us(100);
  87. lcdCmd(0x28);
  88. _delay_us(100);
  89. lcdCmd(0x0E);
  90. _delay_us(100);
  91. lcdCmd(0x01);
  92. _delay_ms(2);
  93. lcdCmd(0x06);
  94. _delay_us(100);
  95. }
  96.  
  97. void lcdStr(unsigned char *str){
  98. unsigned char i=0;
  99. while(str[i]!=0){
  100. lcdDat(str[i]);
  101. i++;
  102. }
  103. }
  104.  
  105. void lcdXY(unsigned char x, unsigned char y){
  106. unsigned char tbe[]={0x80,0xC0,0x94,0xD4}; // 20x4
  107. lcdCmd(tbe[y-1]+x-1);
  108. _delay_us(100);
  109. }
  110.  
  111. void _delay_us(unsigned int a){
  112. unsigned int i,j;
  113. for(i=0;i<5;i++)
  114. for(j=0;j<a;j++);
  115. }
  116.  
  117. void _delay_ms(unsigned int b){
  118. unsigned int i;
  119. for(i=0;i<b;i++) _delay_us(1000);
  120. }
  121.  
  122. void lcdClear(){
  123. lcdCmd(0x01);
  124. _delay_us(10);
  125. }
  126.  

Due to ISP absence for AT89C52. I can burned firmware into this controller. Hence I just only simulate this program in simulator.


AT89C52 and Character LCD in 4-bit mode Using Keil
Program simulation

Click here to download this example.



No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90