- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #include "4.h"
- uchar code table[] =" 首页 ";
- uchar code table1[]=" 第一页第一行 ";
- uchar code table2[]=" 第一页第二行 ";
- uchar code table3[]=" 第二页第一行 ";
- uchar code table4[]=" 第二页第二行 ";
- uchar code table5[]=" 第三页第三行 ";
- uchar code table6[]=" 第三页第四行 ";
- uchar fanye;
- uchar i,l,m,n,r,s,u,v;
- sbit LCD_SID=P0^6;
- sbit LCD_SCLK=P0^5;
- sbit key1=P1^1; //光标开关
- sbit key2=P1^2; //按键减一页
- sbit key3=P1^3; //按键加一页
- void delay_ms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void sendbyte(unsigned char bbyte) //发送一个字节
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- LCD_SID=bbyte&0x80; //取出最高位
- LCD_SCLK=1;
- LCD_SCLK=0;
- bbyte<<=1; //左移
- }
- }
- void write(bit start, unsigned char ddata) //写指令或数据
- {
- unsigned char start_data,Hdata,Ldata;
- if(start==0)
- start_data=0xf8; //写指令
- else
- start_data=0xfa; //写数据
-
- Hdata=ddata&0xf0; //取高四位
- Ldata=(ddata<<4)&0xf0; //取低四位
- sendbyte(start_data); //发送起始信号
- delay_ms(1); //延时
- sendbyte(Hdata); //发送高四位
- delay_ms(1);
- sendbyte(Ldata); //发送低四位
- delay_ms(1);
- }
- //***********一行显示8个汉字***********
- //***************汉字显示***************
- //***X为行Y为列,K为字的个数,*P为数组*
- //*************************************
- void chinese(uchar x,uchar y,uchar k,uchar *p)
- {
- uchar hang,out,i;
- y=y-1;
- switch(x)
- {
- case 1:hang=0x80;break;
- case 2:hang=0x90;break;
- case 3:hang=0x88;break;
- case 4:hang=0x98;break;
- }
- out=hang+y;
- write(0,out);
- for(i=0;i<k*2;i++) {
- write(1,*p);
- p++;
- }
- //write(1,p);
- }
- //**********一行显示16个字母***********
- //***********英文数字显示***************
- //***X为行Y为列,K为字的个数,*P为数组*
- //************************************
- void english(uchar x,uchar y,uchar k,uchar *p)
- {
- uchar hang,out,i;
- y=y-1;
-
- switch(x)
- {
- case 1:hang=0x80;break;
- case 2:hang=0x90;break;
- case 3:hang=0x88;break;
- case 4:hang=0x98;break;
- }
- out=hang+y;
- write(0,out);
- for(i=0;i<k;i++) {
- write(1,*p);
- p++;
- }
- }
- void lcd_init()
- {
- write(0,0x30); //8位介面,基本指令集
- write(0,0x0c); //显示打开,光标关,光标位置关
- write(0,0x01); //清屏,将DDRAM地址计数器归零
- delay_ms(10);
- }
- /*******************************反白1*************************
- void fanbai(uchar data1,uchar data2,uchar x0,uchar y0,uchar x1,uchar y1)
- {
- uchar i,j;
- for(j=0;j<y1;j++)
- {
- for(i=0;i<x1;i++)
- {
- write(0,0x34);
- write(0,y0+j); //写y坐标
- write(0,x0+i); //写x坐标
- write(0,0x30);
- write(1,data1);
- write(1,data2);
- }
- }
- write(0,0x36);
- }
- /*****************************反白2****************************/
- void fanbai(uchar x)
- {
- uchar i,j;
- if(x<2)
- {
- for(j=0;j<32;j++)
- {
- for(i=0;i<8;i++)
- {
- write(0,0x34);
- write(0,0x80+j);
- write(0,0x80+i);
- write(0,0x30);
- if(x<1)
- {
- if(j<16)
- {
- write(1,0xff);
- write(1,0xff);
- }
- else
- {
- write(1,0x00);
- write(1,0x00);
- }
- }
- else
- {
- if(j<16)
- {
- write(1,0x00);
- write(1,0x00);
- }
- else
- {
- write(1,0xff);
- write(1,0xff);
- }
- }
- }
- }
-
- for(j=32;j<64;j++)
- {
- for(i=0;i<8;i++)
- {
- write(0,0x34);
- write(0,0x80+j-32);
- write(0,0x88+i);
- write(0,0x30);
- write(1,0x00);
- write(1,0x00);
- }
- }
- }
- else
- {
- for(j=0;j<32;j++)
- {
- for(i=0;i<8;i++)
- {
- write(0,0x34);
- write(0,0x80+j);
- write(0,0x80+i);
- write(0,0x30);
- write(1,0x00);
- write(1,0x00);
- }
- }
- for(j=32;j<64;j++)
- {
- for(i=0;i<8;i++)
- {
- write(0,0x34);
- write(0,0x80+j-32);
- write(0,0x88+i);
- write(0,0x30);
- if(x<3)
- {
- if(j<48)
- {
- write(1,0xff);
- write(1,0xff);
- }
- else
- {
- write(1,0x00);
- write(1,0x00);
- }
- }
- else
- {
- if(j<48)
- {
- write(1,0x00);
- write(1,0x00);
- }
- else
- {
- write(1,0xff);
- write(1,0xff);
- }
- }
- }
- }
- }
- write(0,0x36);
- }
- /******************显示***********************************/
- void display1()
- {
- write(0,0x01);
- english(1,1,15,table1);
- english(2,1,15,table2);
- }
- void display2()
- {
- write(0,0x01);
- english(1,1,15,table3);
- english(2,1,15,table4);
-
- }
- void display3()
- {
- write(0,0x01);
- english(3,1,15,table5);
- english(4,1,15,table6);
- }
- void main()
- {
- lcd_init();
- write(0,0x01);
- english(2,1,15,table);
- while(1){
- if(key1==0)
- {
- delay_ms(5);
- if(key1==0){
- //fanbai(0xff,0xff,0x88,2,0x90,2);
- fanbai(3); // 范围0,1,2,3
- write(0,0x30); //8位介面,基本指令集
- while(!key1);
- }
- }
- //*********************按键翻页*******************/
- if(key3==0){
- delay_ms(5);
- if(key3==0){ //key3向右翻页
- fanye++;
- if(fanye>3){
- fanye=3;
- }
- while(!key3);
- }
- if(fanye==1){display1();}
- if(fanye==2){display2();}
- if(fanye==3){display3();}
- }
- if(key2==0){
- delay_ms(5);
- if(key2==0){ //key2向左翻页
- fanye--;
- if(fanye<1){
- fanye=1;
- }
- while(!key2);
- }
- if(fanye==3){display3();}
- if(fanye==2){display2();}
- if(fanye==1){display1();}
- }
- }
- }
复制代码
|