仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (14.97 KB, 下載次數(shù): 83)
下載附件
2020-12-7 03:29 上傳
單片機源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define LED P0
- sbit num0=P2^0;
- sbit num1=P2^1;
- sbit num2=P2^2;
- unsigned char key1_times;
- unsigned int zt,doc,n;
- sbit K1=P1^0;//用P1^0模擬串口數(shù)據(jù)
- uchar code dis_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, //段碼表0,1,2,3,4,5,6,7
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40};//段碼表8,9,a,b,c,d,e,f,滅(16),-(17)
- unsigned char SegBuf[3]={0};
- //*********************************
- //延時程序
- //*********************************
- void delayled(uchar N)
- {
- uchar i,j;
- for(i=0;i<N;i++)
- for(j=0;j<125;j++);
- }
- //**********************************
- //顯示轉(zhuǎn)換
- //**********************************
- void convert(uint n)
- {
- SegBuf[0] =n%10;
- SegBuf[1] =n/10%10;
- SegBuf[2] =n/100%10;
- }
- //**********************************
- //顯示程序
- //**********************************
- void display(void)
- {
- LED=dis_code[SegBuf[0]];
- num0=0;
- delayled(3);
- num0=1;
-
- LED=dis_code[SegBuf[1]]|0x80;
- num1=0;
- delayled(3);
- num1=1;
-
- LED=dis_code[SegBuf[2]];
- num2=0;
- delayled(3);
- num2=1;
- }
- void Disp(uint n)
- {
- convert(n);
- display() ;
- }
- /*****延時程序**********/
- void delay(uchar ms)
- {
- uchar i;
- while(ms--)
- {
- for (i=0;i<125;i++);
- }
- }
- /***********************************/
- void main()
- {
- TMOD = 0x01;
- TH0 = (65536-1000)/256;//定時器T0的初值
- TL0 = (65536-1000)%256;
- key1_times =2;//記錄K1按鍵的次數(shù)。
- IE = 0x8a;
- TR0 = 1;//先開T0,T1是關(guān)閉的
- while(1)
- {
- if(K1==0)
- {
- delay(5);
- if(K1==0)
- {
- while(!K1);
- key1_times++;
- if(key1_times==3)key1_times=0;
- }
- }
- }
- }
- void timer0() interrupt 1//顯示程序,每隔定時的時間顯示一位數(shù)值。
- {
- TR0=0;
- TH0 = (65536-1000)/256;
- TL0 = (65536-1000)%256;
- Disp(doc);
- if(key1_times==0)
- {
- n++;
- if(n==20)//T1每兩次中斷計數(shù)值加1
- {
- doc++; n=0;
- }
- }
- if(key1_times==1) ;
- if(key1_times==2)doc=0;
- TR0=1;
- }
復(fù)制代碼
所有資料51hei提供下載:
數(shù)字秒表.7z
(19.41 KB, 下載次數(shù): 22)
2020-12-7 03:28 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|