51单片机数码管6秒倒计时c语言
推荐于2018-03-17 · 郑州西亚斯学院副教授
关注
展开全部
#include <reg51.h>
#define KEY (P1&0x7)
#define SEG P2
#define WEI P3
typedef unsigned char uint8;
uint8 sec,min,ct,flag1,flag2;
uint8 code dis[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
// 0 1 2 3 4 5 6 7
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};//0~F的段码
// 8 9 A B C D E F
uint8 dis_buf[4];
bit keyp = 0;
void delay(uint8 m) //延时程序
{
uint8 i, j;
for(i = 0 ;i < m; i++)
for(j = 0;j < 128 ; j ++)
;
}
void init()
{
TMOD = 0x01;
TH0 = (65536 - 50000)/256;
TL0 = 65536 - 50000; //timer0 init
ET0 = 1;
EA = 1;
TR0 = 1;
}
void p_data(uint8 dat1,uint8 dat2) //数据处理
{
dis_buf[0] = dat1 / 10;
dis_buf[1] = dat1 % 10;
dis_buf[2] = dat2 / 10;
dis_buf[3] = dat2 % 10;
}
void display(uint8 flash) //显示
{
uint8 i,wx = 1;
for(i = 0; i < 4 ; i++)
{
if(flash != 0)
{
if((i==(flash-1))&&(ct < 10))
{
SEG = 0x00;
}
else
SEG = dis[dis_buf[i]];
}
else
SEG = dis[dis_buf[i]];
WEI = ~(wx<<i);
delay(20);
SEG = 0x00;
}
}
void keyscan()
{
if(KEY != 7)
{
delay(5);
if(KEY != 7)
{
if(!keyp)
{
keyp = 1;
switch(KEY)
{
case 6:
flag1++;
break;
case 5:
flag2++;
break;
case 3:
flag2--;
break;
}
}
}
}
else
keyp = 0;
}
void main()
{
sec = 6;
init();
while(1)
{
keyscan();
p_data(min,sec);
display(flag1%5);
if(flag1!=0)
{
if(ct == 19)
ct = 0;
}
switch(flag1)
{
case 1:
sec = flag2;
if(flag2 > 5)
flag2 = 0;
if(flag2 == -1)
flag2 = 6;
break;
}
}
}
void t0() interrupt 1 using 1
{
TH0 = (65536 - 50000)/256;
TL0 = 65536 - 50000;
ct++;
if(ct > 19)
{
ct = 0;
sec--;
if(sec == 255)
{
sec = 6;
}
}
}
#define KEY (P1&0x7)
#define SEG P2
#define WEI P3
typedef unsigned char uint8;
uint8 sec,min,ct,flag1,flag2;
uint8 code dis[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
// 0 1 2 3 4 5 6 7
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};//0~F的段码
// 8 9 A B C D E F
uint8 dis_buf[4];
bit keyp = 0;
void delay(uint8 m) //延时程序
{
uint8 i, j;
for(i = 0 ;i < m; i++)
for(j = 0;j < 128 ; j ++)
;
}
void init()
{
TMOD = 0x01;
TH0 = (65536 - 50000)/256;
TL0 = 65536 - 50000; //timer0 init
ET0 = 1;
EA = 1;
TR0 = 1;
}
void p_data(uint8 dat1,uint8 dat2) //数据处理
{
dis_buf[0] = dat1 / 10;
dis_buf[1] = dat1 % 10;
dis_buf[2] = dat2 / 10;
dis_buf[3] = dat2 % 10;
}
void display(uint8 flash) //显示
{
uint8 i,wx = 1;
for(i = 0; i < 4 ; i++)
{
if(flash != 0)
{
if((i==(flash-1))&&(ct < 10))
{
SEG = 0x00;
}
else
SEG = dis[dis_buf[i]];
}
else
SEG = dis[dis_buf[i]];
WEI = ~(wx<<i);
delay(20);
SEG = 0x00;
}
}
void keyscan()
{
if(KEY != 7)
{
delay(5);
if(KEY != 7)
{
if(!keyp)
{
keyp = 1;
switch(KEY)
{
case 6:
flag1++;
break;
case 5:
flag2++;
break;
case 3:
flag2--;
break;
}
}
}
}
else
keyp = 0;
}
void main()
{
sec = 6;
init();
while(1)
{
keyscan();
p_data(min,sec);
display(flag1%5);
if(flag1!=0)
{
if(ct == 19)
ct = 0;
}
switch(flag1)
{
case 1:
sec = flag2;
if(flag2 > 5)
flag2 = 0;
if(flag2 == -1)
flag2 = 6;
break;
}
}
}
void t0() interrupt 1 using 1
{
TH0 = (65536 - 50000)/256;
TL0 = 65536 - 50000;
ct++;
if(ct > 19)
{
ct = 0;
sec--;
if(sec == 255)
{
sec = 6;
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
硬件不知道,主程序大致这样
uchar counter=0;uchar seond=6;
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
while(second!=0)
{display(second);}
}
中断程序
void timer0 (void) interrupt 1
{ TH0=……
TL0=……
counter++;
if(counter==20)
{counter=0;second--;}
}
uchar counter=0;uchar seond=6;
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
while(second!=0)
{display(second);}
}
中断程序
void timer0 (void) interrupt 1
{ TH0=……
TL0=……
counter++;
if(counter==20)
{counter=0;second--;}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include #define uchar unsigned char #define uint unsigned int sbit duan=P2^6; sbit wei=P2^7; uchar disp=0; uchar time=10; int miao=0; uchar code dispcode[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询