51單片機(jī)測速度或測方波頻率兩用程序
#include
#define uchar unsigned char //定義一下方便使用
#define uint unsigned int
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
char DAT[4]={0};
uint count;
char flag;
uint t=0;
uint x ;
//unchar num;
//uchar temp;
void DELAY_MS (unsigned int a)//延時(shí)程序
{
unsigned int i;
while( --a != 0)
{
for(i = 0; i < 125; i++);
}
}
//簡化顯示函數(shù)
void disp(int x)
{
int i,temp = 0x01;
DAT[0]=x/1000;
DAT[1]=x/100%10;
DAT[2]=x/10%10;
DAT[3]=x%10;
for(i=0;i<4;i++)
{
P1 = ~temp;
P0 = tab[DAT[i]];
DELAY_MS(4);
temp = temp<<1; //左移位
}
}
void irtime()
{
/*TMOD = 0X51;//設(shè)定時(shí)器1并用工作方式1
TH0 = (65536-50000)/256;//高位賦初值
TL0 = (65536-50000)%256;//低位賦初值
EA = 1;//開啟總中斷開關(guān)
ET0 = 1;//啟動定時(shí)中斷
TR0 = 1;//開啟定時(shí)器
EX0 = 1;//打開外部中斷
IT0 = 1;//選擇外部中斷方式1*/
TMOD=0x51; //T1計(jì)數(shù)器,T0定時(shí)器,方式1
TL0=(65536-10000)%256;
TH0=(65536-10000)/256;
TL1=0;
TH1=0;
TR0=1;
TR1=1;
ET0=1;
EA=1;
}
main()
{
irtime();
while(1)
{
disp(t/3);
}
}
void timer0() interrupt 1
{ x++;
if(x==100)
{
x = 0;
t = TH1*256+TL1;
TL1=0;
TH1=0;
}
TL0=(65536-10000)%256;
TH0=(65536-10000)/256;
}
評論