大二工科直女,单片机刚入坑,这是第一个全部由自己完成的处女作啦,在图书馆调试到腰酸背痛,但还是超级开心!!
我的单片机是淘宝买的学习板,所以电路图直接发现有的咯
萌新们大家一起加油鸭~~
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit key2=P3^0;
- sbit key3=P3^1;
- sbit key4=P3^2;
- sbit key5=P3^3;
- sbit DU=P2^6;
- sbit WE=P2^7;
- uchar secs,sec, msec, usec;
- uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
- void display(uchar secs,sec,msec)
- {
-
- P0 = 0XFF;
- WE = 1;
- P0 = 0XFE;
- WE = 0;
-
- DU = 1;
- P0 = tabel[secs];
- DU = 0;
- delay(5);
- P0 = 0XFF;
- WE = 1;
- P0 = 0XFD;
- WE = 0;
-
- DU = 1;
- P0 = tabel[sec];
- DU = 0;
- delay(5);
- P0 = 0XFF;
- WE = 1;
- P0 = 0XFB;
- WE = 0;
-
- DU = 1;
- P0 = tabel[msec];
- DU = 0;
- delay(5);
- }
- void dian()
- {
- P0 = 0XFF;
- WE = 1;
- P0 = 0XFD;
- WE = 0;
-
- DU = 1;
- P0 = 0x80;
- DU = 0;
- delay(5);
- }
- void count()
- {
- if(usec == 10)
- {
- usec = 0;
- msec++;
- }
- if(msec==10)
- {
- msec=0;
- sec++;
- }
- if(sec==10)
- {
- sec=0;
- secs++;
- }
- if(secs==10)
- {
- TR0=0;
- TH0=0XDC;
- TL0=0X00;
- usec=0;
- msec=0;
- sec=0;
- secs=0;
- TR0=1;
- }
- }
- void timer0() interrupt 1
- {
- TH0=0XDC;
- TL0=0X00;
- usec++;
- count();
-
- }
- void init()
- {
-
- TMOD=0X01;
- TH0=0XDC;
- TL0=0X00;
- EA=1;
- ET0=1;
- sec=0;
- msec=0;
- usec=0;
- secs=0;
- }
- void keyscan()
- {
- if(key2==0)//判断按键是否按下
- {
- delay(10);//延时10ms用以消抖
- if(key2==0)//按键确实按下
- {
- while(key2==0)//松手检测
- {
- display(secs,sec,msec);//还没松手时,不断刷新显示函数,这样数码管就不会不显示
- }
- TR0=1;//打开定时器0
-
- }
- }
-
- if(key3==0)//判断按键是否按下
- {
- delay(10);//延时10ms用以消抖
- if(key3==0)//按键确实按下
- {
- while(key3==0)//松手检测
- {
- TR0=0;//关闭定时器0
- }
- }
- }
- if(key4==0)//判断按键是否按下
- {
- delay(10);//延时10ms用以消抖
- if(key4==0)//按键确实按下
- {
- while(key4==0)//松手检测
- {
- TR0=1;//关闭定时器0
- }
- }
- }
-
- if(key5==0)
- {
- delay(10);//延时10ms消抖
- if(key5==0)//zero按键确实按下
- {
- TR0=0;//关闭定时器0
- while(key5==0)//松手检测
- {
- display(secs,sec,msec);//还没松手时,不断刷新显示函数,这样数码管就不会不显示
- }
- TH0=0; //重新装初值
- TL0=0;
- sec=0;//初始化一下变量为默认的数值(0);
- msec=0;
- usec=0;
- secs=0;
-
- }
-
- }
-
- }
- void main()
- {
-
- init();
- while(1)
- { dian();
- keyscan();
- display(secs,sec,msec);
- }
- }
复制代码
所有资料51hei提供下载:
秒表.rar
(22.78 KB, 下载次数: 29)
开发板原理图V3(1).pdf
(152.3 KB, 下载次数: 27)
|