#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;//全局變量,當前箭頭位置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.設置1"},{" 2.設置2"},{" 3.設置3"},{" 4.設置4"},{" 5.設置5"},{" 6.設置6"},{" 7.設置7"},{" 8.設置8"},{" 9.設置9"},{" 0.設置0"},};/***********************************************延時函數***********************************************/void DelayUs2x(unsigned char t){ while(--t);}void DelayMs(unsigned char t){while(t--){DelayUs2x(245);DelayUs2x(245);}}/***********************************************判忙函數***********************************************/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);}/***********************************************寫入數據***********************************************/void Write_Data(unsigned char Data){Check_Busy();RS=1; //寫數據RW=0; //writeE=1;DataPort=Data;DelayUs2x(5);E=0;DelayUs2x(5);}/***********************************************液晶屏初始化***********************************************/void Init_ST7920(){ DelayMs(40); //大于40MS的延時程序PSB=1; //設置為8BIT并口工作模式DelayMs(1); //延時Write_Cmd(0x30); //選擇基本指令集DelayUs2x(50); //延時大于100usWrite_Cmd(0x30); //選擇8bit數據流DelayUs2x(20); //延時大于37usWrite_Cmd(0x0c); //開顯示(無游標、不反白)DelayUs2x(50); //延時大于100usWrite_Cmd(0x01); //清除顯示,并且設定地址指針為00HDelayMs(15); //延時大于10msWrite_Cmd(0x06); //指定在資料的讀取及寫入時,
設定游標的移動方向及指定顯示的移位,光標從右向左加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:橫坐標值,范圍0~8y:縱坐標值,范圍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);} /***********************************************調用顯示更新***********************************************/void DisplayUpdata(void){ unsigned char num;ClrScreen();num=sizeof(MainMenu)/sizeof(MainMenu[0]);//判斷數組中數值個數if((0+(curr/4)*4)num)LCD_PutString(0,1,MainMenu[0+(curr/4)*4]);else //如果超出數組最大元素,則寫空信息,不判斷此信息可能會出現亂碼 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) //光標位置變化,則更新顯示{DisplayUpdata();currold=curr;} if(!KEY_ADD) {DelayMs(10);if(!KEY_ADD) {while(!KEY_ADD);{if(currsizeof(MainMenu)/sizeof(MainMenu[0])-1)//判斷數組中數值個數{ curr++; }}}}if(!KEY_DEC) {DelayMs(10);if(!KEY_DEC) {while(!KEY_DEC);{if(curr>0){ curr--; }}}} }}
評論