基于Proteus和KeilC的程序開發 驗證計數顯示器的如下功能:可統計按鈕BUT的按壓次數,并將按壓結果以十進制數形式顯示出來;當顯示值達到99后可自動從1開始,無限循環。
驗證發光二極管的如下功能:仿真運行開始時,當開關p3_2閉合時,按一下按鈕p3_4,流水燈全亮,再按一下按鈕,流水燈全滅,然后正式開始流水,從第一個二極管開始每當按鈕BUT按壓一次,二極管就亮一次,且再次按壓按鈕時,下一個二極管亮一次,以此類推,循環往復。 簡單東西,入門學習,不喜勿噴
單片機源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- sbit P3_4=P3^4;
- sbit P3_2=P3^2;
- bit ldelay=0;
- uchar t=0;
- unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f};
- unsigned char count;
- void delay(unsigned int time){
- unsigned int j=0;
- for(;time>0;time--)
- for(j=0;j<125;j++);
- }
- void main(){
- uchar code ledp[10]={0x00,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- uchar ledi;
- TMOD=0x01;
- TH0=0x3c;
- TL0=0xb0;
- TR0=1;
- ET0=1;
- EA=1;
- count=0;
- P0=table[count/10];
- P2=table[count%10];
- while(1){
-
- if(P3_4==0){
- delay(10);
- if(P3_4==0){
- count++;
- if(count==100)
- count=0;
- P0=table[count/10];
- P2=table[count%10];
- while(P3_4==0);}
-
- if(P3_2==0){
-
- if(ldelay) {
- ldelay=0;
- P1=ledp[ledi];
- delay(1000);
- ledi++;
- if(ledi==10) ledi=2;
- }
- }
- }
- }
- }
- timer0() interrupt 1{
- t++;
- if(t==20) {t=0;ldelay=1;}
- TH0=0X3C;TL0=0xb0;
- }
復制代碼
|