51單片機(jī)簡(jiǎn)易頻率計(jì)源代碼
#include
#define uchar unsigned char
#define uint unsigned int
#define uint unsigned int
uint ff; //接受頻率值
uchar start=1; //定時(shí)器,計(jì)數(shù)模式啟動(dòng)變量
uchar start=1; //定時(shí)器,計(jì)數(shù)模式啟動(dòng)變量
code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60};
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
void delay(uint k) //延時(shí)函數(shù)
{
while(k--);
}
{
while(k--);
}
void show(uint k) //數(shù)值顯示函數(shù)
{
static uchar i,j;
uint ss[5];
i=0;
do
{
ss[i]=k%10;
i++;
}while(k/=10);
for(j=0;j {
P0=seg[ss[j]];
P2=tab[j];
delay(100);
P0=0xff;
P2=0xff;
}
}
{
static uchar i,j;
uint ss[5];
i=0;
do
{
ss[i]=k%10;
i++;
}while(k/=10);
for(j=0;j {
P0=seg[ss[j]];
P2=tab[j];
delay(100);
P0=0xff;
P2=0xff;
}
}
void time0_init()
{
TMOD|=0x01; //定時(shí)器0 定時(shí)模式 計(jì)數(shù)范圍65536
TH0=0x3c; //定時(shí)初值50ms
TL0=0xb0;
TR0=0; //暫停定時(shí)器0
}
void time1_init()
{
TMOD|=0x50; //定時(shí)器1 計(jì)數(shù)模式,計(jì)數(shù)范圍65536
TH1=0; //計(jì)數(shù)初值0
TL1=0;
TR1=0;//暫停計(jì)數(shù)器
}
{
TMOD|=0x50; //定時(shí)器1 計(jì)數(shù)模式,計(jì)數(shù)范圍65536
TH1=0; //計(jì)數(shù)初值0
TL1=0;
TR1=0;//暫停計(jì)數(shù)器
}
void str_init() //定時(shí)器中斷
{
EA=1;
ET0=1;
}
{
EA=1;
ET0=1;
}
void time0_event() interrupt 1
{
static uchar i;
i++;
TH0=0x3c; //定時(shí)初值50ms
TL0=0xb0;
if(i==20)
{
i=0;
TR1=0; //停止計(jì)數(shù)
TR0=0; //停止定時(shí)
ff=(TH1*256+TL1); //求出頻率值 就是1秒內(nèi)脈沖次數(shù)
TH1=0; //計(jì)數(shù)值清零
TL1=0;
TH0=0x3c; //定時(shí)初值50ms
TL0=0xb0;
start=1; //啟動(dòng)定時(shí)器開啟變量
}
}
{
static uchar i;
i++;
TH0=0x3c; //定時(shí)初值50ms
TL0=0xb0;
if(i==20)
{
i=0;
TR1=0; //停止計(jì)數(shù)
TR0=0; //停止定時(shí)
ff=(TH1*256+TL1); //求出頻率值 就是1秒內(nèi)脈沖次數(shù)
TH1=0; //計(jì)數(shù)值清零
TL1=0;
TH0=0x3c; //定時(shí)初值50ms
TL0=0xb0;
start=1; //啟動(dòng)定時(shí)器開啟變量
}
}
void main()
{
time0_init();
time1_init();
str_init();
{
time0_init();
time1_init();
str_init();
while(1)
{
if(start==1)
{
TR0=1; //啟動(dòng)定時(shí)器
TR1=1; //啟動(dòng)計(jì)數(shù)器
start=0; //關(guān)閉啟動(dòng)變量位 保證1秒時(shí)間
{
if(start==1)
{
TR0=1; //啟動(dòng)定時(shí)器
TR1=1; //啟動(dòng)計(jì)數(shù)器
start=0; //關(guān)閉啟動(dòng)變量位 保證1秒時(shí)間
}
show(ff);
}
}
show(ff);
}
}
評(píng)論