

#include "reg51.h"
#define uint unsigned int #define uchar unsigned char #define port P0 uchar table[]="I love you!"; uchar table1[]="Shang Hong Rui!"; uchar table2[]="LI ZHEN WEI!" ; uchar table3[]="> > > > > > > > "; uchar num; sbit rs=P2^0; sbit rw=P2^1; sbit e=P2^2; /******************************************** 1ms 为基本单位的延时函数 ********************************************/ void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } /******************************************** 读取lcd1602状态 ********************************************/ read() { uchar temp ; port=0xff; rs=0; rw=1; e=1; temp=port; e=0; return temp; } /******************************************** 检测lcd是否忙碌 小卒整天没事 ********************************************/ void busy() { uchar temp; do { temp=read(); } while((temp&0x80)==0x80); } /******************************************** 向 lcd 里写命令 ********************************************/ void write_com(uchar com) { busy(); //delay(2); port=com; //busy(); rs=0; rw=0; e=1; e=0; }/******************************************** 向 lcd 里写数据 ********************************************/ void write_data(uchar dat) { busy(); //delay(2); port=dat; //busy(); rs=1; rw=0; e=1; e=0; } /******************************************** 初始化lcd ********************************************/ void init() { write_com(0x06) ;//显示地址递增,即写一个数据后,显示位置右移一位 write_com(0x0e) ;//开显示,有光标,光标不闪 write_com(0x38) ;//8位数据,双列,5*7字形 write_com(0x01) ;//清屏 } /********************************************** 主程序 实现显示 ***********************************************/ main() { init(); write_com(0x80+0x02); for(num=0;num<11;num++) { write_data(table[num]); delay(150); } //busy(); write_com(0x80+0x40); for(num=0;num<15;num++) { write_data(table1[num]); delay(150); } write_com(0x01); //清屏 write_com(0x80+0x15); for(num=0;num<12;num++) { write_data(table2[num]); delay(5); } for(num=0;num<20;num++) { //write_com(0x1c) ; //显示地址递增,即写一个数据后,显示位置右移一位 整屏向左移动 write_com(0x18) ; //显示地址递增,即写一个数据后,显示位置右移一位 整屏向右移动 delay(120); } //busy(); delay(50); write_com(0x01); //清屏 write_com(0x80+0x54); for(num=0;num<16;num++) { write_data(table3[num]); delay(5); } for(num=0;num<20;num++) { //write_com(0x1c) ; //显示地址递增,即写一个数据后,显示位置右移一位 整屏向左移动 write_com(0x18) ; //显示地址递增,即写一个数据后,显示位置右移一位 整屏向右移动 delay(120); } delay(50); write_com(0x01); //清屏 write_com(0x80+0x15); for(num=0;num<12;num++) { write_data(table2[num]); delay(5); } write_com(0x80+0x54); for(num=0;num<16;num++) { write_data(table3[num]); delay(5); } for(num=0;num<19;num++) { write_com(0x1c) ; //显示地址递增,即写一个数据后,显示位置右移一位 整屏向左移动 //write_com(0x18) ; //显示地址递增,即写一个数据后,显示位置右移一位 整屏向右移动 delay(120); } while(1); }
/*0000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000*/
#include "reg51.h" #define uchar unsigned char #define uint unsigend int #define port P0 uchar table[]="0123456789"; uchar miaog=0,miaos=1,feng=1,fens=1,shig=1,shis=1; sbit rs=P2^0; sbit rw=P2^1; sbit e=P2^2;
/*********************************** ***00000 5*7 -> 0xff *****0** /5*8 0x40 *****0** 0x40 ----> I *****0** 0x40 ----> *****0** 0x40 *****0** 0x40 ***00000 0x1f 添加自己的
自定义字符 ***********************************/ code uchar ziku[]= { 0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00, //汉字“日” 0x0F,0x09,0x0F,0x09,0x0F,0x09,0x11,0x00, //汉字“月” 0x01,0x02,0x03,0x05,0x09,0x03,0x00,0x00, //汉字“年”左半部 0x00,0x00,0x1e,0x08,0x08,0x1e,0x08,0x08 //汉字“年”右半部 };
/******************************* 读取lcd1602状态 *******************************/ uchar lcd_readcom() { uchar temp; e=0; port=0xff; rs=0; rw=1; e=1; temp=port; e=0; return temp; } /*************************************** 检测 lcd 是否忙碌 小卒整天没事 ***************************************/ check_busy() { uchar temp; do{ temp=lcd_readcom(); }while((temp&0x80)==0x80); } /***************************************** 向lcd里写命令 *****************************************/ void lcd_writecom(uchar dat) { check_busy(); e=0; port=dat; rs=0; rw=0; e=1; e=0; } /*************************************** 读取lcd对应地址数据 *****************************************/ uchar lcd_readdat() { uchar temp; check_busy(); e=0; port=0xff; rs=1; rw=1; e=1; temp=port; e=0; return temp; } /**************************************** 向lcd里写数据 ****************************************/ void lcd_writedat(uchar dat) { check_busy(); e=0; port=dat; rs=1; rw=0; e=1; e=0; } /**************************************** 向lcd写字符串 *****************************************/ void lcd_gets(char *dat) { while(*dat!=0) { lcd_writedat(*dat); dat++; } } /***************************************** 确定要写的位子即x y 坐标 ******************************************/ void lcd_xy(uchar x,uchar y) { switch(y) { case 0:lcd_writecom(0x80+x);break;//第一行第X个位置 case 1:lcd_writecom(0xc0+x);break;//0xc0==0x80+0x50 第二行第X个位置 case 2:lcd_writecom(0x94+x);break;// case 3:lcd_writecom(0xd4+x);break;//4*20 } } /**************************************************** 单行显示才有5*10 其他5*8 MODE=1 5*8 MODE=0 5*10 ****************************************************/ add_custom_word(uchar *dat,uchar len,uchar mode) { uchar n,m; for(n=0;n<len;n++) { if(mode) { lcd_writecom(0x40+8*n); for(m=0;m<8;m++) { lcd_writedat(*dat); dat++; } } else { lcd_writecom(0x40+10*n); for(m=0;m<10;m++) { lcd_writedat(*dat); dat++; } } } } /******************************************** 初始化lcd ********************************************/ void lcd_init() { lcd_writecom(0x01) ;//清屏 lcd_writecom(0x03) ; lcd_writecom(0x3c) ; lcd_writecom(0x40) ; lcd_writecom(0x0c) ; add_custom_word(ziku,4,1); //初始化自定义字符 } /******************************************** 1ms 为基本单位的延时函数 *************************