以下是原代码:
- #include<reg52.h>
- #include<intrins.h>
- #define LCD1602_DB P0
- sbit LCD1602_RS = P2^0;
- sbit LCD1602_RW = P2^1;
- sbit LCD1602_E = P2^2;
- bit flag500ms = 0; //500毫秒标志
- unsigned char T0RH = 0;
- unsigned char T0RL = 0;
- unsigned char code str1[] = "DA JIA HAO.";
- unsigned char code str2[] = "Let's move...";
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str, unsigned char len);
- void configT0(unsigned int ms);
- void InitLcd1602();
- void main()
- {
- unsigned char i;
- unsigned char index = 0;
-
- unsigned char pdata bufMove1[16 + sizeof(str1) + 16]; //字符缓冲区
- unsigned char pdata bufMove2[16 + sizeof(str2) + 16];
-
- EA = 1;
- InitLcd1602();
- configT0(2);
-
- for(i = 0; i < 16; i++) //字符缓冲区前15字符赋初值
- {
- bufMove1[i] = ' ';
- bufMove2[i] = ' ';
- }
- for(i = 0; i < (sizeof(str1) - 1); i++) //字符缓冲区中间15字符赋初值
- {
- bufMove1[16+i] = str1[i];
- bufMove2[16+i] = str2[i];
- }
- for(i = (16 + sizeof(str1) - 1); i < sizeof(bufMove1); i++) //字符缓冲区后15字符赋初值
- {
- bufMove1[i] = ' ';
- bufMove2[i] = ' ';
- }
-
- while(1)
- {
- if(flag500ms) //到达500毫秒刷新一次
- {
- flag500ms = 0;
- LcdShowStr(0, 0, bufMove1 + index, 16);
- LcdShowStr(0, 1, bufMove2 + index, 16);
- index++;
- if(index >= (16 + sizeof(str1) - 1))
- {
- index = 0;
- }
- }
- }
- }
- void configT0(unsigned int ms)
- {
- unsigned long int tmp;
- tmp = 12000000 / 12;
- tmp = (tmp * ms) / 1000;
- tmp = 65536 - tmp + 12;
- T0RH = (unsigned char)(tmp >> 8);
- T0RL = (unsigned char)tmp;
- TMOD &= 0XF0;
- TMOD |= 0X01;
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
- }
- void LcdWaiReady() //读指令
- {
- unsigned char sta;
- LCD1602_E = 0;
- LCD1602_DB = 0XFF;
- LCD1602_RS = 0;
- LCD1602_RW = 1;
- do
- {
- LCD1602_E = 1;
- sta = LCD1602_DB;
- LCD1602_E = 0;
- }
- while(sta & 0x80);
- }
- void LcdWriteCMD(unsigned char cmd) //写指令
- {
- LcdWaiReady();
- LCD1602_E = 0;
- LCD1602_RS = 0;
- LCD1602_RW = 0;
- LCD1602_DB = cmd;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- void LcdWriteDat(unsigned char dat) //写数据
- {
- LcdWaiReady();
- LCD1602_E = 0;
- LCD1602_RS = 1;
- LCD1602_RW = 0;
- LCD1602_DB = dat;
- LCD1602_E = 1;
- LCD1602_E = 0;
- }
- void LcdSetCursor(unsigned char x, unsigned char y)
- {
- unsigned char addr;
- if(y == 0)
- {
- addr = 0x00 + x;
- }
- else
- {
- addr = 0x40 + x;
- }
- LcdWriteCMD(addr | 0x80);
- }
- void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str, unsigned char len)
- {
- LcdSetCursor(x, y);
- while(len--)
- {
- LcdWriteDat(*str++);
- }
- }
- void InitLcd1602()
- {
- LcdWriteCMD(0x38);
- LcdWriteCMD(0x0C);
- LcdWriteCMD(0x06);
- LcdWriteCMD(0x01);
- }
- void interrupt_T0() interrupt 1
- {
- static unsigned char time500ms = 0;
- TH0 = T0RH;
- TL0 = T0RL;
-
- time500ms++;
-
- if(time500ms >= 250)
- {
- time500ms = 0;
- flag500ms = 1;
- }
- }
复制代码
以下是原代码与仿真文件:
2.LCD1602整屏移动.rar
(84.59 KB, 下载次数: 8)
|