1602液晶的显示问题
时间:10-02
整理:3721RD
点击:
请各位大神帮我看一下 我用protuse做的仿真 液晶不显示字符 不知道哪里出的问题程序:
#include <reg51.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P2^2;
bit flagT0 = 0; //T0中断产生标志
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
unsigned char code str1[] = "Kingst Studio"; //待显示的第一行字符串
unsigned char code str2[] = "Let's move..."; //待显示的第二行字符串,需保持与第一行字符串等长,较短的行可用空格补齐
void ConfigTimer0(unsigned int ms);
void LcdShowStr(unsigned char x, unsigned char y, const unsigned char *str, unsigned char len);
void LcdInit();
void main ()
{
unsigned char i;
unsigned char iMove = 0; //移动索引
unsigned int tmrMove = 0; //屏幕移动定时器
unsigned char pdata bufMove1[16 + sizeof(str1) + 16]={0}; //移动显示的缓冲区
unsigned char pdata bufMove2[16 + sizeof(str1) + 16]={0}; //移动显示的缓冲区
EA = 1; //开总中断
ConfigTimer0(10); //配置T0定时10ms
LcdInit(); //初始化液晶
for (i=0; i<16; i++) //缓冲区开头一段填充为空格
{
bufMove1[ i] = ' ';
bufMove2[ i] = ' ';
}
for (i=0; i<(sizeof(str1)-1); i++) //待显示字符串拷贝到缓冲区中间位置
{
bufMove1[16+i] = str1[ i];
bufMove2[16+i] = str2[ i];
}
for (i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++) //缓冲区结尾一段也填充为空格
{
bufMove1[ i] = ' ';
bufMove2[ i] = ' ';
}
while(1)
{
if (flagT0)
{
flagT0 = 0;
tmrMove += 10;
if (tmrMove >= 500) //每500ms移动一次屏幕
{
tmrMove = 0;
LcdShowStr(0, 0, bufMove1+iMove, 16); //从缓冲区抽出需显示的一段字符显示到液晶上
LcdShowStr(0, 1, bufMove2+iMove, 16);
iMove++; //移动索引递增,实现左移
if (iMove >= (16+sizeof(str1)-1)) //起始位置达到字符串尾部后即返回从头开始
{
iMove = 0;
}
}
}
}
}
void ConfigTimer0(unsigned int ms) //T0配置函数
{
unsigned long tmp;
tmp = 11059200 / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 12; //修正中断响应延时造成的误差
T0RH = (unsigned char)(tmp >> 8); //定时器重载值拆分为高低字节
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //配置T0为模式1
TH0 = T0RH; //加载T0重载值
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
void LcdWaitReady() //等待液晶准备好
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_E = 1;
sta = LCD1602_DB; //读取状态字
LCD1602_E = 0;
} while (sta & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止
}
void LcdWriteCmd(unsigned char cmd) //写入命令函数
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat) //写入数据函数
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdInit() //液晶初始化函数
{
LcdWriteCmd(0x38); //16*2显示,5*7点阵,8位数据接口
LcdWriteCmd(0x0C); //显示器开,光标关闭
LcdWriteCmd(0x06); //文字不动,地址自动+1
LcdWriteCmd(0x01); //清屏
}
void LcdShowStr(unsigned char x, unsigned char y, const unsigned char *str, unsigned char len) // 显示字符串,屏幕起始坐标(x,y),字符串指针str,需显示长度len
{
unsigned char addr;
//由输入的显示坐标计算显示RAM的地址
if (y == 0)
{
addr = 0x00 + x; //第一行字符地址从0x00起始
}
else
{
addr = 0x40 + x; //第二行字符地址从0x40起始
}
//由起始显示RAM地址连续写入字符串
LcdWriteCmd(addr | 0x80); //写入起始地址
while (len--) //连续写入字符串数据
{
LcdWriteDat(*str);
str++;
}
}
void InterruptTimer0() interrupt 1 //T0中断服务函数
{
TH0 = T0RH; //定时器重新加载重载值
TL0 = T0RL;
flagT0 = 1;
}
#include <reg51.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P2^2;
bit flagT0 = 0; //T0中断产生标志
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
unsigned char code str1[] = "Kingst Studio"; //待显示的第一行字符串
unsigned char code str2[] = "Let's move..."; //待显示的第二行字符串,需保持与第一行字符串等长,较短的行可用空格补齐
void ConfigTimer0(unsigned int ms);
void LcdShowStr(unsigned char x, unsigned char y, const unsigned char *str, unsigned char len);
void LcdInit();
void main ()
{
unsigned char i;
unsigned char iMove = 0; //移动索引
unsigned int tmrMove = 0; //屏幕移动定时器
unsigned char pdata bufMove1[16 + sizeof(str1) + 16]={0}; //移动显示的缓冲区
unsigned char pdata bufMove2[16 + sizeof(str1) + 16]={0}; //移动显示的缓冲区
EA = 1; //开总中断
ConfigTimer0(10); //配置T0定时10ms
LcdInit(); //初始化液晶
for (i=0; i<16; i++) //缓冲区开头一段填充为空格
{
bufMove1[ i] = ' ';
bufMove2[ i] = ' ';
}
for (i=0; i<(sizeof(str1)-1); i++) //待显示字符串拷贝到缓冲区中间位置
{
bufMove1[16+i] = str1[ i];
bufMove2[16+i] = str2[ i];
}
for (i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++) //缓冲区结尾一段也填充为空格
{
bufMove1[ i] = ' ';
bufMove2[ i] = ' ';
}
while(1)
{
if (flagT0)
{
flagT0 = 0;
tmrMove += 10;
if (tmrMove >= 500) //每500ms移动一次屏幕
{
tmrMove = 0;
LcdShowStr(0, 0, bufMove1+iMove, 16); //从缓冲区抽出需显示的一段字符显示到液晶上
LcdShowStr(0, 1, bufMove2+iMove, 16);
iMove++; //移动索引递增,实现左移
if (iMove >= (16+sizeof(str1)-1)) //起始位置达到字符串尾部后即返回从头开始
{
iMove = 0;
}
}
}
}
}
void ConfigTimer0(unsigned int ms) //T0配置函数
{
unsigned long tmp;
tmp = 11059200 / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 12; //修正中断响应延时造成的误差
T0RH = (unsigned char)(tmp >> 8); //定时器重载值拆分为高低字节
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //配置T0为模式1
TH0 = T0RH; //加载T0重载值
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
void LcdWaitReady() //等待液晶准备好
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_E = 1;
sta = LCD1602_DB; //读取状态字
LCD1602_E = 0;
} while (sta & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止
}
void LcdWriteCmd(unsigned char cmd) //写入命令函数
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat) //写入数据函数
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdInit() //液晶初始化函数
{
LcdWriteCmd(0x38); //16*2显示,5*7点阵,8位数据接口
LcdWriteCmd(0x0C); //显示器开,光标关闭
LcdWriteCmd(0x06); //文字不动,地址自动+1
LcdWriteCmd(0x01); //清屏
}
void LcdShowStr(unsigned char x, unsigned char y, const unsigned char *str, unsigned char len) // 显示字符串,屏幕起始坐标(x,y),字符串指针str,需显示长度len
{
unsigned char addr;
//由输入的显示坐标计算显示RAM的地址
if (y == 0)
{
addr = 0x00 + x; //第一行字符地址从0x00起始
}
else
{
addr = 0x40 + x; //第二行字符地址从0x40起始
}
//由起始显示RAM地址连续写入字符串
LcdWriteCmd(addr | 0x80); //写入起始地址
while (len--) //连续写入字符串数据
{
LcdWriteDat(*str);
str++;
}
}
void InterruptTimer0() interrupt 1 //T0中断服务函数
{
TH0 = T0RH; //定时器重新加载重载值
TL0 = T0RL;
flagT0 = 1;
}
上面程序有两句忘了改 原文: unsigned char pdata bufMove1[16 + sizeof(str1) + 16]; //移动显示的缓冲区
unsigned char pdata bufMove2[16 + sizeof(str1) + 16]; //移动显示的缓冲区