永发信息网

求单片机C语言时钟程序~~~

答案:2  悬赏:70  手机版
解决时间 2021-01-12 06:41
  • 提问者网友:欲劫无渡
  • 2021-01-12 01:35
求单片机C语言时钟程序~~~
最佳答案
  • 五星知识达人网友:酒者煙囻
  • 2021-01-12 01:49
这个是简单的数码管时钟显示如果有需要,我写过比较完善的1602显示时钟.显示控制年月日等等.#include
sbit Begin=P2^0;
sbit Hour=P2^1;
sbit Mus=P2^2;
sbit End=P2^3;
unsigned char code Tab[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
unsigned char code num[]={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char Time[]={0,0,16,0,0,16,0,0};
unsigned char a;
unsigned int x,m,th;
void init()
{
TMOD=0x01;
TH0=(65535/50000)/256;
TL0=(65535/50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
unsigned char keyboard()
{
if(Begin==0){
delay(5);
if(Begin==0)
return 1;
}
if(Hour==0){
delay(30);
if(Hour==0)
return 2;
}
if(Mus==0)
return 3;
if(End==0)
return 4;
}
void display()
{
unsigned char i; for(i=0;i<8;i++)
{
P3=num[i];
P0=Tab[Time[i]];
delay(5);
}
}
void main()
{
unsigned char Key;
init();
Key=keyboard();
if(Key==1)
{
while(1){
Key=keyboard();
display();
if(Key==2)
x+=3600;
if(Key==3)
x+=1;
if(Key==4)
return;
}
}}
void holes() interrupt 1 using 2
{
TH0=(65535/50000)/256;
TL0=(65535/50000)%256;
a++;
if(a>=20)
{
x++;
m=x;
th=m/3600; //设置小时
Time[0]=th/10;
Time[1]=th%10;
m=m%3600; Time[2]=16; th=m/60; //设置分钟
Time[3]=th/10;
Time[4]=th%10;
m=m%60; Time[5]=16; th=m; //设置秒
Time[6]=th/10;
Time[7]=th%10; a=0;
}
}
全部回答
  • 1楼网友:迷人又混蛋
  • 2021-01-12 03:09
#include unsigned char dispbitcnt; //数码管位码扫描变量
unsigned char second; //秒变量
unsigned char minite; //分变量
unsigned char hour; //时变量
unsigned char tcnt; //秒信号产生变量
unsigned char mstcnt; //扫描时间变量 const unsigned char dispcode[10]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
//0123456789段码
const unsigned char dispbitcode[6]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf};
//123456位码
unsigned char dispbuf[6]={0,0,0,0,0,0};
//缓冲数组
/#include
unsigned char code SEG7[11]={0xC0,
0xF9,
0xA4,
0xB0,
0x99,
0x92,
0x82,
0xF8,
0x80,
0x90,
0xbf,
};unsigned short int tcount;struct time
{
unsigned char second;
unsigned char minute;
unsigned char hour;
}time1;



void delay(k)
unsigned int k;
{
unsigned char j;
while((k--)!=0)
for(j=0;j<125;j++);
}



TimeUpDown(n,x,y)
unsigned char n,x,y;
{
if(P3_2==0)
{
delay(50);
if(P3_2==0)
{
n++;
if(n==x)
n=0;
while(P3_2==0);
}
} if(P3_3==0)
{
delay(50);
if(P3_3==0)
{
if(n==0)
n=y;
else
n--;
while(P3_3==0);
}
}
return n;
}



void display(DisSecondH,DisSecondL,DisMinutH,DisMinutL,DisHourH,DisHourL)
unsigned char DisSecondH,DisSecondL,DisMinutH,DisMinutL,DisHourH,DisHourL;
{
P0=SEG7[DisSecondH]; //显示秒钟的十位
P2=0xfe;
delay(2); P0=SEG7[DisSecondL]; //显示分钟的个位
P2=0xfd;
delay(2); P0=0xbf; //显示-
P2=0xfb;
delay(2);

P0=SEG7[DisMinutH]; //显示秒钟的十位
P2=0xf7;
delay(2); P0=SEG7[DisMinutL]; //显示分钟的个位
P2=0xef;
delay(2);

P0=0xbf; //显示-
P2=0xdf;
delay(2);

P0=SEG7[DisHourH]; //显示小时的个位
P2=0xbf;
delay(2);
P0=SEG7[DisHourL]; //显示小时的十位
P2=0x7f;
delay(2);
}



main()
{
unsigned char Flag;
TMOD = 0x01;
TH0 = (65536-63000)/256;
TL0 = (65536-60000)%256;
ET0 = 1;
EA = 1;
while(1)
{
if(P3_4 == 0) //设置标志位 Flag
{
delay(50);
if(P3_4 == 0)
{
TR0 = 0;
Flag++;
if(Flag > 3)
Flag = 0;
}
while(P3_4== 0);
} switch(Flag)
{
case 0:
TR0=1;
display(time1.second%10,time1.second/10,time1.minute%10,time1.minute/10,time1.hour%10,time1.hour/10);
break; case 1: //时钟小时调整
TR0=0;
time1.hour=TimeUpDown(time1.hour,24,23);
display(10,10,10,10,time1.hour%10,time1.hour/10);
break; case 2: //时钟分钟调整
time1.minute=TimeUpDown(time1.minute,60,59);
display(10,10,time1.minute%10,time1.minute/10,10,10);
break; case 3: //时钟秒调整
time1.second=TimeUpDown(time1.second,60,59);
display(time1.second%10,time1.second/10,10,10,10,10);
break; default:
break;
} }
}




void t0(void) interrupt 1 using 0 {

tcount++;
if(tcount==10)
{
tcount=0;
time1.second++;
} if (time1.second>=60)
{
time1.second=0;
time1.minute++;
} if (time1.minute>=60)
{
time1.minute=0;
time1.hour++;
} if(time1.hour>=24)
time1.hour=0; TH0=(65536-63000)/256;
TL0=(65536-60000)%256;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯