#include <Target.h>
#include <C8051F410_defs.h>
#define BUSY 0x80
unsigned char LCD_data;
void delay(unsigned int t)
{
for(; t>0; t--);
}
unsigned char LCD_ReadStatus(void)
{
LCD_data = 0x00;
LCD_RS = 0;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
LCD_data = (0x00)|(P0^2 << 7)|(P0^3 <<6)|(P0^6 << 5)|(P0^7 << 4)|(P2^0 << 3)|(P2^1 << 2)|(P2^2 << 1)|(P2^3<<0);
while(LCD_data & BUSY);
return(LCD_data);
}
unsigned char LCD_ReadData(void)
{
LCD_data = 0x00;
LCD_RS = 1;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
LCD_data = (0x00)|(P0^2 << 7)|(P0^3 <<6)|(P0^6 << 5)|(P0^7 << 4)|(P2^0 << 3)|(P2^1 << 2)|(P2^2 << 1)|(P2^3);
return(LCD_data);
}
void LCD_WriteCommand(unsigned char _data, unsigned char status)
{
unsigned char temp;
if(status)
{
LCD_ReadStatus();
}
temp = _data;
LCD_DB7 = (temp >> 7);
LCD_DB6 = (temp & 0x7f) >> 6;
LCD_DB5 = (temp & 0x3f) >> 5;
LCD_DB4 = (temp & 0x1f) >> 4;
LCD_DB3 = (temp & 0x0f) >> 3;
LCD_DB2 = (temp & 0x07) >> 2;
LCD_DB1 = (temp & 0x03) >> 1;
LCD_DB0 = (temp & 0x01);
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
}
void LCD_WriteData(unsigned char _data)
{
unsigned char temp;
temp = _data;
LCD_ReadStatus(); //检测忙
LCD_DB7 = (temp >> 7);
LCD_DB6 = (temp & 0x7f) >> 6;
LCD_DB5 = (temp & 0x3f) >> 5;
LCD_DB4 = (temp & 0x1f) >> 4;
LCD_DB3 = (temp & 0x0f) >> 3;
LCD_DB2 = (temp & 0x07) >> 2;
LCD_DB1 = (temp & 0x03) >> 1;
LCD_DB0 = (temp & 0x01);
LCD_RS = 1;
LCD_RW = 0;
LCD_E = 0; //若晶振速度太高可以在这后加小的延时
LCD_E = 0; //延时
LCD_E = 1;
}
void LCD_INIT(void)
{
LCD_data = 0;
LCD_WriteCommand(0x38,0); //三次显示模式设置,不检测忙信号
delay(100);
LCD_WriteCommand(0x38,0);
delay(100);
LCD_WriteCommand(0x38,0);
delay(100);
LCD_WriteCommand(0x38,1); //显示模式设置,开始要求每次检测忙信号
LCD_WriteCommand(0x08,1); //关闭显示
LCD_WriteCommand(0x01,1); //显示清屏
LCD_WriteCommand(0x06,1); // 显示光标移动设置
LCD_WriteCommand(0x0C,1); // 显示开及光标设置
}
void LCD_DisplayChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if(Y)
X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80; // 算出指令码
LCD_WriteCommand(X, 0); //这里不检测忙信号,发送地址码
LCD_WriteData(DData);
}
//按指定位置显示一串字符
void LCD_DisplayList(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength]>=0x20) //若到达字串尾则退出
{
if (X <= 0xF) //X坐标应小于0xF
{
LCD_DisplayChar(X, Y, DData[ListLength]); //显示单个字符
ListLength++;
X++;
}
}
}