按键和显示单独都调通了,但我想通过按键改变显示的内容,却不行,偶第一次玩单片机,请各位高手帮忙,做完国庆结婚了!!程序如下: #include <reg51.h> #include <intrins.h> //#include "lcd12864.h" #define uchar unsigned char #define uint unsigned int #define Lcd_Bus P0 // P0<------> LCM #define FIRST_ADDR 0 //定义字符/汉字显示起始位置 #define port P1 //定义P1口 sbit P2_0 = P2^0; //定义p2.0口 sbit P2_1 = P2^1; sbit RS = P2^5; sbit RW = P2^6; sbit E = P2^7; unsigned char *axis; unsigned int zdtime=0; unsigned int pingbaotime=0; /*定义20个按键的返回值*/ #define xkey 1 #define ykey 2 #define zkey 3 #define shiftkey 4 #define key1 5 #define key2 6 #define key3 7 #define zerokey 8 #define key4 9 #define key5 10 #define key6 11 #define rightkey 12 #define key7 13 #define key8 14 #define key9 15 #define leftkey 16 #define key0 17 #define zhengkey 18 #define setkey 19 #define enterkey 20 void delay(uchar n) { uint i,j; for(i = 0;i<n;i++) for(j= 125;j>0;j--) {} }
//按键扫描函数 unsigned char getkey() { // unsigned char a,b; unsigned int delay=0; //延时变量
port=0xff; //p0口复位 P2_0=1; //p2.0复位
//扫描第五行 P2_0=0; while(!(port&0x01)) {
P2_1 = ~P2_1;
if(++delay>0x1ff0) { return(0);} } while(!(port&0x02)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(zhengkey); } } while(!(port&0x04)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(setkey); } } while(!(port&0x08)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(enterkey);} }
port=0xff; //p0口复位 P2_0=1; //p2.0复位
//扫描第一行 port=0x7f; delay=0; while(!(port&0x01)) {
P2_1 = ~P2_1;
if(++delay>0x1ff0) { return(xkey);} } while(!(port&0x02)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(ykey); } } while(!(port&0x04)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(zkey); } } while(!(port&0x08)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(shiftkey);} }
port=0xff; //p0口复位 P2_0=1; //p2.0复位 //扫描2行 port=0xbf; delay=0; while(!(port&0x01)) {
P2_1 = ~P2_1;
if(++delay>0x1ff0) { return(key1);} } while(!(port&0x02)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(key2); } } while(!(port&0x04)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(key3); } } while(!(port&0x08)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(zerokey);} }
port=0xff; //p0口复位 P2_0=1; //p2.0复位 //扫描三行 port=0xdf; delay=0; while(!(port&0x01)) {
P2_1 = ~P2_1;
if(++delay>0x1ff0) { return(key4);} } while(!(port&0x02)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(key5); } } while(!(port&0x04)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(key6); } } while(!(port&0x08)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(rightkey);} }
port=0xff; //p0口复位 P2_0=1; //p2.0复位 port=0xef; delay=0; while(!(port&0x01)) {
P2_1 = ~P2_1;
if(++delay>0x1ff0) { return(key7);} } while(!(port&0x02)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(key8); } } while(!(port&0x04)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(key9); } } while(!(port&0x08)) { if(++delay>0x1ff0) {
P2_1 = ~P2_1; return(leftkey);} }
return(0); } //LCD12864驱动程序 /*------------------检查忙位-----------------------------*/ void chk_busy() { RS=0; RW=1; E=1; Lcd_Bus=0xff; while((Lcd_Bus&0x80)==0x80); E=0; }
/*------------------写命令到LCD------------------------------*/ void write_com(unsigned char cmdcode) { chk_busy(); RS=0; RW=0; E=1; Lcd_Bus=cmdcode; delay(5);////////////////////在数据写入的时候加入适当的延时 E=0; delay(5); }
/*-------------------写数据到LCD----------------------------*/ void write_data(unsigned char Dispdata) { chk_busy(); RS=1; RW=0; E=1; Lcd_Bus=Dispdata; delay(5);///////////////////在数据写入的时候加入适当的延时 E=0; delay(5); } /*------------------初始化LCD屏--------------------------*/ void lcdreset() { delay(2000); write_com(0x30); delay(10); //选择基本指令集 write_com(0x30); //选择8bit数据流 delay(5); write_com(0x0c); //开显示(无游标、不反白) delay(10); write_com(0x01); //清除显示,并且设定地址指针为00H delay(500); write_com(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位 delay(0); }
/*------------------显示字符串--------------------------*/ void hzkdis(unsigned char code *s) { while(*s>0) { write_data(*s); s++; delay(50); } } /*------------------首屏显示--------------------------*/ void ceshi() { write_com(0x01);//清除显示,并且设定地址指针为00H delay(5); write_com(0x80);//第一行(如果是地址是:80H,即LCD的第一行的第一个位置显示) hzkdis("Position:");
write_com(0x90);//第二行(如果是地址是:90H,即LCD的第二行的第一个位置显示) hzkdis(*axis); write_com(0x94); write_com(0x0e); hzkdis("85.5"); write_com(0x96); hzkdis("mm"); write_com(0x88);//第三行(如果是地址是:88H,即LCD的第二行的第一个位置显示) hzkdis("Lasers : "); write_com(0x8d); write_com(0x06); hzkdis("ON"); write_com(0x98);//第四行(如果是地址是:98H,即LCD的第二行的第一个位置显示) write_com(0x0f); hzkdis("|X |"); write_com(0x9a); write_com(0x0f); hzkdis("|Z1|"); write_com(0x9c); write_com(0x0f); hzkdis("|Z2|"); write_com(0x9e); write_com(0x0f); hzkdis("|Set|"); } /*------------------显示图片------------------------2005.11.14.21:20修改---benladn911---*/ void Disp_Img(unsigned char code *map) { unsigned int j=0; unsigned char x,y,i; for(i=0;i<9;i+=8) for(y=0;y<32;y++) for(x=0;x<8;x++) { write_com(0x36);//功能设置---8BIT控制界面,扩充指令集 write_com(y+0x80); //行地址 write_com(x+0x80+i); //列地址 write_com(0x30); write_data(map[j++]); write_data(map[j++]); }
} /*------------------清整个GDRAM空间----------------------------*/ void clrgram() { unsigned char x,y; for(y=0;y<64;y++) for(x=0;x<16;x++) { write_com(0x34); write_com(y+0x80); //行地址 write_com(x+0x80); //列地址 write_com(0x30); write_data(0x00); write_data(0x00); } } /*------------------整屏写入数据:全亮,或者竖条-----------------------------*/ void lcdfill(unsigned char disdata) { unsigned char x,y; for(y=0;y<32;y++) for(x=0;x<16;x++) { write_com(0x36); write_com(y+0x80); //行地址 write_com(x+0x80); //列地址 write_com(0x30); write_data(disdata); write_data(disdata); } write_com(0x34); write_com(0x36); }
/*------------------清屏命令--------------------------*/ void clrscreen() { write_com(0x01); delay(10); } main() { unsigned char key; int p; for(p=0;p<3;p++) { P2_1 =1; delay(5000); P2_1 =0; delay(5000); } RW=0; lcdreset(); //初始化LCD屏 // Disp_Img(map_1); // delay(5000); ceshi(); //显示测试字样 RW =1; while(1) { key = getkey(); switch(key) { case xkey: { write_com(0x90); clrgram(); axis = "X :" ; }; break; case ykey: { write_com(0x90); //clrgram( ); axis = "Z1:"; }; break; default: break; } } }
|