仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (42.07 KB, 下載次數: 120)
下載附件
2021-8-15 21:35 上傳
單片機源程序如下:
- #include<reg51.h>
- #include"lcd.h"
- #include<intrins.h>
- #define GPIO_KEY P1
- unsigned char i,u;
- unsigned char KeyValue;
- //用來存放讀取到的鍵值
- unsigned char KeyState;
- //用來存放按鍵狀態
- unsigned char error;
- unsigned char PuZh[]= " sun ";
- unsigned char code w[]= " wrong ";
- unsigned char code s[]= " ok ";
- unsigned char code clear[]= " please in code ";
- unsigned char code dat[]= "0123456789ABCDEF";
- unsigned char mima[]={1,2,3,4,5,6};
- unsigned char guanli[]={1,1,1,1,1,1};
- sbit men=P3^3;
- sbit in=P3^4;
- sbit out=P3^5;
- sbit model=P3^6;
- sbit sp=P3^7;
- void Delay10ms(); //延時50us
- void delay2s(void);
- void KeyDown(); //檢測按鍵函數
- void InitUART(void);
- void SendOneByte(char c);
- void delay_ms(char p)
- {
- char m;
- for(m=0;m<p;m++)
- {
- void Delay10ms();
- }
- }
-
- /*******************************************************************************
- * 函 數 名 : main
- * 函數功能 : 主函數
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void main(void)
- {
- unsigned char i;
- LcdInit();
- InitUART();
- sp=0;
- KeyState=0;
- for(i=0;i<16;i++)
- {
- // LcdWriteCom(0x80);
- LcdWriteData(PuZh[i]);
- }
- LcdWriteCom(0x80+0X40);
- for(i=0;i<16;i++)
- {
- // LcdWriteCom(0x80);
- LcdWriteData(clear[i]);
- }
- while(1)
- {
- KeyDown();
- if(in==0)
- {
- Delay10ms();
- if(in==0)
- u++;
- while(!in);
- PuZh[0]=(int)dat[(u/10000)];
- PuZh[1]=(int)dat[(u/1000)];
- PuZh[2]=(int)dat[(u/100)];
- PuZh[3]=(int)dat[(u/10%10)];
- PuZh[4]=(int)dat[(u%10)];
- LcdWriteCom(0x80);
- for(i=0;i<16;i++)
- {
- LcdWriteData(PuZh[i]);
- }
- for(i=0;i<5;i++)
- {
- SendOneByte(PuZh[i]);
- }
- SendOneByte('\r');
- SendOneByte('\n');
- }
- if(model==0)
- {
- Delay10ms();
- if(model==0){
- PuZh[9]=dat[10];
- Delay10ms();
- LcdWriteCom(0x80);
- for(i=0;i<16;i++)
- {
- LcdWriteData(PuZh[i]);
- }
- if(men==0)
- Delay10ms();
- if(men==0)
- {
- sp=1;
- SendOneByte('w');
- SendOneByte('a');
- SendOneByte('r');
- SendOneByte('i');
- SendOneByte('n');
- SendOneByte('g');
- SendOneByte('\r');
- SendOneByte('\n');
- }
- Delay10ms();
- }
- }
-
-
- if(model==1)
- {PuZh[9]=dat[11];
- sp=0;
- LcdWriteCom(0x80);
- for(i=0;i<16;i++)
- {
- LcdWriteData(PuZh[i]);
- }
- }
-
-
- }
- }
-
- /*******************************************************************************
- * 函 數 名 : KeyDown
- * 函數功能 : 檢測有按鍵按下并讀取鍵值
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void KeyDown(void)
- {
- char a,k,l,error;
-
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)
- {
- Delay10ms();
- if(GPIO_KEY!=0x0f)
- {
- k++;
- KeyState=1;
- //測試列
- GPIO_KEY=0X0F;
- // Delay10ms();
- switch(GPIO_KEY)
- {
- case(0X07): KeyValue=0;break;
- case(0X0b): KeyValue=1;break;
- case(0X0d): KeyValue=2;break;
- case(0X0e): KeyValue=3;break;
- // default: KeyValue=17; //檢測出錯回復17意思是把數碼管全滅掉。
- }
- //測試行
- GPIO_KEY=0XF0;
- Delay10ms();
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+4;break;
- case(0Xd0): KeyValue=KeyValue+8;break;
- case(0Xe0): KeyValue=KeyValue+12;break;
- // default: KeyValue=17;
- }
- while((a<50)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
- {
- Delay10ms();
- a++;
- }
-
- a=0;
- if(k==6){
- LcdWriteCom(0x80+0X40);
- k=0;
- for(l=0;l<6;l++)
- {
- if(mima[l]==guanli[l]) error=1;
- else {error=0; break;}
- }
- if(error==1) {
- for(i=0;i<16;i++)
- {
- LcdWriteData(s[i]);
- }
- delay2s();; }
- else
- {
- for(i=0;i<16;i++)
- {
- LcdWriteData(w[i]);
- }
- delay2s();
- }
- LcdWriteCom(0x80+0X40);
- for(i=0;i<16;i++)
- {
- LcdWriteData(clear[i]);
- }
- }
- }
- }
- }
- /*******************************************************************************
- * 函 數 名 : Delay10ms
- * 函數功能 : 延時函數,延時10ms
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void Delay10ms(void) //誤差 0us
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- void delay2s(void) //誤差 0us
- {
- unsigned char a,b,c;
- for(c=127;c>0;c--)
- for(b=235;b>0;b--)
- for(a=32;a>0;a--);
- _nop_(); //if Keil,require use intrins.h
- }
- void InitUART(void)
- {
- TMOD = 0x20;
- SCON = 0x50;
- TH1 = 0xFD;
- TL1 = TH1;
- PCON = 0x00;
- EA = 1;
- ES = 1;
- TR1 = 1;
- }
- void SendOneByte(char c)
- {
- SBUF = c;
- while(!TI);
- TI = 0;
- }
- void UARTInterrupt(void) interrupt 4
- {
- if(RI)
- {
- RI = 0;
-
- }
- }
復制代碼
仿真代碼51hei附件下載:
仿真程序.7z
(251.29 KB, 下載次數: 104)
2021-8-15 21:36 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|