|
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit BEEP=P3^6;
- sbit RS=P2^0;
- sbit RW=P2^1;
- sbit E=P2^2;
- sbit BF=P0^7;
- uchar code digit[]="0123456789ABCDEF";
- uchar code key_code[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77 };
- uchar code str[]="Input Data:";
- uchar key;
- /************************************
- 函數(shù)功能:1ms基準(zhǔn)延時(shí)
- 入口參數(shù):n
- ************************************/
- void delay1ms(unsigned int n )
- {
- unsigned char i;
- while(n--)
- for(i=0;i<115;i++);
- }
- /************************************
- 函數(shù)功能: 500us延時(shí)
- ************************************/
- void delay500us()
- {
- uchar i;
- for(i=0;i<57;i++);
- }
- /************************************
- 函數(shù)功能: 蜂鳴器產(chǎn)生1KHZ頻率聲音,
- ************************************/
- void beep()
- {
- uchar i;
- for(i=0;i<200;i++){
- BEEP=~BEEP;
- delay500us();
- }
- BEEP=1;
- }
- /************************************
- 函數(shù)功能:判斷液晶的忙碌狀態(tài)
- 返回值:result=1,忙碌;result=0,不忙
- ************************************/
- bit BusyTest()
- {
- bit result;
- RS=0; //讀狀態(tài)時(shí),RS=0,RW=1,E=1
- RW=1;
- E=1;
- _nop_(); //延時(shí)一個(gè)時(shí)鐘周期
- _nop_();
- _nop_();
- _nop_(); //4個(gè)周期延時(shí),給硬件反應(yīng)時(shí)間
- result=BF;
- E=0; //將E置0,不允許讀
- return result;
- }
- /************************************
- 函數(shù)功能:將模式設(shè)置指令或顯示地址送入液晶
- 入口參數(shù):dictate
- ************************************/
- void WriteInstruction(uchar dictate)
- {
- while(BusyTest()==1); //如果忙就等待
- RS=0; //寫(xiě)指令時(shí),RS=0.RW=0,E=負(fù)跳變
- RW=0;
- E=0; //先將E拉低
- _nop_();
- _nop_(); //給硬件反應(yīng)時(shí)間
- P0=dictate; //將數(shù)據(jù)送入P0口,即指令或地址
- _nop_();
- _nop_();
- _nop_();
- _nop_(); //反應(yīng)時(shí)間
- E=1; //上升沿讀入指令或地址
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- E=0; //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開(kāi)始執(zhí)行命令
- }
- /************************************
- 函數(shù)功能:指定字符顯示的實(shí)際地址
- 入口參數(shù):x
- ************************************/
- void WriteAddress(uchar x)
- {
- WriteInstruction(x|0x80); //顯示位置的確定方法為“0x80+地址碼x”
- }
- /************************************
- 函數(shù)功能:將數(shù)據(jù)(ASKII碼)寫(xiě)入液晶模塊
- 入口參數(shù):y(字符常量)
- ************************************/
- void WriteData(unsigned char y)
- {
- while(BusyTest()==1); //如果忙碌就等待
- RS=1; //寫(xiě)數(shù)據(jù)時(shí)RS=1,RW=0,E=負(fù)跳變
- RW=0;
- E=0; //先將E拉低
- _nop_();
- _nop_();
- P0=y;
- _nop_();
- _nop_();
- _nop_();
- _nop_(); //反應(yīng)時(shí)間
- E=1;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- E=0; //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開(kāi)始執(zhí)行命令
- }
- /************************************
- 函數(shù)功能:對(duì)LCD的顯示模式進(jìn)行初始化
- ************************************/
- void LcdInt()
- {
- delay1ms(15); //首次寫(xiě)指令時(shí)給LCD一段較長(zhǎng)的反應(yīng)時(shí)間
- WriteInstruction(0x38); //顯示模式設(shè)置:16X2顯示,5X7點(diǎn)陣,8位數(shù)據(jù)總線
- delay1ms(5);
- WriteInstruction(0x38);
- delay1ms(5);
- WriteInstruction(0x38);
- delay1ms(5); //3次寫(xiě) 設(shè)置模式
- WriteInstruction(0x0c); //顯示模式設(shè)置:顯示開(kāi),有光標(biāo),光標(biāo)閃爍
- delay1ms(5);
- WriteInstruction(0x06); //顯示模式設(shè)置:光標(biāo)右移,字符不移
- delay1ms(5);
- WriteInstruction(0x01); //清屏
- delay1ms(5);
- }
- /************************************
- 函數(shù)功能: 掃描鍵盤(pán)
- ************************************/
- void key_scan()
- {
- uchar scan1,scan2,keycode,i;
- P1=0xf0;
- scan1=P1;
- if((scan1&0xf0)!=0xf0){ //判斷是否按鍵
- delay1ms(15); //延時(shí)消陡
- scan1=P1;
- if((P1&0xf0)!=0xf0){ //二次判斷是否按鍵
- P1=0x0f; //線反轉(zhuǎn)法德核心
- scan2=P1;
- keycode=scan1|scan2; //組合成鍵編碼
- }
- for(i=0;i<16;i++){
- if(keycode==key_code[i]){ //查表得鍵值
- key=i;
- }
- }
- }
- else{
- P1=0xff; //P1口寫(xiě)1,輸入狀態(tài)
- }
-
- }
- /***************************************
- 函數(shù)功能:顯示數(shù)字
- ***************************************/
- void Display()
- {
- WriteAddress(0x47);
- delay1ms(5);
- WriteData(digit[key]);
- }
-
- /***********************************
- 函數(shù)功能:主函數(shù)
- ***********************************/
- void main()
- {
- uchar i;
- P1=0xff; //寫(xiě)1,讀狀態(tài)
- delay1ms(15);
- LcdInt();
- WriteInstruction(0x01);
- delay1ms(5);
- WriteAddress(0x02);
- delay1ms(5);
- for(i=0;str[i]!='\0';i++){
- WriteData(str[i]);
- delay1ms(10);
- }
-
- WriteAddress(0x47);
- delay1ms(5);
- WriteData('0');
- while(1){
- P1=0xf0;
- if((P1&0xf0)!=0xf0){
- delay1ms(15);
- if((P1&0xf0)!=0xf0){
-
- key_scan();
- Display();
- beep();
- delay1ms(200);
- }
- }
- }
- }
-
復(fù)制代碼
|
|