標(biāo)題: LCD1602 4位數(shù)據(jù)串行方式顯示的單片機源碼 [打印本頁]

作者: 凱爾特    時間: 2018-7-30 17:24
標(biāo)題: LCD1602 4位數(shù)據(jù)串行方式顯示的單片機源碼
單片機源程序如下:
  1. /*******************************************************************

  2. * 描述:                                                           
  3. *    LCD1602可以分為8位和4位控制方式,8位控制方式是用D0-D7數(shù)據(jù)線   
  4. * 來傳送控制命令及數(shù)據(jù)。4位控制方式是用D4-D7數(shù)據(jù)線來傳送控制命令   
  5. * 及數(shù)據(jù)。使用4位數(shù)據(jù)線做控制時,需要分兩次來傳送,先送出高4位數(shù)   
  6. * 據(jù),再送出低4位數(shù)據(jù)。可以節(jié)省單片機的4根端口線。                 
  7. *                                                                  
  8. *******************************************************************/
  9. #include < reg52.h >
  10. #include < intrins.h >
  11. #define uchar unsigned char
  12. #define uint  unsigned int
  13. sbit  LCD_RS = P2^0;            
  14. sbit  LCD_RW = P2^1;
  15. sbit  LCD_EN = P2^2;
  16. uchar code  cdis1[ ] = {"   WELCOME TO   "};
  17. uchar code  cdis2[ ] = {" WWW*RICHMCU*COM "};
  18. /**********************************************************

  19. * 5us 延時子程序

  20. **********************************************************/
  21. void  delayNOP()
  22. {
  23.    _nop_();
  24.    _nop_();
  25.    _nop_();
  26.    _nop_();
  27.    _nop_();
  28. }

  29. /**********************************************************

  30. * 延時子程序

  31. **********************************************************/
  32. void delay(uint ms)

  33. {
  34.    uchar t;
  35.    while(ms--)
  36.    {
  37.      for(t = 0; t < 120; t++);
  38.    }
  39. }


  40. /**********************************************************

  41. * 檢查LCD忙狀態(tài)                                          
  42. * lcd_busy為1時,忙,等待。                              
  43. * lcd-busy為0時,閑,可寫指令與數(shù)據(jù)                        

  44. **********************************************************/
  45. bit lcd_busy()
  46. {                          
  47.     bit result;
  48.     LCD_RS = 0;
  49.     LCD_RW = 1;
  50.     LCD_EN = 1;
  51.     delayNOP();
  52.     result = (bit)(P0&0x80);
  53.     LCD_EN = 0;
  54.     return(result);
  55. }

  56. /*********************************************************

  57. * 寫指令或數(shù)據(jù)

  58. * start=0, 寫入命令;  start=1, 寫入數(shù)據(jù)

  59. *********************************************************/

  60. void lcd_write(bit start, uchar in_data)
  61. {
  62.     uchar Hdata,Ldata;

  63.     while(lcd_busy());

  64.     Hdata=in_data&0xf0;             //取高四位
  65.     Ldata=(in_data<<4)&0xf0;    //取低四位

  66.     if(start==0)
  67.     LCD_RS = 0;               //寫入命令
  68.     else
  69.     LCD_RS = 1;                 //寫入數(shù)據(jù)

  70.     LCD_RW = 0;
  71.     LCD_EN = 0;
  72.     delayNOP();

  73.     P0 = Hdata;                 //發(fā)送高四位   
  74.     LCD_EN = 1;
  75.     delayNOP();
  76.     LCD_EN = 0;
  77.     delayNOP();

  78.     P0 = Ldata;                 //發(fā)送低四位     
  79.     LCD_EN = 1;
  80.     delayNOP();
  81.     LCD_EN = 0;
  82.     delayNOP();
  83. }

  84. /*************************************************************

  85. *  LCD初始化設(shè)定                                             

  86. *************************************************************/
  87. void lcd_init()
  88. {
  89.     delay(15);
  90.     lcd_write(0,0x28);                //16*2顯示,5*7點陣,4位數(shù)據(jù)
  91.     delay(5);
  92.     lcd_write(0,0x28);
  93.     delay(5);
  94.     lcd_write(0,0x28);

  95.     delay(5);
  96.     lcd_write(0,0x0c);               //顯示開,關(guān)光標(biāo)
  97.     delay(5);
  98.     lcd_write(0,0x06);              //移動光標(biāo)
  99.     delay(5);
  100.     lcd_write(0,0x01);              //清除LCD的顯示內(nèi)容
  101.     delay(25);                          //延時
  102. }

  103. /**********************************************************

  104. *  設(shè)定顯示位置                                             

  105. **********************************************************/
  106. void lcd_pos(uchar pos)
  107. {                          
  108.    lcd_write(0,pos|0x80);     //數(shù)據(jù)指針=80+地址變量
  109. }

  110. /**********************************************************

  111. * 主函數(shù)

  112. **********************************************************/
  113. void  main()
  114. {
  115.     uchar  m;

  116.     lcd_init();                          //LCD1602初始化

  117.     lcd_pos(0x00);                 //設(shè)置顯示位置為第一行
  118.     for(m=0;m<16;m++)
  119.     lcd_write(1,cdis1[m]);


  120.      lcd_pos(0x40);                //設(shè)置顯示位置為第二行     
  121.      for(m=0;m<16;m++)
  122.      lcd_write(1,cdis2[m]);

  123.       while(1);
  124. }

  125. /*********************************************************/

復(fù)制代碼

下載:
1602LCD串行方式顯示.rar (14.21 KB, 下載次數(shù): 46)



作者: catlu    時間: 2021-9-14 11:55
謝謝樓主的源碼分享,在我對LCD1602進(jìn)行I2C通信改裝時,提供清晰有效的思路最后還是改造下來了




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1