标题: 基于AT89C51的速度测量与仿真 [打印本页]

作者: 丁荣全001    时间: 2018-12-16 13:44
标题: 基于AT89C51的速度测量与仿真
基于51单片机的简单速度测量仪
protues仿真如图参考代码如下
#include <reg51.h>
#define N 1//转盘上所安装的磁钢个数
sbit DLed1=P2^0;//定义第一位数码管"位选"控制线的别名
sbit DLed2=P2^1;//定义第二位数码管"位选"控制线的别名
sbit DLed3=P2^2;
sbit DLed4=P2^3;
Sbit Buzzer=P3^3;
unsigned int speed=0;//最后的转速值
unsigned int count=0;//在指定时间内记到的外部脉冲数
unsigned char k=0;
Unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void disp(unsigned char x1,unsigned char x2,unsigned char x3,unsigned char x4);
2)延时子函数程序
void delay1ms(unsigned int k)
{
  unsigned char n,m;
  for(m=0;m<k;m++)
  {for(n=0;n<115;n++);}
}
void main()
{
unsigned char n;
//EA=0;
TMOD=0x51;
TH0=0x3c;
TL0=0xb0;
ET0=1;
EA=1;
TR0=1;
TR1=1;
while(1)
{         
speed=2*0.25*3.14*3.6*100*count/N;
Buzzer=1;
If(speed>=2000)
{
Buzzer=~Buzzer;
}   
              for(n=20;n>0;n--)               
{
                   disp(speed/1000,speed%1000/100,speed%100/10,speed%10);
         }
                TR0=1;
                TR1=1;
      }
}
(3)T0中断处理函数程序
void int0_fun() interrupt 1
{
k++;
TH0=0x3c;
TL0=0xb0;
if(k>=20)
{
        TR0=0;
        TR1=0;
        count=TH1*256+TL1;
        TH1=0x00;
        TL1=0x00;
        k=0;
}
}
(4)四位数码管的显示函数程序
void disp(unsigned char x1,unsigned char x2,unsigned char x3,unsigned char x4)
{
DLed1=1;DLed2=0;DLed3=0;DLed4=0;
P1=tab[x1];
               delay1ms(10);
P1=0xff;
DLed1=0;DLed2=1;DLed3=0;DLed4=0;
        P1=tab[x2];
        delay1ms(10);
P1=0xff;
DLed1=0;DLed2=0;DLed3=1;DLed4=0;
        P1=tab[x3];
        delay1ms(10);
       P1=0xff;
DLed1=0;DLed2=0;DLed3=0;DLed4=1;
        P1=tab[x4];
        delay1ms(10);
P1=0xff;
}

112.png (371.42 KB, 下载次数: 48)

112.png





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1