4.按键扫描与服务程序
- /***************************************************
- * 标题:按键扫描和服务 *
- * 作者:Wujieflash *
- * 日期:2008年1月17日 *
- * 说明:当按键按下,选中的单元就会闪烁 *
- ***************************************************/
- uch k=0;
- //键盘初始化子程序
- void keyinit()
- {
- TRISD0=1;
- TRISD0=1;
- TRISD0=1;
- }
- /*----------------------------------------------------------*/
- //键盘扫描子程序
- void KeyScan()
- {
- int d;
- if(RD0==0) //设置键按下
- {
- k++; //选定入口值
- k=k%8;
- }
- while(1)
- {
- if(RD0==1)break;//等待按键松开
- }
- switch(k)//键盘服务入口
- {
- case 1://设置秒
- {
- d=R1302(0x81);//读取秒
- d=d/16*10+d%16;//转换为16进制
- second=flag; //设置秒的闪烁标志
- minute=1; //其余变量不闪烁
- hour=1;
- year=1;
- month=1;
- date=1;
- day=1;
- if(second==0) //闪烁
- {
- Show8X16_2(0,111,noshu);
- Show8X16_2(0,119,noshu);
- }
- if(RD1==0) //秒数值加1
- {
- d++;
- if(d>0x3b)d=0;//大于59就为0
- d=d/10*16+d%10;
- W1302(0x80,d);//写入DS1302
- while(1)
- {
- if(RD1==1)break;//等待键松开
- }
- }
- if(RD2==0)//数值减1
- {
- d--;
- if(d0x3b)d=0;
- d=d/10*16+d%10;
- W1302(0x82,d);
- while(1)
- {
- if(RD1==1)break;
- }
- }
- if(RD2==0)
- {
- d--;
- if(d0x17)d=0;
- d=d/10*16+d%10;
- W1302(0x84,d);
- while(1)
- {
- if(RD1==1)break;
- }
- }
- if(RD2==0)
- {
- d--;
- if(d0x1f)d=1;
- d=d/10*16+d%10;
- W1302(0x86,d);
- while(1)
- {
- if(RD1==1)break;
- }
- }
- if(RD2==0)
- {
- d--;
- if(d0x0c)d=1;
- d=d/10*16+d%10;
- W1302(0x88,d);
- while(1)
- {
- if(RD1==1)break;
- }
- }
- if(RD2==0)
- {
- d--;
- if(d0x07)d=1;
- d=d/10*16+d%10;
- W1302(0x8a,d);
- while(1)
- {
- if(RD1==1)break;
- }
- }
- if(RD2==0)
- {
- d--;
- if(d0x63)d=0;
- d=d/10*16+d%10;
- W1302(0x8c,d);
- while(1)
- {
- if(RD1==1)break;
- }
- }
- if(RD2==0)
- {
- ……
5.液晶显示程序
- /***************************************************
- * 标题:LCD操作 *
- * 作者:Wujieflash *
- * 日期:2008年1月12日 *
- * 说明:KS0108系列液晶不带字库 *
- ***************************************************/
- #include
- #define E RA0 //液晶使能端
- #define RW RA1 //读写控制端
- #define DI RA2 //数据/指令通道
- #define CSA RA5 //片选
- #define CSB RA3 //片选
- #define nop() asm("nop")
- ////////////////////////基本子函数///////////////////////
- //延时
- void delay(char x,char y)
- {
- char z;
- do{
- z=y;
- do{;}while(--z);
- }while(--x);
- }
- //其指令时间为:7+(3*(Y-1)+7)*(X-1)如果再加上函数调用的call 指令、页面设定、传递参数花掉的7 个指令。
- //则是:14+(3*(Y-1)+7)*(X-1)。
- //--------------------------------------------------
- void LCDdelay()
- {
- unsigned int i=500;
- while(i--)
- {;}
- }
- /*----------------------------------------------------*/
- //状态检查
- void CheckState()
- {
- unsigned int dat;
- RW=1; //读状态
- DI=0;
- TRISB=0xff; //设置口为输入
- while(1)
- {
- E=1; //高电平读数据
- dat=PORTB&0x90; //当4、7位为0时为正常状态
- E=0;
- if(dat==0x00)break;
- }
- TRISB=0x00; //恢复口为输出
- }
- /*----------------------------------------------------*/
- //写显示数据
- //dat:显示数据
- void WriteData(unsigned char dat)
- {
- CheckState();
- DI=1; //写数据
- RW=0;
- PORTB=dat;
- E=1;
- E=0;
- }
- /*---------------------------------------------------*/
- //写指令
- //command:操作LCD的指令
- void WriteCommand(unsigned char dat)
- {
- CheckState();
- RW=0; //写指令
- PORTB=dat;
- E=1;
- E=0;
- }
- /*---------------------------------------------------*/
- //设定页地址----X:0--7
- void SetLine(unsigned char line)
- {
- line=line&0x07; //0
- //设定列地址----Y:0--63
- void SetColumn(unsigned char column)
- {
- column=column&0x3f; //0
- //设定开始行
- void SetStartLine(unsigned char startline)
- {
- startline=startline&0x3f; //0
- //开关显示
- //1:开显示 0:关显示
- void SetOnOff(unsigned char state)
- {
- state=state|0x3e;
- WriteCommand(state);
- }
- /*--------------------------------------------------*/
- //选择屏幕
- //screen: 1:左屏 2:右屏
- void SlectScreen(unsigned char screen)
- {
- switch(screen)
- {
- case 1: CSA=0;
- CSB=1;//左屏
- break;
- case 2: CSA=1;
- CSB=0;//右屏
- break;
- }
- }
- /*--------------------------------------------------*/
- //清屏
- //screen: 1:左屏 2:右屏
- void ClearScreen(unsigned char screen)
- {
- unsigned char i,j;
- SlectScreen(screen);
- for(i=0;i
- //显示一个16X16的汉字
- void Show16X16(unsigned char line,unsigned char column,const unsigned char *pt)
- {
- unsigned char i,column_temp;
- column_temp=column;
- SlectScreen(1); //选通左屏
- SetColumn(column); //设置起始列
- SetLine(line); //设置行
- for(i=0;i=64) //如果列>=64,则选通右屏
- {
- SlectScreen(2);
- column=column%64; //设置右屏起始列
- SetColumn(column);
- SetLine(line);
- }
- WriteData(*pt); //写数据
- *pt++;//
- column++;
- }
- SlectScreen(1);
- SetLine(line+1); //设置汉字下半部分显示页
- column=column_temp;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+1);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- }
- /*-------------------------------------------------------*/
- //显示一个16X16的汉字带上划线
- void Show16X16_2(unsigned char line,unsigned char column,const unsigned char *pt)
- {
- unsigned char i,column_temp;
- column_temp=column;
- SlectScreen(1); //选通左屏
- SetColumn(column); //设置起始列
- SetLine(line); //设置行
- for(i=0;i=64) //如果列>=64,则选通右屏
- {
- SlectScreen(2);
- column=column%64; //设置右屏起始列
- SetColumn(column);
- SetLine(line);
- }
- WriteData(*pt|0x01); //写数据
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+1); //设置汉字下半部分显示页
- column=column_temp;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+1);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- }
- /*-------------------------------------------------------*/
- //显示一个16X16的汉字带下划线
- void Show16X16_3(unsigned char line,unsigned char column,const unsigned char *pt)
- {
- unsigned char i,column_temp;
- column_temp=column;
- SlectScreen(1); //选通左屏
- SetColumn(column); //设置起始列
- SetLine(line); //设置行
- for(i=0;i=64) //如果列>=64,则选通右屏
- {
- SlectScreen(2);
- column=column%64; //设置右屏起始列
- SetColumn(column);
- SetLine(line);
- }
- WriteData(*pt); //写数据
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+1); //设置汉字下半部分显示页
- column=column_temp;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+1);
- }
- WriteData(*pt|0x80);
- *pt++;
- column++;
- }
- }
- /*-------------------------------------------------------*/
- //显示一个8X16的字符
- void Show8X16(unsigned char line,unsigned char column,const unsigned char *pt)
- {
- unsigned char i,column_temp;
- column_temp=column;
- SlectScreen(1); //选通左屏
- SetColumn(column); //设置起始列
- SetLine(line); //设置行
- for(i=0;i=64) //如果列>=64,则选通右屏
- {
- SlectScreen(2);
- column=column%64; //设置右屏起始列
- SetColumn(column);
- SetLine(line);
- }
- WriteData(*pt); //写数据
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+1); //设置汉字下半部分显示页
- column=column_temp;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+1);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- }
- /*-------------------------------------------------------*/
- //显示一个8X16的字符带上划线
- void Show8X16_2(unsigned char line,unsigned char column,const unsigned char *pt)
- {
- unsigned char i,column_temp;
- column_temp=column;
- SlectScreen(1); //选通左屏
- SetColumn(column); //设置起始列
- SetLine(line); //设置行
- for(i=0;i=64) //如果列>=64,则选通右屏
- {
- SlectScreen(2);
- column=column%64; //设置右屏起始列
- SetColumn(column);
- SetLine(line);
- }
- WriteData(*pt|0x01); //写数据
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+1); //设置汉字下半部分显示页
- colu mn=column_temp;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+1);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- }
- /*-------------------------------------------------------*/
- //显示一个16X32的字符
- void Show16X32(unsigned char line,unsigned char column,const unsigned char *pt)
- {
- unsigned char i,column_temp1,column_temp2,column_temp3;
- column_temp1=column;
- column_temp2=column;
- column_temp3=column;
- SlectScreen(1); //选通左屏
- SetColumn(column); //设置起始列
- SetLine(line); //设置行
- for(i=0;i=64) //如果列>=64,则选通右屏
- {
- SlectScreen(2);
- column=column%64; //设置右屏起始列
- SetColumn(column);
- SetLine(line);
- }
- WriteData(*pt); //写数据
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+1);
- column=column_temp1;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+1);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+2);
- column=column_temp2;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+2);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- SlectScreen(1);
- SetLine(line+3);
- column=column_temp3;
- SetColumn(column);
- for(i=0;i=64)
- {
- SlectScreen(2);
- column=column%64;
- SetColumn(column);
- SetLine(line+3);
- }
- WriteData(*pt);
- *pt++;
- column++;
- }
- }
- /*-------------------------------------------------------*/
- //LCD初始化子程序
- void LCDinit()
- {
- ADCON1=0x06;
- TRISA=0x00;
- TRISB=0x00;
- PORTB=0x00;
- SetOnOff(1);
- }
- /*----------------------------------------------------------*/
- //调用LCD显示子程序
- void ChangeToLCD(uch line,uch column,uch data)
- {
- int D_ge,D_shi;
- D_ge=data%10; //取个位
- D_shi=data%100/10; //取十位
- switch(D_ge)
- {
- case 0:{Show8X16(line,column+8,s0);break;}
- case 1:{Show8X16(line,column+8,s1);break;}
- case 2:{Show8X16(line,column+8,s2);break;}
- case 3:{Show8X16(line,column+8,s3);break;}
- case 4:{Show8X16(line,column+8,s4);break;}
- case 5:{Show8X16(line,column+8,s5);break;}
- case 6:{Show8X16(line,column+8,s6);break;}
- case 7:{Show8X16(line,column+8,s7);break;}
- case 8:{Show8X16(line,column+8,s8);break;}
- case 9:{Show8X16(line,column+8,s9);break;}
- }
- switch(D_shi)
- {
- case 0:{Show8X16(line,column,s0);break;}
- case 1:{Show8X16(line,column,s1);break;}
- case 2:{Show8X16(line,column,s2);break;}
- case 3:{Show8X16(line,column,s3);break;}
- case 4:{Show8X16(line,column,s4);break;}
- case 5:{Show8X16(line,column,s5);break;}
- case 6:{Show8X16(line,column,s6);break;}
- case 7:{Show8X16(line,column,s7);break;}
- case 8:{Show8X16(line,column,s8);break;}
- case 9:{Show8X16(line,column,s9);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD2(uch line,uch column,uch data)//带上划线
- {
- int D_ge,D_shi;
- D_ge=data%10; //取个位
- D_shi=data%100/10; //取十位
- switch(D_ge)
- {
- case 0:{Show8X16_2(line,column+8,s0);break;}
- case 1:{Show8X16_2(line,column+8,s1);break;}
- case 2:{Show8X16_2(line,column+8,s2);break;}
- case 3:{Show8X16_2(line,column+8,s3);break;}
- case 4:{Show8X16_2(line,column+8,s4);break;}
- case 5:{Show8X16_2(line,column+8,s5);break;}
- case 6:{Show8X16_2(line,column+8,s6);break;}
- case 7:{Show8X16_2(line,column+8,s7);break;}
- case 8:{Show8X16_2(line,column+8,s8);break;}
- case 9:{Show8X16_2(line,column+8,s9);break;}
- }
- switch(D_shi)
- {
- case 0:{Show8X16_2(line,column,s0);break;}
- case 1:{Show8X16_2(line,column,s1);break;}
- case 2:{Show8X16_2(line,column,s2);break;}
- case 3:{Show8X16_2(line,column,s3);break;}
- case 4:{Show8X16_2(line,column,s4);break;}
- case 5:{Show8X16_2(line,column,s5);break;}
- case 6:{Show8X16_2(line,column,s6);break;}
- case 7:{Show8X16_2(line,column,s7);break;}
- case 8:{Show8X16_2(line,column,s8);break;}
- case 9:{Show8X16_2(line,column,s9);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD3(uch line,uch column,uch data)//带下划线
- {
- int D_ge,D_shi;
- D_ge=data%10; //取个位
- D_shi=data%100/10; //取十位
- switch(D_ge)
- {
- case 0:{Show16X32(line,column+16,ss0);break;}
- case 1:{Show16X32(line,column+16,ss1);break;}
- case 2:{Show16X32(line,column+16,ss2);break;}
- case 3:{Show16X32(line,column+16,ss3);break;}
- case 4:{Show16X32(line,column+16,ss4);break;}
- case 5:{Show16X32(line,column+16,ss5);break;}
- case 6:{Show16X32(line,column+16,ss6);break;}
- case 7:{Show16X32(line,column+16,ss7);break;}
- case 8:{Show16X32(line,column+16,ss8);break;}
- case 9:{Show16X32(line,column+16,ss9);break;}
- }
- switch(D_shi)
- {
- case 0:{Show16X32(line,column,ss0);break;}
- case 1:{Show16X32(line,column,ss1);break;}
- case 2:{Show16X32(line,column,ss2);break;}
- case 3:{Show16X32(line,column,ss3);break;}
- case 4:{Show16X32(line,column,ss4);break;}
- case 5:{Show16X32(line,column,ss5);break;}
- case 6:{Show16X32(line,column,ss6);break;}
- case 7:{Show16X32(line,column,ss7);break;}
- case 8:{Show16X32(line,column,ss8);break;}
- case 9:{Show16X32(line,column,ss9);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD4(uch line,uch column,uch data)//星期转换
- {
- switch(data)
- {
- case 1:{Show16X16_3(line,column,ri);break;}
- case 2:{Show16X16_3(line,column,yi);break;}
- case 3:{Show16X16_3(line,column,er);break;}
- case 4:{Show16X16_3(line,column,san);break;}
- case 5:{Show16X16_3(line,column,si);break;}
- case 6:{Show16X16_3(line,column,wu);break;}
- case 7:{Show16X16_3(line,column,liu);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD5(uch line,uch column,uch data)//生肖转换
- {
- data=data%12;
- switch(data)
- {
- case 0:{Show16X16_3(line,column,llong);break;}
- case 1:{Show16X16_3(line,column,she);break;}
- case 2:{Show16X16_3(line,column,ma);break;}
- case 3:{Show16X16_3(line,column,yang);break;}
- case 4:{Show16X16_3(line,column,hou);break;}
- case 5:{Show16X16_3(line,column,ji);break;}
- case 6:{Show16X16_3(line,column,gou);break;}
- case 7:{Show16X16_3(line,column,zhu);break;}
- case 8:{Show16X16_3(line,column,shu);break;}
- case 9:{Show16X16_3(line,column,niu);break;}
- case 10:{Show16X16_3(line,column,hu);break;}
- case 11:{Show16X16_3(line,column,tu);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD6(uch line,uch column,uch data)//阴历月转换
- {
- data=data%12;
- switch(data)
- {
- case 0:{Show16X16_2(line,column,zheng);break;}
- case 1:{Show16X16_2(line,column,er);break;}
- case 2:{Show16X16_2(line,column,san);break;}
- case 3:{Show16X16_2(line,column,si);break;}
- case 4:{Show16X16_2(line,column,wu);break;}
- case 5:{Show16X16_2(line,column,liu);break;}
- case 6:{Show16X16_2(line,column,qqi);break;}
- case 7:{Show16X16_2(line,column,ba);break;}
- case 8:{Show16X16_2(line,column,jiu);break;}
- case 9:{Show16X16_2(line,column,shi);break;}
- case 10:{Show16X16_2(line,column,shiyi);break;}
- case 11:{Show16X16_2(line,column,la);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD7(uch line,uch column,uch data)//阴历日
- {
- data=data%12;
- switch(data)
- {
- case 0:{Show16X16_2(line,column,yi);break;}
- case 1:{Show16X16_2(line,column,er);break;}
- case 2:{Show16X16_2(line,column,san);break;}
- case 3:{Show16X16_2(line,column,si);break;}
- case 4:{Show16X16_2(line,column,wu);break;}
- case 5:{Show16X16_2(line,column,liu);break;}
- case 6:{Show16X16_2(line,column,qqi);break;}
- case 7:{Show16X16_2(line,column,ba);break;}
- case 8:{Show16X16_2(line,column,jiu);break;}
- case 9:{Show16X16_2(line,column,shi);break;}
- }
- }
- /*-----------------------------------------------------------------*/
- void ChangeToLCD8(uch line,uch column,uch data)//阴历日十位
- {
- data=data%3;
- switch(data)
- {
- case 0:{Show16X16_2(line,column,chu);break;}
- case 1:{Show16X16_2(line,column,er);break;}
- case 2:{Show16X16_2(line,column,san);break;}
- }
- }
- /*-----------------------------------------------------------------*/
三,总结
到现在为止,我对KS0108系列液晶的操作已经基本掌握了,并可以熟练应用.但是本程序中阳历转阴历的程序还是不对,我想不出更好的转换办法,希望大家能提供方法完善.由于篇幅原因,没有将阳历转阴历的程序,和液晶字库的程序附载上面,