專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

單片機(jī)+數(shù)碼管簡(jiǎn)單秒表程序,帶停止啟動(dòng)復(fù)位

作者:佚名   來(lái)源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年11月05日   【字體:

p0口是數(shù)碼管的位選入口,P2口是數(shù)碼管的段選用4位一體的數(shù)碼管,P3.0 P3.1 P3.2分別是啟動(dòng)停止和復(fù)位按鈕
 

 #include <reg51.h>
 
#define uchar unsigned char
#define uint unsigned int

 
#define DATA P2
sbit start=P3^0;
sbit stop=P3^1;
sbit reset=P3^2;

 
//---------啟停標(biāo)志-----
bit SAT=0;

 
bit RST=0;

 
//------------數(shù)碼管碼表----------
uchar tab[10]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,  0x7F, 0x6F};

 
//------------
uint time=0;    //時(shí)間
uint cnt=0;     //計(jì)數(shù)

 

 
//-------------

 

 
void delay(int k)
{
while(k--);
}

 

 

 
void startkey()
{
static uchar i,j;
  if(start==0)
{
if(i==0)
{
j++;
     if(j>3)
{
      if(start==0)
{
i=1;j=0; SAT=1;
}
}
}
}
else
{
i=j=0;
}
}

 

 
void stopkey()
{
static uchar i,j;
  if(stop==0)
{
if(i==0)
{
j++;
     if(j>3)
{
      if(stop==0)
{
i=1;j=0; SAT=0;
}
}
}
}
else
{
i=j=0;
}
 
}

 

 
void resetkey()
{
static uchar i,j;
  if(reset==0)
{
if(i==0)
{
j++;
     if(j>3)
{
      if(reset==0)
{
i=1;j=0; RST=1;
}
}
}
}
else
{
i=j=0;RST=0;
}
 
}

 
//---------數(shù)碼管顯示  00.0-------
void shownumber(int num)         
{
 DATA=tab[num%10];
 P0=7;
 delay(100);
 DATA=0x80;
 P0=6;
 delay(100);
 DATA=tab[num%100/10];
 P0=5;
 delay(100);
 DATA=tab[num/100];
 P0=4;
 delay(100);
 
}

 
//---------定時(shí)器初始化ˉ---------
void time0_init()
TMOD=0x01;
 
TH0=(65536-18348)/256;
TL0=(65536-18348)%256;
EA=1;
ET0=1;
 
}

 
void T0_time()interrupt 1
{
TH0=(65536-18348)/256;
TL0=(65536-18348)%256;
cnt++;
if(cnt==5)
{
time++;cnt=0;
}
 
}

 

 
void main(void)
{
delay(50000);
time0_init();
while(1)
{
shownumber(time);
if(SAT==1)TR0=1;
if(SAT==0)TR0=0;
if(RST==1){TR0=0;SAT=0;time=0;}
startkey();
stopkey();
resetkey();
 
}
}
關(guān)閉窗口

相關(guān)文章