51单片机试题,写一个用单个数码管显示10秒倒计时的程序,急!!!!! 100
2个回答
展开全部
#include <reg52.h>
#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,0x5e,0x79,0x71}; //段码表
void Timer0Init(void) //1毫秒@12.000MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void main()
{
Timer0Init()
ET0=1; //开启定时器0中断
EA=1;
while(1);//等待中断
}
void time0(void)interrupt 1
{
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
if(disp&1=0)
{
duan=1;
P0=dispcode[time%10];
duan=0;
wei=1;
P0=0xdf;
wei=0;
}
else
{
duan=1;
P0=dispcode[time/10];
duan=0;
wei=1;
P0=0xef;
wei=0;
}
disp++;
miao++;
if(miao==1000)
{
miao=0;
if(miao!=0)time--;
}
}
#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,0x5e,0x79,0x71}; //段码表
void Timer0Init(void) //1毫秒@12.000MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void main()
{
Timer0Init()
ET0=1; //开启定时器0中断
EA=1;
while(1);//等待中断
}
void time0(void)interrupt 1
{
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
if(disp&1=0)
{
duan=1;
P0=dispcode[time%10];
duan=0;
wei=1;
P0=0xdf;
wei=0;
}
else
{
duan=1;
P0=dispcode[time/10];
duan=0;
wei=1;
P0=0xef;
wei=0;
}
disp++;
miao++;
if(miao==1000)
{
miao=0;
if(miao!=0)time--;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询