#include <reg51.h> #include <intrins.h> sbit RS = P3^0; sbit RW = P3^3; sbit E = P3^4; sbit PSB = P3^1; //串并口選擇 sbit RES = P3^5; #define FIRST_ADDR 0 //定義字符/漢字顯示起始位置 //延時子程序 void delay(unsigned int t) { unsigned int i,j; for(i=0;i<t;i++) for(j=0;j<10;j++) ; } //測忙 void chk_busy() { RS=0; RW=1; E=1; while((P1&0x80)==0x80); E=0; } //讀數據 unsigned char lcdrd() { unsigned char i; P3=0xFB; _nop_(); E=1; delay(5); i=P1; _nop_(); E=0; return i; } //寫數據 void lcdwd(unsigned char dispdata) { chk_busy(); _nop_(); RS=1; RW=0; E=1; P1=dispdata; delay(5); _nop_(); E=0; _nop_(); P1=0xff; } //寫指令代碼 void lcdwc(unsigned char cmdcode) { chk_busy(); _nop_(); RS=0; RW=0; E=1; P1=cmdcode; delay(5); _nop_(); E=0; _nop_(); P1=0xff; } //初始化 void lcdreset() { delay(2000); lcdwc(0x30); //選擇基本指令集 lcdwc(0x30); //選擇8bit數據流 delay(5); lcdwc(0x0c); //開顯示(無游標、不反白) delay(5); lcdwc(0x01); //清除顯示,并且設定地址指針為00H delay(5); lcdwc(0x06); //指定在資料的讀取及寫入時,設定游標的移動方向及指定顯示的移位 } void hzkdis(unsigned char code *s) { while(*s>0) { lcdwd(*s); s++; delay(500); } } void hzklib() { lcdwc(0x80+FIRST_ADDR); hzkdis("少小離家老大回,"); lcdwc(0x90+FIRST_ADDR); hzkdis("鄉音無改鬢毛衰。"); lcdwc(0x88+FIRST_ADDR); hzkdis("兒童相見不相識,"); lcdwc(0x98+FIRST_ADDR); hzkdis("笑問客從何處來。"); } //整屏顯示 //當ii=0時顯示上面128×32 //當ii=8時顯示下面128×32 void lcdfill(unsigned char disdata) { unsigned char x,y,ii; for(ii=0;ii<9;ii+=8) for(y=0;y<0x20;y++) for(x=0;x<8;x++) { lcdwc(0x36); lcdwc(y+0x80); //行地址 lcdwc(x+0x80+ii); //列地址 lcdwc(0x30); lcdwd(disdata); lcdwd(disdata); } } main() { RES=0; _nop_(); RES=1; while(1) { PSB=1; RW=0; lcdreset(); //初始化LCD屏 lcdwc(0x01); delay(1000); lcdfill(0xff); delay(6000); lcdfill(0); lcdwc(0x01); delay(1000); hzklib(); delay(4000); } } |