日常小玩具,数码管一共有八位,改着改着就只用了5位,ms级别的目前用不上; 具有 开机动画,暂停,清零,关屏等功能;欢迎各路大神观看,时间仓促,如有不足之处,还请轻喷!!! 感谢各位;
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "reg51.h"
- #include "timer.h"
- sbit k1 = P1^0;
- sbit k2 = P1^1;
- sbit k3 = P1^2;
- sbit le = P1^7;
- unsigned char cp,ms,sec=58,min=29;
- unsigned char key1 = 0,key2 = 0,key3 = 0;
- unsigned char key22 = 0;
- unsigned char seg[] = {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xff};
- unsigned char seg_dot[] = {0X40,0X79,0X24,0X30,0X19,0X12,0X02,0X78,0X00,0X10};
- unsigned char seg_buff[] = {0,0,0,0,0,0,0,0};
- unsigned char seg_init[] ={0x89,0x86,0xC7,0xc0};
- void Delay10ms() //@12.000MHz
- {
- unsigned char i, j;
- i = 20;
- j = 113;
- do
- {
- while (--j);
- } while (--i);
- }
- void t0_init()
- {
- TMOD = 0X02;
- TH0 = 0X9C;
- TL0 = 0X9C;
- EA = 1;
- ET0 = 1;
- // TR0 = 1;
- }
- void delay(unsigned int t)
- { unsigned char i;
- for(i=0;i<t;i++)
- {
- Delay10ms();
- }
- }
- void SEG_duan(unsigned char pos,unsigned char dat)
- {
- P0 = 0XFF;
- P0 = dat;
- P2 = 0X80 >> pos;
- }
- void seg_init1(void)
- {
- le = 1;
- SEG_duan(0,seg_init[0]);
- delay(1);
- SEG_duan(1,seg_init[1]);
- delay(1);
- SEG_duan(2,seg_init[2]);
- delay(1);
- SEG_duan(3,seg_init[2]);
- delay(1);
- SEG_duan(4,seg_init[3]);
- delay(1);
- SEG_duan(5,0XBF);
- delay(1);
- SEG_duan(6,0XBF);
- delay(1);
- SEG_duan(7,0XBF);
- delay(1);
- }
- void tran_seg()
- {
- seg_buff[0] = min/10;
- seg_buff[1] = min%10;
- seg_buff[2] = 0xbf;
- seg_buff[3] = sec/10;
- seg_buff[4] = sec%10;
- seg_buff[5] = 0xff;
- }
- void Dis_p() //时间显示
- {
-
- tran_seg();
- SEG_duan(0,seg[seg_buff[0]]);
- delay(1);
- SEG_duan(1,seg[seg_buff[1]]);
- delay(1);
- SEG_duan(2,seg_buff[2]);
- delay(1);
- SEG_duan(3,seg[seg_buff[3]]);
- delay(1);
- SEG_duan(4,seg[seg_buff[4]]);
- delay(1);
- }
- void Dis_p1() //关闭数码管;
- {
-
-
- SEG_duan(0,seg[10]);
- delay(1);
- SEG_duan(1,seg[10]);
- delay(1);
- SEG_duan(2,seg[10]);
- delay(1);
- SEG_duan(3,seg[10]);
- delay(1);
- SEG_duan(4,seg[10]);
- delay(1);
- }
- void t0_isr(void) interrupt 1
- {
- cp++; //100us
- if(cp==50)
- {
- ms++;
- cp = 0;
- }
- if(ms == 200)
- {
- sec++;
- ms = 0;
- }
- if(sec > 59)
- {
- sec = 0;
- min++;
- }
- if(min > 59)
- min = 0;
- }
- //============按键按下第一次时,选择
- void scanf_key1()
- {
- if(k1 == 0)
- key1 = 1;
- if(k1 == 1 && key1 == 1) //暂停;
- {
-
- TR0 = ~ TR0;
- key1 = 0;
-
- }
-
-
- if(k2 == 0) // 清零 瞬时性
- key2 = 1;
- if(k2 == 1 && key2 == 1)
- {
- TR0 = 1;
- SEG_duan(0,min = 0);
- SEG_duan(2,sec = 0);
- SEG_duan(4,ms = 0);
-
- key2 = 0;
- key22 = 1;
-
- }
-
- if(k3 == 0) // ka
- key3 = 1;
- if(k3 == 1 && key3 == 1)
- {
- TR0 = 0;
- key22 = 2;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
2.23.rar
(62.64 KB, 下载次数: 51)
|