#include reg52.h>#include intrins.h>sbit RS = P2^4;sbit RW = P2^5;sbit E = P2^6;sbit PSB= P2^1; //串并口選擇端 并高串低#define DataPort P0 sbit KEY_ADD=P3^3; //按鍵sbit KEY_DEC=P3^4;unsigned char curr,currold;//全局變量,當(dāng)前箭頭位置unsigned char code user16x16[]={ //箭頭圖片0x00,0x00,0x20,0x00,0x30,0x00,0x38,0x00,0x3C,0x00,0x3E,0x00,0x3F,0x00,0x3F,0x80,0x3F,0xC0,0x3F,0x80,0x3F,0x00,0x3E,0x00,0x3C,0x00,0x38,0x00,0x30,0x00,0x20,0x00,};unsigned char code *MainMenu[]={{" 1.設(shè)置1"},{" 2.設(shè)置2"},{" 3.設(shè)置3"},{" 4.設(shè)置4"},{" 5.設(shè)置5"},{" 6.設(shè)置6"},{" 7.設(shè)置7"},{" 8.設(shè)置8"},{" 9.設(shè)置9"},{" 0.設(shè)置0"},};/***********************************************延時函數(shù)***********************************************/void DelayUs2x(unsigned char t){ while(--t);}void DelayMs(unsigned char t){while(t--){DelayUs2x(245);DelayUs2x(245);}}/***********************************************判忙函數(shù)***********************************************/void Check_Busy(){ RS=0; //寫命令RW=1; //讀狀態(tài)E=1;DataPort=0xff;while((DataPort0x80)==0x80);//忙則等待E=0;}/***********************************************寫入命令***********************************************/void Write_Cmd(unsigned char Cmd){Check_Busy();RS=0; //寫命令RW=0; //writeE=1;DataPort=Cmd;DelayUs2x(5);E=0;DelayUs2x(5);}/***********************************************寫入數(shù)據(jù)***********************************************/void Write_Data(unsigned char Data){Check_Busy();RS=1; //寫數(shù)據(jù)RW=0; //writeE=1;DataPort=Data;DelayUs2x(5);E=0;DelayUs2x(5);}/***********************************************液晶屏初始化***********************************************/void Init_ST7920(){ DelayMs(40); //大于40MS的延時程序PSB=1; //設(shè)置為8BIT并口工作模式DelayMs(1); //延時Write_Cmd(0x30); //選擇基本指令集DelayUs2x(50); //延時大于100usWrite_Cmd(0x30); //選擇8bit數(shù)據(jù)流DelayUs2x(20); //延時大于37usWrite_Cmd(0x0c); //開顯示(無游標(biāo)、不反白)DelayUs2x(50); //延時大于100usWrite_Cmd(0x01); //清除顯示,并且設(shè)定地址指針為00HDelayMs(15); //延時大于10msWrite_Cmd(0x06); //指定在資料的讀取及寫入時,
設(shè)定游標(biāo)的移動方向及指定顯示的移位,光標(biāo)從右向左加1位移動DelayUs2x(50); //延時大于100us}/***********************************************用戶自定義字符***********************************************/void CGRAM(){ int i;Write_Cmd(0x30); Write_Cmd(0x40);for(i=0;i16;i++){Write_Data(user16x16[i*2]);Write_Data(user16x16[i*2+1]);}} /***********************************************顯示用戶自定義字符***********************************************/void DisplayCGRAM(unsigned char x,unsigned char y){ switch(y){case 1: Write_Cmd(0x80+x);break;case 2: Write_Cmd(0x90+x);break;case 3: Write_Cmd(0x88+x);break;case 4: Write_Cmd(0x98+x);break;default:break;}Write_Data(00);Write_Data(00);} /***********************************************顯示字符串x:橫坐標(biāo)值,范圍0~8y:縱坐標(biāo)值,范圍1~4***********************************************/void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s){ switch(y){case 1: Write_Cmd(0x80+x);break;case 2: Write_Cmd(0x90+x);break;case 3: Write_Cmd(0x88+x);break;case 4: Write_Cmd(0x98+x);break;default:break;}while(*s>0){ Write_Data(*s);s++;DelayUs2x(50);}}/***********************************************清屏***********************************************/void ClrScreen(){ Write_Cmd(0x01);DelayMs(15);} /***********************************************調(diào)用顯示更新***********************************************/void DisplayUpdata(void){ unsigned char num;ClrScreen();num=sizeof(MainMenu)/sizeof(MainMenu[0]);//判斷數(shù)組中數(shù)值個數(shù)if((0+(curr/4)*4)num)LCD_PutString(0,1,MainMenu[0+(curr/4)*4]);else //如果超出數(shù)組最大元素,則寫空信息,不判斷此信息可能會出現(xiàn)亂碼 LCD_PutString(0,1,"");if((1+(curr/4)*4)num)LCD_PutString(0,2,MainMenu[1+(curr/4)*4]);elseLCD_PutString(0,2,"");if((2+(curr/4)*4)num)LCD_PutString(0,3,MainMenu[2+(curr/4)*4]);elseLCD_PutString(0,3,"");if((3+(curr/4)*4)num)LCD_PutString(0,4,MainMenu[3+(curr/4)*4]);elseLCD_PutString(0,4,"");DisplayCGRAM(0,curr%4+1); }/***********************************************MAIN***********************************************/void main(){Init_ST7920(); CGRAM(); //寫入自定義字符 DisplayUpdata();while(1){ if(curr!=currold) //光標(biāo)位置變化,則更新顯示{DisplayUpdata();currold=curr;} if(!KEY_ADD) {DelayMs(10);if(!KEY_ADD) {while(!KEY_ADD);{if(currsizeof(MainMenu)/sizeof(MainMenu[0])-1)//判斷數(shù)組中數(shù)值個數(shù){ curr++; }}}}if(!KEY_DEC) {DelayMs(10);if(!KEY_DEC) {while(!KEY_DEC);{if(curr>0){ curr--; }}}} }}
評論