51单片机测速度或测方波频率两用程序
#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)//延时程序
{
unsigned int i;
while( --a != 0)
{
for(i = 0; i < 125; i++);
}
}
//简化显示函数
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;//设定时器1并用工作方式1
TH0 = (65536-50000)/256;//高位赋初值
TL0 = (65536-50000)%256;//低位赋初值
EA = 1;//开启总中断开关
ET0 = 1;//启动定时中断
TR0 = 1;//开启定时器
EX0 = 1;//打开外部中断
IT0 = 1;//选择外部中断方式1*/
TMOD=0x51; //T1计数器,T0定时器,方式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;
}
51单片机测速度测方波频 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)