📄 displaylcd.c
字号:
#include <at89x52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define xtal 8
#define Set_CS() P2 |= (1<<0)
#define Set_SID() P2 |= (1<<1)
#define Set_SCLK() P2 |= (1<<2)
#define Clr_CS() P2 &=~(1<<0)
#define Clr_SID() P2 &=~(1<<1)
#define Clr_SCLK() P2 &=~(1<<2)
//====================================================================
//函数声明
void Delay(uint ms); //延时子程序
void W_1byte(uchar RW, uchar RS, uchar W_data);
void Write_8bits(uint W_bits);
void LCD_Init(void);
/********************************************************************
********************************************************************/
//===================================================================
const uchar mynew1[]={" 开门鸿公司"};
const uchar mynew2[]={" 欢迎您!"};
const uchar mynew3[]={" 联系电话:"};
const uchar mynew4[]={"12345678"};
/********************************************************************
********************************************************************/
void main()
{
uchar i = 0;
P2 = 0x00;
P2 |= (1<<6);
P2 |= (1<<7);
P2 |= (1<<5);
P2 |= 1<<3;
Clr_CS();
Clr_SID();
Clr_SCLK();
LCD_Init();
while(1)
{
_nop_();
W_1byte(0,0,0x80); //显示的地址0x80
_nop_();
for(i=0;mynew1[i]!='\0';i++)
{
W_1byte(0,1,mynew1[i]);
}
W_1byte(0,0,0x90); //显示的地址0x90
for(i=0;mynew2[i]!='\0';i++)
{
W_1byte(0,1,mynew2[i]);
}
Delay(100);
_nop_();
W_1byte(0,0,0x01); //写指令
_nop_();
W_1byte(0,0,0x02); //写指令
_nop_();
W_1byte(0,0,0x80); //显示的地址0x88
for(i=0;mynew3[i]!='\0';i++)
{
W_1byte(0,1,mynew3[i]);
}
W_1byte(0,0,0x90); //显示的地址0x98
for(i=0;mynew4[i]!='\0';i++)
{
W_1byte(0,1,mynew4[i]);
}
Delay(100);
_nop_();
W_1byte(0,0,0x01); //写指令
_nop_();
W_1byte(0,0,0x02); //写指令
_nop_();
Delay(100);
}
}
/******************************************************************/
void LCD_Init(void)
{
uchar cmd;
cmd=0x30; //功能设置 8位数据,基本指令
W_1byte(0,0,cmd);
Delay(2);
cmd=0x0C; //显示状态 ON,游标OFF,反白OFF
W_1byte(0,0,cmd); //写指令
Delay(2);
cmd=0x01; //清除显示
W_1byte(0,0,cmd); //写指令
Delay(2);
cmd=0x02; //地址归位
W_1byte(0,0,cmd); //写指令
Delay(2);
cmd=0x80; //设置DDRAM地址
W_1byte(0,0,cmd); //写指令
Delay(2); //延时
}
/*******************************************************************
函 数 名:W_1byte
入口参数:RW、RS、W_data
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:写一个字节的数据到12864液晶,包括指令和数据
说 明:RW=1,从液晶读数据到MCU;RW=0,写一个数据到液晶;
(一般RW都设为0,即只向液晶写数据,不读数据)
RS=1,写入的是数据;RS=0,写入的是指令;
一般模式:RW=0,RS=1;写数据
RW=0,RS=0;写指令
********************************************************************/
void W_1byte(uchar RW, uchar RS, uchar W_data)
{
uint H_data,L_data,S_ID = 0xf8; //11111RWRS0
if(RW == 0)
{
S_ID &=~ 0x04;
}
else //if(RW==1)
{
S_ID |= 0X04;
}
if(RS == 0)
{
S_ID &=~ 0x02;
}
else //if(RS==1)
{
S_ID |= 0X02;
}
H_data = W_data;
H_data &= 0xf0; //屏蔽低4位的数据
L_data = W_data; //xxxx0000格式
L_data &= 0x0f; //屏蔽高4位的数据
L_data <<= 4; //xxxx0000格式
Set_CS();
Write_8bits(S_ID); //发送S_ID
Write_8bits(H_data); //发送H_data
Write_8bits(L_data); //发送L_data
Clr_CS();
}
/********************************************************************
函 数 名:Write_8bits
入口参数:W_bits
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:负责串行输出8个bit位
说 明:
********************************************************************/
void Write_8bits(uint W_bits)
{
uint i,Temp_data,j=0;
for(i=0; i<8; i++)
{
Temp_data = W_bits;
Temp_data <<= i;
if((Temp_data&0x80)==0) //bit7 is zero
{
Clr_SID();
j++;
Set_SCLK();
_nop_();
Clr_SCLK();
_nop_();
Clr_SID();
_nop_();
}
else //bit7 is one
{
Set_SID();
_nop_();
Set_SCLK();
_nop_();
_nop_();
Clr_SCLK();
_nop_();
Clr_SID();
_nop_();
}
}
}
/********************************************************************
函 数 名:Delay
入口参数:ms
出口参数:无
建立日期:2007年3月3日
修改日期:
函数作用:毫秒级的延时程序,当晶振为12Mhz时,xtal=12;
说 明:
********************************************************************/
void Delay(uint ms)
{
uint i;
while(ms--)
{
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
}
//===================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -