标题: 在学单片机LCD1602整屏移动时遇到问题,显示的与预期不符 [打印本页]

作者: 机械嗷嗷嗷    时间: 2021-10-3 21:48
标题: 在学单片机LCD1602整屏移动时遇到问题,显示的与预期不符
为什么本来应该显示在第二行的句子显示到了第一行,而且本来应该显示16格结果只显示了8格?


单片机源程序如下:
  1. #include "reg52.h"

  2. #define LCD_DB P0
  3. sbit LCD_EN = P2^7;
  4. sbit LCD_RS = P2^6;
  5. sbit LCD_RW = P2^5;

  6. unsigned char T0RH = 0;
  7. unsigned char T0RL = 0;
  8. bit flag500ms = 0;

  9. unsigned char code str1[] = "Hello CuiWencan";
  10. unsigned char code str2[] = "I am your srevant";

  11. void InitLCD();
  12. void WaitReady();
  13. void ConfigTimer(unsigned int ms);
  14. void WriteCmd(unsigned char cmd);
  15. void WriteDat(unsigned char dat);
  16. void SetCursor(unsigned char x,unsigned char y);
  17. void ShowStr(unsigned char x,unsigned char y,unsigned char *str,unsigned char len);

  18. void main()
  19. {
  20.         unsigned char i;
  21.         unsigned char index1 = 0;
  22.         unsigned char index2 = 0;
  23.         unsigned char j = 0;
  24.         unsigned char k = 0;
  25.         unsigned char pdata bufMove1[16+sizeof(str1)+16];        
  26.         unsigned char pdata bufMove2[16+sizeof(str2)+16];

  27.         EA = 1;
  28.         ConfigTimer(10);
  29.         InitLCD();

  30.         for(i = 0;i < 16;i++)
  31.         {
  32.                 bufMove1[i] = ' ';
  33.                 bufMove2[i] = ' ';
  34.         }
  35.         for(j = 0;j<(sizeof(str1)-1);j++)
  36.                 bufMove1[16+j] = str1[j];
  37.         for(k = 0;k<(sizeof(str2)-1);k++)
  38.                 bufMove2[16+k] = str2[k];
  39.         for(i = (16+sizeof(str1)-1);i<sizeof(bufMove1);i++)
  40.                 bufMove1[i] = ' ';        
  41.         for(i = (16+sizeof(str2)-1);i<sizeof(bufMove2);i++)
  42.                 bufMove2[i] = ' ';
  43.         while(1)
  44.         {
  45.                 if(flag500ms)
  46.                 {
  47.                          flag500ms = 0;
  48.                         ShowStr(0,0,bufMove1+index1,16);
  49.                         ShowStr(0,1,bufMove2+index2,16);
  50.                         index1++;
  51.                         index2++;
  52.                         if(index1 >= (16+sizeof(str1)-1))
  53.                         {
  54.                                 index1 = 0;                        
  55.                         }
  56.                         if(index2 >= (16+sizeof(str2)-1))
  57.                         {
  58.                                 index2 = 0;
  59.                         }
  60.                 }
  61.         }        
  62. }

  63. void ConfigTimer(unsigned int ms)
  64. {
  65.         unsigned int tmq;
  66.         EA = 1;
  67.         TMOD &= 0xf0;
  68.         TMOD |= 0x01;
  69.     tmq = (ms*(11059200/12))/1000;
  70.         tmq = 65536 - tmq;
  71.         T0RH = (unsigned char)(tmq>>8);
  72.         T0RL = (unsigned char)(tmq);
  73.         TH0 = T0RH;
  74.         TL0 = T0RL;
  75.         ET0 = 1;
  76.         TR0 = 1;
  77. }

  78. void WaitReady()
  79. {
  80.         unsigned char sta;
  81.         LCD_DB = 0xFF;
  82.         LCD_RS = 0;
  83.         LCD_RW = 1;
  84.         do
  85.         {
  86.                 LCD_EN = 1;
  87.                 sta = LCD_DB;
  88.                 LCD_EN = 0;        
  89.         }while(sta & 0x80);        
  90. }

  91. void WriteCmd(unsigned char cmd)
  92. {
  93.         WaitReady();
  94.         LCD_RS = 0;
  95.         LCD_RW = 0;
  96.         LCD_DB = cmd;
  97.         LCD_EN = 1;
  98.         LCD_EN = 0;
  99. }

  100. void WriteDat(unsigned char dat)
  101. {
  102.         WaitReady();
  103.         LCD_RS = 1;
  104.         LCD_RW = 0;
  105.         LCD_DB = dat;
  106.         LCD_EN = 1;
  107.         LCD_EN = 0;
  108. }

  109. void SetCursor(unsigned char x,unsigned char y)
  110. {
  111.         unsigned char addr;
  112.         if(y == 0)
  113.                 addr = 0x00 + x;         
  114.         else
  115.                 addr = 0x40 + x;        
  116.         WriteCmd(addr | 0xf0);
  117. }

  118. void ShowStr(unsigned char x,unsigned char y,unsigned char *str,unsigned char len)
  119. {
  120.         SetCursor(x,y);
  121.         while(len--)
  122.         {
  123.                 WriteDat(*str++);
  124.         }
  125. }

  126. void InitLCD()
  127. {
  128.     WriteCmd(0x38); //16*2 显示,5*7 点阵,8 位数据接口
  129.     WriteCmd(0x0C); //显示器开,光标关闭
  130.     WriteCmd(0x06); //文字不动,地址自动+1
  131.     WriteCmd(0x01); //清屏
  132. }

  133. void InterruptTimer0() interrupt 1
  134. {
  135.     static unsigned char tmr500ms = 0;

  136.     TH0 = T0RH; //重新加载重载值
  137.     TL0 = T0RL;
  138.     tmr500ms++;
  139.     if (tmr500ms >= 50)
  140.     {
  141.             tmr500ms = 0;
  142.             flag500ms = 1;
  143.     }
  144. }
复制代码


作者: chenyinhu    时间: 2021-10-9 17:16
把所有的16+sizeof(str1)+16改成8+试试呢
作者: chenyinhu    时间: 2021-10-9 17:17
  for(i = 0;i < 16;i++)
        {
                bufMove1[i] = ' ';
                bufMove2[i] = ' ';
        }这部分也去掉,估计是位置已经超了




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1