包含Proteus仿真图和源代码
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- uint count0, count1, count2, flag, flag1; //三个定时时间 递减计时 流水灯计时 暂停计时
- uint inx1, inx2, inx3, inx4, inx5, inx6, temp; //六根数码管的显示
- uchar flowlight;
- void delay(uint num); //延时函数
- void display(uchar inx1, uchar inx2, uchar inx3, uchar inx4, uchar inx5, uchar inx6); //数码管显示函数
- sbit Wela = P2^7; //位选端口
- sbit Dula = P2^6; //段选端口
- sbit beep = P2^3; //蜂鸣器端口
- uchar code Table[] = //段码数组
- {
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D, //共阴极段码 0 1 2 3 4 5
- 0x7D,0x07,0x7F,0x6F,0x77,0x7C, //共阴极段码 6 7 8 9 A b
- 0x39,0x5E,0x79,0x71,0x73,0x3E, //共阴极段码 C d E F P U
- 0x31,0x6E,0x76,0x38,0x00 //共阴极段码 T y H L 灭灯
- };
- /************延时函数***************/
- void delay(uint num) //延时 num ms
- {
- uint x, y;
- for(x=num; x>0; x--)
- {
- for(y=110; y>0; y--);
- }
- }
- void main()
- {
- temp = 765432 - 765000;
- count0 = 0;
- count1 = 0;
- count2 = 0;
- flag = 0;
- flag1 = 0;
- EA = 1;
- EX0 = 1;
- TMOD = 0x11; //设置定时器T0,T1 为工作方式1
- TH0 = (65536 - 50000) / 256;
- TL0 = (65536 - 50000) % 256;
- TH1 = (65536 - 50000) / 256;
- TL1 = (65536 - 50000) % 256;
- ET0 = 1; //开定时器T0中断
- ET1 = 1; //开定时器T1中断
- TR0 = 1; //打开定时器T0
- TR1 = 1; //打开定时器T1
-
- P1 = 0xfe;
- flowlight = P1;
- while(1)
- {
- beep = 0;
- if(flag1 != 1)
- {
- display(7, 6, 5, inx4, inx5, inx6);
- if(temp == 398)
- {
- TR0 = 0;
- TR1 = 0;
- flag = 1;
- P1 = 0xff;
- TH0 = (65536 - 50000) / 256;
- TL0 = (65536 - 50000) % 256;
- TR0 = 1;
- count0 = 0;
- temp--;
- }
- }
- else
- {
- display(20, 14, 21, 21, 0, 22);
- TR0 = 0;
- }
- }
- }
- /***********数码管显示函数************/
- void display(uchar inx1, uchar inx2, uchar inx3, uchar inx4, uchar inx5, uchar inx6)
- {
- Wela = 1;
- P0 = 0xfe;
- Wela = 0;
- Dula = 1;
- P0 = Table[inx1];
- Dula = 0;
- delay(5);
-
- Wela = 1;
- P0 = 0xfd;
- Wela = 0;
- Dula = 1;
- P0 = Table[inx2];
- Dula = 0;
- delay(5);
- Wela = 1;
- P0 = 0xfb;
- Wela = 0;
- Dula = 1;
- P0 = Table[inx3];
- Dula = 0;
- delay(5);
- Wela = 1;
- P0 = 0xf7;
- Wela = 0;
- Dula = 1;
- P0 = Table[inx4];
- Dula = 0;
- delay(5);
- Wela = 1;
- P0 = 0xef;
- Wela = 0;
- Dula = 1;
- P0 = Table[inx5];
- Dula = 0;
- delay(5);
- Wela = 1;
- P0 = 0xdf;
- Wela = 0;
- Dula = 1;
- P0 = Table[inx6];
- Dula = 0;
- delay(5);
- }
- /*********定时器T0定时**********/
- void timer0() interrupt 1 //定时器T0定时50ms
- {
- TH0 = (65536 - 50000) / 256;
- TL0 = (65536 - 50000) % 256;
- count0++;
- if(flag != 1)
- {
- if(count0 == 10) //流水灯延时 500ms 流动
- {
- beep = 1;
- flowlight = _crol_(flowlight, 1);
- P1 = flowlight;
- count0 = 0;
- }
- }
- else
- {
- if(count0 % 4 == 0)
- {
- P1 = ~P1;
- if(count0 == 60)
- {
- flag1 = 1;
- }
- }
- }
- }
- /*********定时器T1中断函数50ms*******/
- void timer1() interrupt 3 //定时器T1定时50ms
- {
- TH1 = (65536 - 50000) / 256;
- TL1 = (65536 - 50000) % 256;
- count1++;
- if(count1 == 2) //数码管以100ms递减
- {
- temp--;
- inx4 = temp / 100;
- inx5 = temp % 100 / 10;
- inx6 = temp % 10;
- count1 = 0;
- }
- }
复制代码 新手程序,望大佬放过
所有资料51hei附件下载:
Works.rar
(95.66 KB, 下载次数: 9)
|