51单片机、12864和键盘的问题,请大神帮忙!
[复制链接]
这是我写的代码,目的是按下键盘的第几个键,就在lcd12864的第一行第一个地址显示出按键的序号,现在出问题了,请大家帮帮忙!
#include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int #define Busy 0x80 #define LCMdata P0
sbit RST=P3^4; sbit RS=P3^5; sbit RW=P3^6; sbit E=P3^7; uchar num,temp,num1;
uchar code string_1[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46};
//*********必要声明***************** uchar keyscan(key); //键盘扫描函数,返回第几个键盘 void Display_String(uchar line,uchar *string);//12864显示字符串函数 void write_12864com(uchar com); //12864写指令函数 void write_12864dat(uchar dat); //12864写数据函数 void Lcm_Init(void); //12864初始化函数 void RDbf(void); //12864读忙状态函数 void delay_ms(uint ms_number); //12864延时函数
//********延时函数********************** //11.05902M晶振,参数是几就延时几毫秒, void delay_ms(uint ms_number) { uint t; uchar j; for(t=0;t<ms_number;t++) { for(j=0;j<200;j++); for(j=0;j<102;j++); } } //**************************************
//*********延时函数******************** /* 11.0592MHz 晶振延时50us void delay_50us(uint t) { uchar j; for(;t>0;t--) for(j=19;j>0;j--); } **************************************/
//****************键盘扫描函数********************** uchar keyscan() { P2=0xfe; temp=P2; temp=temp&0xf0; while(temp!=0xf0) { delay_ms(5); temp=P2; temp=temp&0xf0; while(temp!=0xf0) { temp=P2; switch(temp) { case 0xee:num=0; break; case 0xde:num=1; break; case 0xbe:num=2; break; case 0x7e:num=3; break; } while(temp!=0xf0) { temp=P2; temp=temp&0xf0; } } }
P2=0xfd; temp=P2; temp=temp&0xf0; while(temp!=0xf0) { delay_ms(5); temp=P2; temp=temp&0xf0; while(temp!=0xf0) { temp=P2; switch(temp) { case 0xed:num=4; break; case 0xdd:num=5; break; case 0xbd:num=6; break; case 0x7d:num=7; break; } while(temp!=0xf0) { temp=P2; temp=temp&0xf0; } } }
P2=0xfb; temp=P2; temp=temp&0xf0; while(temp!=0xf0) { delay_ms(5); temp=P2; temp=temp&0xf0; while(temp!=0xf0) { temp=P2; switch(temp) { case 0xeb:num=8; break; case 0xdb:num=90; break; case 0xbb:num=10; break; case 0x7b:num=11; break; } while(temp!=0xf0) { temp=P2; temp=temp&0xf0; } } }
P2=0xf7; temp=P2; temp=temp&0xf0; while(temp!=0xf0) { delay_ms(5); temp=P2; temp=temp&0xf0; while(temp!=0xf0) { temp=P2; switch(temp) { case 0xe7:num=12; break; case 0xd7:num=13; break; case 0xb7:num=14; break; case 0x77:num=15; break; } while(temp!=0xf0) { temp=P2; temp=temp&0xf0; } } } return num; }
//***********12864读忙状态函数*********** void RDbf(void) { while(1) { RS=0; RW=1;E=0; LCMdata=0xFF; E=1; if((P0&Busy)==0) break; } } //*************************************
//*******12864写指令函数********************* void write_12864com(uchar wrcommand,busyc) { if(busyc) RDbf(); RS=0; RW=0; E=1; LCMdata=wrcommand; E=0; } //***************************************
//*********12864写数据函数******************** void write_12864dat(uchar wrdata) //写数据函数 { RDbf(); RS=1; RW=0; E=1; LCMdata=wrdata; E=0; } //****************************************
//**********12864初始化函数*************** void Lcm_Init(void) { delay_ms(5); RST=1; RST=0; RST=1; write_12864com(0x30,0); delay_ms(5); write_12864com(0x30,0); //2次显示模式设置,不用判忙 delay_ms(5); write_12864com(0x0C,1); //开显示及光标设置 delay_ms(5); write_12864com(0x01,1); //显示清屏 delay_ms(5); delay_ms(5); write_12864com(0x06,1); //显示光标移动设置 delay_ms(5); } //*************************************
//*************12864显示函数************** void Display_String(uchar line,string) { uchar addr; if(line==1) addr=0x80; else if(line==2) addr=0x90; else if(line==3) addr=0x88; else if(line==4) addr=0x98; write_12864com(addr,1);
write_12864dat(string_1[string]);
}
//************主函数***************** void main() { uchar key; Lcm_Init(); while(1) { key=keyscan(); //调用键盘扫描, switch(key) { case 0:Display_String(1,0); case 1:Display_String(1,1); case 2:Display_String(1,2); case 3:Display_String(1,3); case 4:Display_String(1,4); case 5:Display_String(1,5); case 6:Display_String(1,6); case 7:Display_String(1,7); case 8:Display_String(1,8); case 9:Display_String(1,9); case 10:Display_String(1,10); case 11:Display_String(1,11); case 12:Display_String(1,12); case 13:Display_String(1,13); case 14:Display_String(1,14); case 15:Display_String(1,15); } } }
|