LCD1602显示程序 已定义显示字符、整数、字符串子函数 下载本文

#include #include

#define uchar unsigned char #define uint unsigned int uchar code disp1[]=\

uchar code disp2[]=%uchar code disp3[]=%uchar code disp4[]=\//LCD1602引脚定义

//采用8位并行方式,DB0~DB7连接至P20~P27 sbit RS=P1^2; sbit RW=P1^1; sbit CS=P1^0;

#define LCDDATA P2

///////////////////////////功能:延时1毫秒 //////////////////////////////// void Delay_xms(uint x) {

uint i,j;

for(i=0;i

//////////////////////////功能:12us延时//////////////////////////////// void Delay_xus(uint t) {

for(;t>0;t--) { _nop_();

} }

///////////////////////////控制LCD写时序,下降沿有效 /////////////////////////// void LCD_en_write(void) {

CS=1;//EN端产生一个高电平脉冲,控制LCD写时序 Delay_xus(20); CS=0; Delay_xus(20); }

////////////////////////////////写指令函数///////////////////////////////// void Write_Instruction(uchar command) { RS=0; RW=0; CS=1;

LCDDATA=command;

LCD_en_write();//写入指令数据,下降沿有效 }

///////////////////////////////写数据函数///////////////////////////// void Write_Data(uchar Wdata) { RS=1; RW=0; CS=1;

LCDDATA=Wdata;

LCD_en_write();//写入数据,下降沿有效 }

/////////////////////////字符显示初始地址设置/////////////////////////// void LCD_SET_XY(uchar X,uchar Y) {

uchar address; if(Y==0)

address=0x80+X; //Y=0,表示在第一行显示,地址基数为0x80

else

address=0xc0+X; //Y非0时,表时在第二行显示,地址基数为0xC0

Write_Instruction(address); //写指令,设置显示初始地址 }

//////////////////在第X列Y行开始显示,指针*S所指向的字符串 /////////////////// void LCD_write_str(uchar X,uchar Y,uchar *s) {

LCD_SET_XY(X,Y); //设置初始字符显示地址

while(*s) //逐次写入显示字符,直到最后一个字符\ {

Write_Data(*s); //写入当前字符并显示 s++; //地址指针加1,指向下一个待写字符 } }

//////////////////在第X行Y列开始显示Wdata所对应的单个字符///////////////// void LCD_write_char(uchar X,uchar Y,uchar Wdata)

{

LCD_SET_XY(X,Y); //写地址

Write_Data(Wdata); //写入当前字符并显示

}

///////////////////写一个无字符整数////////////////////////////////////////// void LCD_write_int(uchar X,uchar Y,uint intdat) {

unsigned char i=0,temp[5]; while(intdat/10 || intdat) {

temp[i]=intdat; intdat/=10; i++; }

LCD_SET_XY(X,Y); while(i) {

i--;

Write_Data(temp[i]+0x30);

} }

////////////////////////////清屏函数 //////////////////////////////////////////// void LCD_clear(void) {

Write_Instruction(0x01); Delay_xms(5); }

//显示屏初始化函数 void LCD_init(void) {

Write_Instruction(0x38); Delay_xms(5);

Write_Instruction(0x38); Delay_xms(5);

Write_Instruction(0x38);

Write_Instruction(0x08); //关显示,不显光标,光标不闪烁 Write_Instruction(0x01); //清屏 Delay_xms(5);

Write_Instruction(0x04); //写一字符,整屏显示不移动 //Write_Instruction(0x05); //写一字符,整屏右移 //Write_Instruction(0x06); //写一字符,整屏显示不移动 //Write_Instruction(0x07); //写一字符,整屏左移

Delay_xms(5);

//Write_Instruction(0x0B); //关闭显示(不显示字符,只有背光亮)

//8bit interface,2line,5*7dots

}

Write_Instruction(0x0C); //开显示,光标、闪烁都关闭

//Write_Instruction(0x0D); //开显示,不显示光标,但光标闪烁 //Write_Instruction(0x0E); //开显示,显示光标,但光标不闪烁 //Write_Instruction(0x0F); //开显示,光标、闪烁均显示

void main(void) { uint a; uchar i;

Delay_xms(50);//等待系统稳定 LCD_init(); //LCD初始化 LCD_clear(); //清屏

Delay_xms(50);

LCD_write_str(0,0,disp1); //开机信息显示 Delay_xms(2000);

LCD_write_str(0,1,disp2); Delay_xms(10000); LCD_clear(); //清屏 while(1) {for(a=1;a<1000;a++) {

Delay_xms(50);

LCD_write_str(0,0,disp3); LCD_write_int(0,1,a) ; LCD_write_str(8,1,disp4); Delay_xms(2000); } } }

//速度显示(a)