|
謝謝回復(fù),整個(gè)程序如下,請(qǐng)問哪里可以BUFMOVE1是指針類型?不是bufMove1+index整體作為指針類型嗎?bufMove1不是這個(gè)16+sizeof(str1)+16范圍了嗎?再加上index是什么意思?不明白bufMove1+index是抽取了哪一段字符?
#include <reg52.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_E = P1^5;
bit flag500ms = 0; //500ms 定時(shí)標(biāo)志
unsigned char T0RH = 0; //T0 重載值的高字節(jié)
unsigned char T0RL = 0; //T0 重載值的低字節(jié)
//待顯示的第一行字符串
unsigned char code str1[] = "Kingst Studio";
//待顯示的第二行字符串,需保持與第一行字符串等長(zhǎng),較短的行可用空格補(bǔ)齊
unsigned char code str2[] = "Let's move...";
void ConfigTimer0(unsigned int ms);
void InitLcd1602();
void LcdShowStr(unsigned char x, unsigned char y,
unsigned char *str, unsigned char len);
void main()
{
unsigned char i;
unsigned char index = 0; //移動(dòng)索引
unsigned char pdata bufMove1[16+sizeof(str1)+16]; //移動(dòng)顯示緩沖區(qū) 1
unsigned char pdata bufMove2[16+sizeof(str2)+16]; //移動(dòng)顯示緩沖區(qū) 2
EA = 1; //開總中斷
ConfigTimer0(10); //配置 T0 定時(shí) 10ms
InitLcd1602(); //初始化液晶
//緩沖區(qū)開頭一段填充為空格
for (i=0; i<16; i++)
{
bufMove1 = ' ';
bufMove2 = ' ';
}
//待顯示字符串拷貝到緩沖區(qū)中間位置
for (i=0; i<(sizeof(str1)-1); i++)
{
bufMove1[16+i] = str1;
bufMove2[16+i] = str2;
}
//緩沖區(qū)結(jié)尾一段也填充為空格
for (i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++)
{
bufMove1 = ' ';
bufMove2 = ' ';
}
while (1)
{
if (flag500ms) //每 500ms 移動(dòng)一次屏幕
{
flag500ms = 0;
//從緩沖區(qū)抽出需顯示的一段字符顯示到液晶上
LcdShowStr(0, 0, bufMove1+index, 16);
LcdShowStr(0, 1, bufMove2+index, 16);
//移動(dòng)索引遞增,實(shí)現(xiàn)左移
index++;
if (index >= (16+sizeof(str1)-1))
{ //起始位置達(dá)到字符串尾部后即返回從頭開始
index = 0;
}
}
}
}
/* 配置并啟動(dòng) T0,ms-T0 定時(shí)時(shí)間 */
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp; //臨時(shí)變量
tmp = 11059200 / 12; //定時(shí)器計(jì)數(shù)頻率
tmp = (tmp * ms) / 1000; //計(jì)算所需的計(jì)數(shù)值
tmp = 65536 - tmp; //計(jì)算定時(shí)器重載值
tmp = tmp + 12; //補(bǔ)償中斷響應(yīng)延時(shí)造成的誤差
T0RH = (unsigned char)(tmp>>8); //定時(shí)器重載值拆分為高低字節(jié)
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零 T0 的控制位
TMOD |= 0x01; //配置 T0 為模式 1
TH0 = T0RH; //加載 T0 重載值
TL0 = T0RL;
ET0 = 1; //使能 T0 中斷
TR0 = 1; //啟動(dòng) T0
}
/* 等待液晶準(zhǔn)備好 */
void LcdWaitReady()
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do {
LCD1602_E = 1;
sta = LCD1602_DB; //讀取狀態(tài)字
LCD1602_E = 0;
} while (sta & 0x80); //bit7 等于 1 表示液晶正忙,重復(fù)檢測(cè)直到其等于 0 為止
}
/* 向 LCD1602 液晶寫入一字節(jié)命令,cmd-待寫入命令值 */
void LcdWriteCmd(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 向 LCD1602 液晶寫入一字節(jié)數(shù)據(jù),dat-待寫入數(shù)據(jù)值 */
void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
/* 設(shè)置顯示 RAM 起始地址,亦即光標(biāo)位置,(x,y)-對(duì)應(yīng)屏幕上的字符坐標(biāo) */
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (y == 0) //由輸入的屏幕坐標(biāo)計(jì)算顯示 RAM 的地址
addr = 0x00 + x; //第一行字符地址從 0x00 起始
else
addr = 0x40 + x; //第二行字符地址從 0x40 起始
LcdWriteCmd(addr | 0x80); //設(shè)置 RAM 地址
}
/* 在液晶上顯示字符串,(x,y)-對(duì)應(yīng)屏幕上的起始坐標(biāo),
str-字符串指針,len-需顯示的字符長(zhǎng)度 */
void LcdShowStr(unsigned char x, unsigned char y,
unsigned char *str, unsigned char len)
{
LcdSetCursor(x, y); //設(shè)置起始地址
while (len--) //連續(xù)寫入 len 個(gè)字符數(shù)據(jù)
{
LcdWriteDat(*str++); //先取 str 指向的數(shù)據(jù),然后 str 自加 1
}
}
/* 初始化 1602 液晶 */
void InitLcd1602()
{
LcdWriteCmd(0x38); //16*2 顯示,5*7 點(diǎn)陣,8 位數(shù)據(jù)接口
LcdWriteCmd(0x0C); //顯示器開,光標(biāo)關(guān)閉
LcdWriteCmd(0x06); //文字不動(dòng),地址自動(dòng)+1
LcdWriteCmd(0x01); //清屏
}
/* T0 中斷服務(wù)函數(shù),定時(shí) 500ms */
void InterruptTimer0() interrupt 1
{
static unsigned char tmr500ms = 0;
TH0 = T0RH; //重新加載重載值
TL0 = T0RL;
tmr500ms++;
if (tmr500ms >= 50)
{
tmr500ms = 0;
flag500ms = 1;
}
} |
|