為什么本來應該顯示在第二行的句子顯示到了第一行,而且本來應該顯示16格結果只顯示了8格?
51hei圖片20211003214102.jpg (3.16 MB, 下載次數: 47)
下載附件
2021-10-3 21:41 上傳
單片機源程序如下:
- #include "reg52.h"
- #define LCD_DB P0
- sbit LCD_EN = P2^7;
- sbit LCD_RS = P2^6;
- sbit LCD_RW = P2^5;
- unsigned char T0RH = 0;
- unsigned char T0RL = 0;
- bit flag500ms = 0;
- unsigned char code str1[] = "Hello CuiWencan";
- unsigned char code str2[] = "I am your srevant";
- void InitLCD();
- void WaitReady();
- void ConfigTimer(unsigned int ms);
- void WriteCmd(unsigned char cmd);
- void WriteDat(unsigned char dat);
- void SetCursor(unsigned char x,unsigned char y);
- void ShowStr(unsigned char x,unsigned char y,unsigned char *str,unsigned char len);
- void main()
- {
- unsigned char i;
- unsigned char index1 = 0;
- unsigned char index2 = 0;
- unsigned char j = 0;
- unsigned char k = 0;
- unsigned char pdata bufMove1[16+sizeof(str1)+16];
- unsigned char pdata bufMove2[16+sizeof(str2)+16];
- EA = 1;
- ConfigTimer(10);
- InitLCD();
- for(i = 0;i < 16;i++)
- {
- bufMove1[i] = ' ';
- bufMove2[i] = ' ';
- }
- for(j = 0;j<(sizeof(str1)-1);j++)
- bufMove1[16+j] = str1[j];
- for(k = 0;k<(sizeof(str2)-1);k++)
- bufMove2[16+k] = str2[k];
- for(i = (16+sizeof(str1)-1);i<sizeof(bufMove1);i++)
- bufMove1[i] = ' ';
- for(i = (16+sizeof(str2)-1);i<sizeof(bufMove2);i++)
- bufMove2[i] = ' ';
- while(1)
- {
- if(flag500ms)
- {
- flag500ms = 0;
- ShowStr(0,0,bufMove1+index1,16);
- ShowStr(0,1,bufMove2+index2,16);
- index1++;
- index2++;
- if(index1 >= (16+sizeof(str1)-1))
- {
- index1 = 0;
- }
- if(index2 >= (16+sizeof(str2)-1))
- {
- index2 = 0;
- }
- }
- }
- }
- void ConfigTimer(unsigned int ms)
- {
- unsigned int tmq;
- EA = 1;
- TMOD &= 0xf0;
- TMOD |= 0x01;
- tmq = (ms*(11059200/12))/1000;
- tmq = 65536 - tmq;
- T0RH = (unsigned char)(tmq>>8);
- T0RL = (unsigned char)(tmq);
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
- }
- void WaitReady()
- {
- unsigned char sta;
- LCD_DB = 0xFF;
- LCD_RS = 0;
- LCD_RW = 1;
- do
- {
- LCD_EN = 1;
- sta = LCD_DB;
- LCD_EN = 0;
- }while(sta & 0x80);
- }
- void WriteCmd(unsigned char cmd)
- {
- WaitReady();
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_DB = cmd;
- LCD_EN = 1;
- LCD_EN = 0;
- }
- void WriteDat(unsigned char dat)
- {
- WaitReady();
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_DB = dat;
- LCD_EN = 1;
- LCD_EN = 0;
- }
- void SetCursor(unsigned char x,unsigned char y)
- {
- unsigned char addr;
- if(y == 0)
- addr = 0x00 + x;
- else
- addr = 0x40 + x;
- WriteCmd(addr | 0xf0);
- }
- void ShowStr(unsigned char x,unsigned char y,unsigned char *str,unsigned char len)
- {
- SetCursor(x,y);
- while(len--)
- {
- WriteDat(*str++);
- }
- }
- void InitLCD()
- {
- WriteCmd(0x38); //16*2 顯示,5*7 點陣,8 位數據接口
- WriteCmd(0x0C); //顯示器開,光標關閉
- WriteCmd(0x06); //文字不動,地址自動+1
- WriteCmd(0x01); //清屏
- }
- void InterruptTimer0() interrupt 1
- {
- static unsigned char tmr500ms = 0;
- TH0 = T0RH; //重新加載重載值
- TL0 = T0RL;
- tmr500ms++;
- if (tmr500ms >= 50)
- {
- tmr500ms = 0;
- flag500ms = 1;
- }
- }
復制代碼
|