仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #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模拟串口数据
- 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++);
- }
- //**********************************
- //显示转换
- //**********************************
- 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按键的次数。
- IE = 0x8a;
- TR0 = 1;//先开T0,T1是关闭的
- 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//显示程序,每隔定时的时间显示一位数值。
- {
- TR0=0;
- TH0 = (65536-1000)/256;
- TL0 = (65536-1000)%256;
- Disp(doc);
- if(key1_times==0)
- {
- n++;
- if(n==20)//T1每两次中断计数值加1
- {
- doc++; n=0;
- }
- }
- if(key1_times==1) ;
- if(key1_times==2)doc=0;
- TR0=1;
- }
复制代码
所有资料51hei提供下载:
数字秒表.7z
(19.41 KB, 下载次数: 22)
|