工业控制 | 能源技术 | 汽车电子 | 通信网络 | 安防监控 | 智能电网 | 移动手持 | 无线技术 | 家用电器 | 数字广播 | 消费电子 | 应用软件 | 其他方案

电路设计->微机单片机电路图->单片机综合电路图->使用89C52来做定时记数器

使用89C52来做定时记数器

作者:dolphin时间:2011-05-05

/***************************************************************
功能:实现单片机定时记数器

***************************************************************/
#include reg51.h
sbit DsDat=0x94; /*定义数据位输入给74ALS164数据位就是从P14出去的*/
sbit DsClk=0x95; /*定义脉冲信号给164,脉冲信号是从P15出去的*/
sbit Gw=0x90; /*定义个位定形P10*/
sbit Sw=0x91; /*定义十位P11*/
sbit Bw=0x92; /*定义百位P12*/
sbit Qw=0x93; /*定义千位P13*/
sbit sw1=P3^2; //启动计时器按钮
sbit sw2=P3^3; //结束按钮
sbit sw3=P3^4; //查看按钮总分钟按钮


/*------------------------------------------------
功能:对应0--9显示码.
-------------------------------------------------*/
unsigned char code Led_Show[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80};//{0x50,0xf5,0x68,0x4a,0x47,0xc2,0xc0,0x5b,0x40,0x42,0x41,0xc4,0xf0,0x4c,0xe0,0xe1,0xff};
unsigned int ms=0;//1000MS为1S作为中间变量
unsigned char ss=0;//作为秒的中间变量
static unsigned int nn=0;//分的中间变量
static unsigned int all_nn=0;//总的时间分钟
unsigned int ms1=0;//定时器1 MS的中间变量
unsigned int ss1=0;//定时器1 秒的中间变量
static unsigned char alltime_show=0;//显示总分钟的标示
static unsigned char time_shong=0;//
/*-------------------------------------------------
功能:1MS延时子程序
-------------------------------------------------*/
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for( i =0;i x;i++ )
{
for( j =0;j500;j++ );
}
}

/*-------------------------------------------------
功能:数码管显示子程序
-------------------------------------------------*/
void Hc164(unsigned char temp )
{//temp是要显示的定符
unsigned char i,temp1;
temp1 = Led_Show[temp];
for(i = 0;i 8;i++ )
{
// DsClk = 0;/*先不给脉冲给164*/
if( temp1 & 0x80 )
DsDat = 1;/*数据不传输*/
else
DsDat= 0;/*一个位的数据传输*/

DsClk = 1;/*给脉冲给164*/
DsClk = 0;/*先不给脉冲给164*/
temp1 =1;
}
}
/*to BCD控制 个十百千 位*/
void toBCD(unsigned int y1)
{
unsigned char x1,x2,x3,x4,y2,y3,y4;
x1=y1/1000;/*显示千位*/
if(y11000)
{}
else
{
Hc164(x1);
Qw=0;
Delay_xMs(1);
Qw=1;
}

y2=y1%1000;
x2=y2/100;

if (y1100)
{}
else
{
Hc164(x2);/*显示百位*/
Bw=0;
Delay_xMs(1);
Bw=1;
}

y3=y2%100;
x3=y3/10;
if(y110)
{}
else
{
Hc164(x3);/*显示十位*/
Sw=0;
Delay_xMs(1);
Sw=1;
}

y4=y3%10;
x4=y4;


Hc164(x4);/*显示个位*/
Gw=0;
Delay_xMs(1);
Gw=1;

}

/*------------------------------------------------
定时器0的初始化工作设为1MS中断一次
-------------------------------------------------*/
void T0_first(void)
{
TMOD=0x01;//定时器0工作方式1
TH0=0xFC;
TL0=0x66;
EA=1;//CPU启动
ET0=1;//T/C0开中断
TR0=1;//启动记数
}
/*------------------------------------------------
定时器T0关中断
--------------------------------------------------*/
void T0_out(void)
{
EA=0;//CPU关中断
ET0=0;//T/C0关中断
TR0=0;//关记数器
}
/*------------------------------------------------
中断函数1MS中断一次
--------------------------------------------------*/
void timer0(void)interrupt 1 using 1
{
TH0=0xFC;
TL0=0x66;
ms=ms+1;
if (ms==1000)//够了1S
{
ms=0;
ss=ss+1;



评论

技术专区