|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2013-2-18 19:01:18
|
显示全部楼层
william_rain 发表于 2013-2-18 16:25 ![](static/image/common/back.gif)
定时器还是用中断 比较好
这位大哥帮忙看看,还是不行,我想把“时”“分”“秒”分开,秒表那种,现在还是一秒一秒倒计时的
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long int uint32;
sbit key1=P1^1;
sbit key2=P1^2;
sbit key3=P1^3;
sbit key4=P1^4;
uint8 tab[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
uint32 key,HG,HS,HB,HQ,HW,HSW,HC,counter,hour,min,sec,
hour_H,hour_L,min_H,min_L,sec_H,sec_L,b,c,t;
void delay(uint8 k)
{
uint8 m,n;
for(m=0;m<k;m++)
{
for(n=0;n<120;n++);
}
}
void display_init(void)
{
P2=0xFF;
P0=tab[0];
delay(1);
}
void display(void)
{
P2=0x10;
P0=tab[sec_H];
delay(1);
P2=0x20;
P0=tab[sec_L];
delay(1);
P2=0x04;
P0=tab[min_H];
delay(1);
P2=0x08;
P0=tab[min_L];
delay(1);
P2=0x01;
P0=tab[hour_H];
delay(1);
P2=0x02;
P0=tab[hour_L];
delay(1);
}
void Timer0(void)
{
if(TF0==1)
{
t++;
TH0=0x3C;
TL0=0xB0;
TF0=0;
}
}
void data_init(void)
{
hour=counter/10000;
HC = counter%10000;
min=HC/100;
sec=HC%100;
hour_H=hour/10;
hour_L=hour%10;
min_H = min/10;
min_L = min%10;
sec_H = sec/10;
sec_L = sec%10;
}
void data_tim(void)
{
Timer0();
if (t==20)
{
counter--;
t=0;
if(counter==0)
{
counter=b;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
void data_in(void) //有问题部分,好像没起作用
{
sec--;
if(sec==0)
{
sec=59;
min--;
if(min==0)
{
min=59;
hour--;
if(hour==0)
{
hour=26;////先随便写了个数字
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void T0_init(void)
{
TMOD=0x01;
TH0=0x3C;
TL0=0xB0;
ET0=0;
TR0=1;
EA=0;
}
void main(void)
{
F0:
counter=b=0;
display_init();
if(key1 == 0)
{
delay(20) ;
if(key1 == 0)
{
key = 1 ;
}
else
{
key = 0;
}
while(key1 == 0) ;
}
if(key2 == 0)
{
delay(20) ;
if(key2 == 0)
{
key = 2;
}
else
{
key = 0;
}
while(key2 == 0) ;
}
if(key3 == 0)
{
delay(20) ;
if(key3 == 0)
{
key = 3;
}
else
{
key = 0;
}
while(key3 == 0) ;
}
switch (key)
{
case 1:counter=b=16;break;
case 2:counter=b=120;break;
case 3:counter=b=654321;break;
default:goto F0;
}
T0_init();
while(1)
{
data_init();
data_tim();
data_in();
display();
}
}
//////////////////END////////////////////
|
|