51 MCU study notes, operation of 1602 LCD

Publisher:快乐旅途Latest update time:2017-02-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

TX-1C Board

 

The 1602 LCD in parallel mode.
The 1st and 2nd pins are gnd and vcc.
The 3rd pin is for contrast adjustment.
The 4th pin is for data/command selection RS.
The 5th pin is for read/write selection R/W. We only write but not read this time, so we connect it to ground directly.
The 6th pin is for read/write enable EN.
The 7th to 14th pins are for data port, which we connect to the P0 port of STC89C52RC.
The 15th and 16th pins are for backlight.

 

Final LCD display

Tom is playing the TNT game!

C Code  Favorite Code

  1. #include   

  2. #include "MY51.H"  

  3.   

  4. void write_data(uchar dataValue); //write data  

  5. void write_cmd(uchar cmdValue); //write command  

  6.   

  7. uchar code tableLCD1[] ={"Tom is playing"};  

  8. uchar code tableLCD2[] ={" the TNT game!"};  

  9.   

  10. void lcdInit() //initialization function  

  11. {  

  12.     dula=0; //  

  13.     wela=0; //Prevent digital tube from displaying incorrectly and wasting current  

  14.     lcdEN=low; //The state before giving high pulse  

  15.     lcdRS=high; //Initial high level, in fact, the default is also high level when powered on  

  16.     write_cmd(0x38); //LCD display mode initialization  

  17.     write_cmd(0x0c); //Turn on the LCD, do not display the cursor, and do not blink the cursor  

  18.     write_cmd(0x06); //Address pointer increments, and cursor increments   

  19.     write_cmd(0x01); //LCD data pointer and display clear  

  20. }  

  21.   

  22. void write_cmd(uchar cmdValue) //write command  

  23. {  

  24.     lcdRS=low; //When it is just powered on, it is high level, and when it is low level, it is command mode  

  25.     P0=cmdValue;  

  26.     delayms(1);  

  27.     lcdEN=high; //A high pulse reads the data  

  28.     delayms(1);  

  29.     lcdEN=low;  

  30. }  

  31.   

  32. void write_data(uchar dataValue) //Write data  

  33. {  

  34.     lcdRS=high; //High level is data mode  

  35.     P0=dataValue;  

  36.     delayms(1);  

  37.     lcdEN=high; // give a high pulse  

  38.     delayms(1);  

  39.     lcdEN=low;  

  40. }  

  41.   

  42. void lcdShow(uchar* pValue) //LCD display function  

  43. {  

  44.     while(*pValue!='\0')  

  45.     {  

  46.         write_data(*pValue);  

  47.         pValue++;  

  48.     }  

  49. }  

  50.   

  51. void main()  

  52. {  

  53.     lcdInit(); // Initialization  

  54.     lcdShow(tableLCD1); //Display the first row  

  55.     write_cmd(0x80+0x40); //Reset the data display pointer  

  56.     lcdShow(tableLCD2); //Display the second line  

  57.     while(1);  

  58. }  

 

C Code  Favorite Code

  1. #ifndef _MY51_H_  

  2. #define _MY51_H_  

  3. #include   

  4. #include   

  5.   

  6. typedef int int16;  

  7. typedef int INT16;  

  8. typedef unsigned int uint16;  

  9. typedef unsigned int UINT16;  

  10. typedef unsigned short uint ;  

  11. typedef unsigned short UINT;  

  12. typedef unsigned short word;  

  13. typedef unsigned short WORD;  

  14. typedef unsigned long uint32;  

  15. typedef unsigned long UINT32;  

  16. typedef unsigned long DWORD;  

  17. typedef unsigned long dword;  

  18. typedef signed long int32;  

  19. typedef signed long INT32;  

  20. typedef float float32;  

  21. typedef double double64;  

  22. typedef signed char int8;  

  23. typedef signed char INT8;  

  24. typedef unsigned char byte;  

  25. typedef unsigned char BYTE ; //This is how it is defined in windef.h of WINDOWS  

  26. typedef unsigned char uchar;  

  27. typedef unsigned char UCHAR;  

  28. typedef unsigned char UINT8;  

  29. typedef unsigned char uint8;  

  30. typedef unsigned char BOOL; // BOOL is defined as int in windows  

  31. typedef unsigned char bool ; //bool is a built-in type of C++  

  32.   

  33. #define TRUE 1  

  34. #define true 1  

  35. #define FALSE 0  

  36. #define false 0  

  37.   

  38. #define open 1 //open and close are used to mark the open and closed states  

  39. #define OPEN 1  

  40. #define close 0  

  41. #define CLOSE 0  

  42. #define lock 0  

  43. #define start 1  

  44. #define START 1  

  45. #define stop 0  

  46. #define STOP 0  

  47. #define keyDown 0  

  48. #define keyUp 1  

  49. #define gnd 0 // ground  

  50. #define GND 0 // Ground  

  51. #define high 1 //High level  

  52. #define low 0 //Low level  

  53. #define yes 1  

  54. #define YES 1  

  55. #define no 0  

  56. #define NO 0  

  57.   

  58. sbit dula =P2^6; //Segment select latch control control pen segment  

  59. sbit wela =P2^7; //bit select latch control control position  

  60.   

  61. #define led P1 //light bus control  

  62. sbit led0=P1^0; //8 LED lights, the cathode sends a low level to light up  

  63. sbit led1=P1^1;  

  64. sbit led2=P1^2;  

  65. sbit led3=P1^3;  

  66. sbit led4=P1^4;  

  67. sbit led5=P1^5;  

  68. sbit led6=P1^6;  

  69. sbit led7=P1^7;  

  70.   

  71. sbit keyS2=P3^4; //4 independent buttons  

  72. sbit keyS3=P3^5;  

  73. sbit keyS4=P3^6;  

  74. sbit keyS5=P3^7;  

  75. sbit lcdEN=P3^4; //LCD communication enable terminal en, high pulse is valid  

  76. sbit lcdRS=P3^5; // LCD pin 4, RS, low level is command mode, high level is data mode  

  77. //sbit lcdR/W //LCD pin 5, low level is write mode, because we only write and not read, so ground  

  78.   

  79. sbit csda=P3^2; //DAC0832 analog-to-digital conversion cs port  

  80. sbit adwr=P3^6; //ADC0804 is the same as DAC0832  

  81. sbit dawr=P3^6;  

  82. sbit adrd=P3^7; //ADC0804  

  83. sbit beep=P2^3; //Buzzer  

  84. void displaySMG(uint8 one,uint8 two,uint8 three,uint8 four,uint8 five,uint8 six,uint8 dot);   

  85. void delayms(uint16 ms);  

  86. void T0_Work();  

  87.   

  88. void delayms(uint16 ms) //soft delay function  

  89. {  

  90.     uint16 i,j;  

  91.     for(i=ms;i>0;i--)  

  92.     {  

  93.         for(j=113;j>0;j--)  

  94.         {}  

  95.     }  

  96. }  

  97. ///////////////////////////////////////////////////// ////////////////////////////  

  98. #define dark 0x11 //In the segment, 0x11 is the 17th element, 0 is low level, the digital tube is not lit  

  99. #define dotDark 0xff //When the decimal point is completely dark  

  100. uint8 code table[]= { //0~F plus decimal point and empty output digital tube code  

  101.     0x3f , 0x06 , 0x5b , 0x4f , // 0 1 2 3  

  102.     0x66 , 0x6d , 0x7d , 0x07 , // 4 5 6 7  

  103.     0x7f , 0x6f , 0x77 , 0x7c , // 8 9 A B  

  104.     0x39 , 0x5e , 0x79 , 0x71 , // C D E F  

  105.     0x80, 0x00, 0x40 //. Empty, negative sign, empty, it is 0x11, which is the 17th element  

  106.  };  

  107.   

  108. uint8 dotTable[]={ //decimal point position  

  109.     0xff , //Full dark  

  110.     0xfe, 0xfd, 0xfb, //1 2 3  

  111.     0xf7, 0xef, 0xdf //4 5 6                      

  112. };  

  113. ///////////////////////////////////////////////////// //////////////////////////////  

  114. uint8 TH0Cout=0; //initial value      

  115. uint8 TL0Cout = 0;      

  116. uint16 T0IntCout=0; //interrupt count  

  117. uint16 T0IntCountAll=0; //(N-1)/65536+1; //Total number of interrupts  

  118. bool bT0Delay=false; //Use delay function flag, not used initially  

  119. bool bT0Over=false; //One of the results of the interrupt processing function execution  

  120.   

  121. void startT0(uint32 ms) //Start the timer  

  122. {     

  123.     float32 t=ms/1000.0; //timing time  

  124.     double64 fox =11.0592*(pow(10,6)); //Crystal oscillator frequency  

  125.     uint32 N=(t*fox)/12; //Timer total value  

  126.   

  127.     TH0Cout = (65536-N%65536)/256; //Load the initial value of the timing value fraction count  

  128.     TL0Cout = (65536-N%65536)%256;  

  129.     T0IntCountAll=(N-1)/65536+1; //Total number of interrupts  

  130.     TMOD=TMOD | 0x01; //Set the working mode of timer 0 to 1  

  131.       

  132.     EA =open; //Open the general interrupt  

  133.     ET0=open; //Open the timer interrupt  

  134.   

  135.     TH0=TH0Cout; //Load the initial value into the timer  

  136.     TL0=TL0Cout;  

  137.     TR0=start; //Start the timer  

  138. }  

  139.   

  140. void delayT0(uint32 ms) //Hard delay function, it is not easy to use if I write it by myself, please give me some advice  

  141. {  

  142.     startT0(ms); //Start the timer  

  143.     bT0Delay=true; //Tell T0 timer to enable delay mode  

  144.     while(bT0Over==false); //Continue testing if the time is not up  

  145.     bT0Over=false; //Time is up, reset the flag  

  146. }  

  147.   

  148. void T0_times() interrupt 1 //T0 timer interrupt function  

  149. {  

  150.     T0IntCout++;  

  151.     if(T0IntCout==T0IntCountAll) //The total number of interrupts is reached  

  152.     {     

  153.         T0IntCout=0; //Reset the interrupt times and restart timing  

  154.         bT0Over=true; //The time is really up  

  155.         if(bT0Delay) //Is this interrupt used for delay?  

  156.         {  

  157.             TR0=stop; //If T0 is turned on by the delay function, turn off T0  

  158.             return;  

  159.         }  

  160.   

  161.         TH0=TH0Cout; //If the timing is cyclic, the initial value must be reloaded, and the timing is 1 second each time, and the initial value must be reloaded once  

  162.         TL0=TL0Cout;  

  163.         T0_Work(); //Work function  

  164.     }  

  165. }  

  166.   

  167. ///////////////////////////////////////////////////// /////////////////////////////////  

  168. void displaySMG(uint8 oneWela,uint8 twoWela,uint8 threeWela,uint8 fourWela,uint8 fiveWela,uint8 sixWela,uint8 dot)  

  169. {     

  170.     //Control the 6-bit digital tube display function, use the parameter dark for the bits that are not displayed, and keep the chip select signal of ADC0804  

  171.     uint8 csadState=0x80&P0; //Extract the highest bit, i.e. the chip select signal of ADC0804  

  172.     uint8 tempP0=((csadState==0)?0x7f:0xff); //Digital tube bit selection initial signal, cathode all set to high level  

  173.     P0=tempP0; //0x7f means the digital tube is off and the ADC0804 chip selection is valid  

  174.     wela=1; //Note: wela and dula are 1 by default when powered on  

  175.     P0=tempP0;  

  176.     wela=0;  

  177.   

  178.     P0=0; //Since the digital tube has a common cathode, the anode sends a low level, the light does not light up, to prevent the light from lighting up by mistake  

  179.     dula=1;  

  180.     P0=0;  

  181.     dula=0; //Segment selection data is cleared and locked  

  182. /////////////////////////////oneWela  

  183.     { // Eliminate overlapping images, set the cathode of the digital tube to a high level, and latch  

  184.         P0=tempP0;  

  185.         wela=1;           

  186.         P0=tempP0;  

  187.         wela=0;  

  188.     }  

  189.     P0=0; // low level is sent to the anode of the digital tube to prevent the digital tube from lighting up by mistake  

  190.     dula=1;  

  191.     P0=table[oneWela]|((0x01&dot)?0x00:0x80); //Send segment data, superimpose decimal point display  

  192.     dula=0;  

  193.       

  194.   

  195.     P0=tempP0; //Turn off all displays before sending data and keep csad signal  

  196.     wela=1;  

  197.     P0=tempP0 & 0xfe; //0111 1110 The highest bit is the AD chip select, the lower 6 bits are the digital tube bit select, low level is effective  

  198.     wela=0;  

  199.     delayms(2);  

  200.   

  201. ///////////////////////////twoWela  

  202.     { //Eliminate overlapping images  

  203.         P0=tempP0;  

  204.         wela=1;           

  205.         P0=tempP0;  

  206.         wela=0;  

  207.     }  

  208.     P0=0;  

  209.     dula=1;  

  210.     P0=table[twoWela]|((0x02&dot)?0x00:0x80);  

  211.     dula=0;  

  212.       

  213.     P0=tempP0;  

  214.     wela=1;  

  215.     P0=tempP0 & 0xfd; //0111 1101  

  216.     wela=0;  

  217.     delayms(2);  

  218.   

  219. ////////////////////////////threeWela  

  220.     { //Eliminate overlapping images  

  221.         P0=tempP0;  

  222.         wela=1;           

  223.         P0=tempP0;  

  224.         wela=0;  

  225.     }  

  226.     P0=0;  

  227.     dula=1;  

  228.     P0=table[threeWela]|((0x04&dot)?0x00:0x80);  

  229.     dula=0;  

  230.   

  231.     P0=tempP0;  

  232.     wela=1;  

  233.     P0=tempP0 & 0xfb; //0111 1011  

  234.     wela=0;  

  235.     delayms(2);  

  236.   

  237. ///////////////////////////fourWela  

  238.     { //Eliminate overlapping images  

  239.         P0=tempP0;  

  240.         wela=1;           

  241.         P0=tempP0;  

  242.         wela=0;  

  243.     }  

  244.     P0=0;  

  245.     dula=1;  

  246.     P0=table[fourWela]|((0x08&dot)?0x00:0x80);  

  247.     dula=0;  

  248.   

  249.     P0=tempP0;  

  250.     wela=1;  

  251.     P0=tempP0 & 0xf7; //0111 0111  

  252.     wela=0;  

  253.     delayms(2);  

  254.   

  255. ///////////////////////////fiveWela  

  256.     { //Eliminate overlapping images  

  257.         P0=tempP0;  

  258.         wela=1;           

  259.         P0=tempP0;  

  260.         wela=0;  

  261.     }  

  262.     P0=0;  

  263.     dula=1;  

  264.     P0=table[fiveWela]|((0x10&dot)?0x00:0x80);  

  265.     dula=0;  

  266.   

  267.     P0=tempP0;  

  268.     wela=1;  

  269.     P0=tempP0 & 0xef; //0110 1111  

  270.     wela=0;  

  271.     delayms(2);  

  272.   

  273. ///////////////////////////sixWela  

  274.     { //Eliminate overlapping images  

  275.         P0=tempP0;  

  276.         wela=1;           

  277.         P0=tempP0;  

  278.         wela=0;  

  279.     }  

  280.     P0=0;  

  281.     dula=1;  

  282.     P0=table[sixWela]|((0x20&dot)?0x00:0x80);  

  283.     dula=0;  

  284.   

  285.     P0=tempP0;  

  286.     wela=1;  

  287.     P0=tempP0 & 0xdf; //0101 1111  

  288.     wela=0;  

  289.     delayms(2);  

  290. }  

  291.   

  292. #endif  

 


Reference address:51 MCU study notes, operation of 1602 LCD

Previous article:51 MCU study notes, simulate iic bus to continuously read and write 24c02 memory
Next article:51 MCU learning notes: using ADC0804 analog-to-digital converter to collect voltage

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号