三、程序代码
lcd.c
//延时子程序模块
//**********************************************
void longdelay(uint32_t delay) //长延时程序延时 n*100ms
{ uint32_t i;
for(;delay>0;delay--)
{for(i=0;i<100;i++) //100ms延时.
DrvSYS_Delay (1000); //调用库函数,延时n us
}
}
void shortdelay(uint32_t dly) //短延时程序 nms
{
for(;dly>0;dly--)
DrvSYS_Delay(1000);
}
//**********************************
//液晶初始化
//**********************************
void init_lcd(void)
{ uint8_t comm;
shortdelay(3);
comm=0x01; //清除显示
wr_i_lcd(comm);
shortdelay(3);
comm=0x30; //普通指令集格式
wr_i_lcd(comm);
shortdelay(3);
comm=0x06; //光标右移
wr_i_lcd(comm);
shortdelay(3);
comm=0x02; //地址归位
wr_i_lcd(comm);
shortdelay(3);
comm=0x06; //整体不移动
wr_i_lcd(comm);
shortdelay(3);
comm=0x0c; //整体显示,不反白
wr_i_lcd(comm);
shortdelay(3);
comm=0x01; //复位
wr_i_lcd(comm);
shortdelay(3);
comm=0x80; //设定DDRAM起始为0x80
wr_i_lcd(comm);
shortdelay(3);
}
//***********************************
//填充液晶DDRAM全为空格
//**********************************
void clrram_lcd(void)
{ uint16_t data;
data=0x30;
wr_i_lcd(data);
shortdelay(3);
data=0x01;
wr_i_lcd(data);
longdelay(2);
}
//***********************************
//对液晶写数据
//content为要写入的数据
//***********************************
void wr_d_lcd(uint8_t content)
{
busy_lcd();
DrvGPIO_Open(E_PORT2, E_PIN0, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN1, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN2, E_IO_OUTPUT);
DrvGPIO_SetBit(E_PORT2, E_PIN0); //rs=1,data mode
DrvGPIO_ClrBit(E_PORT2, E_PIN1); //rw=0,write mode
DrvGPIO_Open(E_PORT0, E_PIN0, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN1, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN2, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN3, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN4, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN5, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN6, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN7, E_IO_OUTPUT);
DrvGPIO_SetPortBits(E_PORT0,content); //write data
DrvGPIO_SetBit(E_PORT2, E_PIN2); //enable
shortdelay(2);
DrvGPIO_ClrBit(E_PORT2, E_PIN2); //disable
}
//********************************
//对液晶写指令
//content为要写入的指令代码
//*****************************
void wr_i_lcd(uint8_t content)
{
busy_lcd();
DrvGPIO_Open(E_PORT2, E_PIN0, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN1, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN2, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_PORT2, E_PIN0); //rs=0,command mode
DrvGPIO_ClrBit(E_PORT2, E_PIN1); //rw=0,write mode
DrvGPIO_Open(E_PORT0, E_PIN0, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN1, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN2, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN3, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN4, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN5, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN6, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN7, E_IO_OUTPUT);
DrvGPIO_SetPortBits(E_PORT0,content); //write command
DrvGPIO_SetBit(E_PORT2, E_PIN2); //enable
shortdelay(2);
DrvGPIO_ClrBit(E_PORT2, E_PIN2); //disable
}
//********************************
//液晶检测忙状态
//在写入之前必须执行
//********************************
void busy_lcd(void)
{
DrvGPIO_Open(E_PORT0, E_PIN0, E_IO_OUTPUT); //下面几行都为打开P0端口,设定为输出模式
DrvGPIO_Open(E_PORT0, E_PIN1, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN2, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN3, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN4, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN5, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN6, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT0, E_PIN7, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN0, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN1, E_IO_OUTPUT);
DrvGPIO_Open(E_PORT2, E_PIN2, E_IO_OUTPUT);
DrvGPIO_SetPortBits(E_PORT0,0xff);
DrvGPIO_ClrBit(E_PORT2, E_PIN0); //rs=0
DrvGPIO_SetBit(E_PORT2, E_PIN1); //rw=1,read
DrvGPIO_SetBit(E_PORT2, E_PIN2); //enable
DrvGPIO_Open(E_PORT0, E_PIN7, E_IO_INPUT); //set p0.7 as a input pin ==busy
while((DrvGPIO_GetBit (E_PORT0, E_PIN7))!=0); //while is busy
DrvGPIO_ClrBit(E_PORT2, E_PIN2); //disable
}
//********************************
//指定要显示字符的坐标
//*******************************
void gotoxy(uint8_t y,uint8_t x)
{
if(y==1)
wr_i_lcd(0x80|x); //第一行
if(y==2)
wr_i_lcd(0x90|x); //第二行
if(y==3)
wr_i_lcd((0x80|x)+8); //第三行
if(y==4)
wr_i_lcd((0x90|x)+8); //第四行
}
//**********************************
//液晶显示字符串程序
//**********************************
void print(uint8_t *str)
{
while(*str!='\0')
{
wr_d_lcd(*str);
str++;
}
}
//***************************************
//液晶显示主程序模块
//***************************************
void show()
{
gotoxy(1,0);
print("南京航空航天大学");
shortdelay(200);
gotoxy(2,0);
print("计算机科学与技术");
shortdelay(200); //扫描延时
gotoxy(3,0);
print("WWW.NUAA.EDU.CN");
shortdelay(200); //扫描延时
gotoxy(4,0);
print("12864 液晶测试");
shortdelay(200); //扫描延时
}
|