|
本帖最后由 越南超級(jí)人類 于 2019-9-24 19:55 編輯
/**********************BST-M51實(shí)驗(yàn)開發(fā)板例程************************
* 平臺(tái):BST-M51 + Keil U4 + STC89C52
* 名稱:與上個(gè)軟件延時(shí)的計(jì)時(shí)相比,此采用定時(shí)器的方法,不但節(jié)省了單片機(jī)的
資源,而且定時(shí)精度高。故做精確定時(shí)或者單片機(jī)資源有限時(shí),提倡用定時(shí)器
的方法。
這里采用的是定時(shí)器0工作于模式1。
用的是中斷法。
* 日期:2015-6
* 晶振:11.0592MHZ[audio][/audio]
******************************************************************/
//注意晶振需為11.0592M。
//若為其他數(shù)值晶振,請(qǐng)改變TH0與TL0參數(shù)值,否則計(jì)時(shí)會(huì)有很大誤差 。
#include<reg51.h>
#define uchar unsigned char
#define dula P0 //段選信號(hào)的鎖存器控制
#define wela P2 //位選信號(hào)的鎖存器控制,這里只用到P2.4-P2.7
sbit k1=P3^4;
sbit k2=P3^5;
unsigned int j,k,a1,a0,c1,c0,m,f,s,num,n=255;
unsigned char pp;
unsigned char code weitable[]={0x8f,0x4f,0x2f,0x1f};
//數(shù)碼管各位的碼表
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(unsigned char i)
{
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void display1(uchar wei,uchar shu)//在任意一位顯示任意的數(shù)字
{
wei=wei-1;
wela|=0xf0;//給P2.4-P2.7置1
P0=table[shu];
wela=wela&weitable[wei];//給P2需要顯示的那一位置1,其他置0
delay(5);
}
void display(unsigned int a,unsigned int b,unsigned int c,unsigned int d)
{ //一次顯示4個(gè)數(shù)字,且每位顯示數(shù)字可自定。
display1(2,a);
display1(1,b);
display1(4,c);
display1(3,d);
}
void keyscan()
{
if(k1==0)
{
while(k1==0);
f++;
if(f==60)
f=0;
}
if(k2==0)
{
while(k2==0);
s++;
if(s==24)
s=0;
}
}
void main()
{
TMOD=0x01; //模式設(shè)置,00000001,可見采用的是定時(shí)器0,工作與模式1(M1=0,M0=1)。
TR0=1; //打開定時(shí)器
TH0=(65536-46080)/256;// 由于晶振為11.0592,故所記次數(shù)應(yīng)為46080,計(jì)時(shí)器每隔50000微秒發(fā)起一次中斷。
TL0=(65536-46080)%256;//46080的來歷,為50000*11.0592/12
ET0=1; //開定時(shí)器0中斷
EA=1; //開總中斷
while(1)
{
keyscan();
if(pp==20)
{ pp=0;
m++;
n--;
P1=n;//閃爍燈
if(m==60)
{
m=0;
f++;
if(f==60)
{
f=0;
s++;
if(s==24)
{
s=0;
//若到了60s,則歸零
}
}
}
}
c0=f%10; //取出當(dāng)前描述的個(gè)位與十位
c1=f/10;
a0=s%10;
a1=s/10;
display(c1,c0,a1,a0); //顯示
}
}
void time0() interrupt 1
{TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
pp++;
}
--------------------------------------------------------------------------------------------------
|
|