標(biāo)題: 51單片機(jī)LCD1602液晶顯示器(LM016L)顯示功能實(shí)現(xiàn)+Proteus仿真+常見(jiàn)問(wèn)題原因 [打印本頁(yè)]

作者: Rremake    時(shí)間: 2022-5-9 21:14
標(biāo)題: 51單片機(jī)LCD1602液晶顯示器(LM016L)顯示功能實(shí)現(xiàn)+Proteus仿真+常見(jiàn)問(wèn)題原因
常見(jiàn)問(wèn)題:
液晶顯示器只有背光無(wú)法顯示字符
將判忙函數(shù)替換成延時(shí)函數(shù),延時(shí)5ms左右即可
proteus仿真圖



代碼:
  1. #include<reg51.h>
  2. #include<intrins.h>  //包含_nop_()函數(shù)定義的頭文件

  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. sbit RS=P2^0;
  6. sbit RW=P2^1;
  7. sbit E=P2^2;
  8. sbit BF=P1^7;

  9. uchar code dis1[]={"hellow world"};
  10. uchar code dis2[]={"1 2 3 4 5 6"};

  11. void delay(uchar ms){  //延時(shí)ms時(shí)間
  12. uchar i;
  13. while(ms--){
  14. for(i=0;i<250;i++){
  15. _nop_();_nop_();_nop_();_nop_();  //延時(shí)4個(gè)機(jī)器周期
  16. }
  17. }
  18. }

  19. bit busy(){  //檢查是否忙碌
  20. bit result;
  21. RS=0;
  22. RW=1;
  23. E=1;
  24. _nop_();_nop_();_nop_();_nop_()
  25. result=BF;
  26. E=0;
  27. return result;
  28. }

  29. void wcmd(uchar cmd){  //寫(xiě)命令
  30. //while(busy()==1);  //判斷LCD是否忙碌
  31. delay(20);  //將判忙函數(shù)換成延時(shí)函數(shù)(5毫秒左右)可以改變LM016L只有背光不顯示的情況,還可以多次初始化
  32. RS=0;
  33. RW=0;  //RS和RW同時(shí)為低電平時(shí),可以寫(xiě)入命令
  34. E=0;  //為了使E正跳變,先置低電平
  35. _nop_();_nop_();_nop_();_nop_();
  36. P1=cmd;  //將命令輸入P1口
  37. _nop_();_nop_();_nop_();_nop_();
  38. E=1;  //E置高電平,產(chǎn)生正跳變,將指令寫(xiě)入液晶模塊
  39. _nop_();_nop_();_nop_();_nop_();
  40. E=0;


  41. }

  42. void pos(uchar y,uchar x){  //設(shè)置顯示位置y行x列
  43. y&=0x1;  //最多兩行,限制y的范圍0~1
  44. x&=0xf;  //最多每行16個(gè)字符,限制x的范圍0~15
  45. if(y==1) x|=0xc0;
  46. if(y==0) x|=0x80;
  47. wcmd(x);  //發(fā)送地址碼
  48. }

  49. void wdat(uchar dat){  //寫(xiě)要顯示的內(nèi)容
  50. //while(busy()==1);
  51. delay(20);
  52. RS=1;
  53. RW=0;
  54. E=0;
  55. P1=dat;
  56. _nop_();_nop_();_nop_();_nop_();
  57. E=1;
  58. _nop_();_nop_();_nop_();_nop_();
  59. E=0;

  60. }

  61. void init(){  //初始化
  62. delay(15);
  63. wcmd(0x38);
  64. delay(10);
  65. wcmd(0x38);
  66. delay(10);
  67. wcmd(0x38);
  68. delay(10);
  69. wcmd(0x0c);
  70. delay(10);
  71. wcmd(0x06);
  72. delay(10);
  73. wcmd(0x01);
  74. delay(10);
  75. }

  76. void main(void){
  77. uchar i;
  78. init();
  79. delay(1);
  80. pos(0,0);
  81. i=0;
  82. while(dis1[i]!='\0'){
  83. wdat(dis1[i]);
  84. i++;
  85. }
  86.         
  87. pos(1,0);
  88. i=0;
  89. while(dis2[i]!='\0'){
  90. wdat(dis2[i]);
  91. i++;
  92. }

  93. while(1);
  94. }
復(fù)制代碼






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