霍尔元件是磁敏元件,要想用来测转速,就必须在被测的旋转体上装一个磁体,旋转时,每当磁体经过霍尔元件,霍尔元件就发出一个信号,经放大整形得到脉冲信号,也有的霍尔元件可以直接输出脉冲信号,送运算,两个脉冲的间隔时间就是周期,由周期可以换算出转速,也可计数单位时间内的脉冲数,再换算出转速。
霍尔元件
以下以数码管显示以及1602显示为例,介绍霍尔元件测速。
数码管显示:
#include<reg52.h>
#define uchar unsigned
#define uint unsigned
uint i ,M;
uchar qian,bai,shi,ge;
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0-9
void delay(uchar ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
void T0_init()
{
TMOD |= 0x01;//16位定时器
TH0 =(65536-50000)/256;
TL0 = (65536-50000)%6;
ET0 = 1;//使能中断
TR0 = 1;//打开定时器
IE = 0x8f;
IP = 0x08;
}
void Int0_init()
{
TCON = 0x01;
}
void Int0() interrupt 0
{
M++;
}
void Timer0() interrupt 1
{
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%6;
i=i+1;
if(i==20)
{
i=0;
qian = M/1000;
bai = M00/100;
shi = M000/10;
ge = M000;
M=0;
}
}
霍尔元件
void display()
{
while(1)
{
P2 = 0xf7;
P0 = table[qian];
delay(5);
P2 = 0xfb;
P0 = table[bai];
delay(5);
P2 = 0xfd;
P0 = table[shi];
delay(5);
P2 = 0xfe;
P0 = table[ge];
delay(5);
}
}
void main()
{
delay(5);
// delay();
Int0_init();
T0_init();
while(1)
{
display();
}
}
1602显示:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit RS=P2^6; //寄存器选择位,将RS位定义为P2.0引脚
sbit RW=P2^5; //读写选择位,将RW位定义为P2.1引脚
sbit E=P2^7
; //使能信号位,将E位定义为P2.2引脚
uint i,M1;
//uchar buf[10];
uchar bai1,shi1,ge1;
uchar dis1[]={"Speed test...."};
uchar dis2[]={"A_S: r/m"};
void delay1ms(uchar ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
uchar lcd_busy()
{
bit result;
RS=0;
RW=1;
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(P0&0x80);
E=0;
return result;
}
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
RS=0;
RW=0;
E=0;
_nop_();
_nop_();
P0=cmd;
_nop_();
_nop_();
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
E=0;
}
void lcd_pos(uchar pos)
{
lcd_wcmd(pos|0x80);
}
void lcd_wdat(uchar dat)
{
while(lcd_busy());
RS=1;
RW=0;
E=0;
P0=dat;
_nop_();
_nop_();
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
E=0;
}
void LcdInit()
{
delay1ms(15);
lcd_wcmd(0x38);
delay1ms(5);
lcd_wcmd(0x38);
delay1ms(5);
lcd_wcmd(0x38);
delay1ms(5);
lcd_wcmd(0x0c);
delay1ms(5);
lcd_wcmd(0x06);
delay1ms(5);
lcd_wcmd(0x01);
delay1ms(5);
}
void T1_Init()
{
TMOD |= 0x10;
IE = 0x8f;
IP=0x08;
TH1 = (65535-50000)/256;
TL1 = (65535-50000)%6;
ET1 = 1;
TR1 = 1;
}
void INT_Init()
{
TCON = 0x01;
}
void my_INT0(void) interrupt 0
{
M1++;
}
void timer1() interrupt 3
{
TH1 = (65535-50000)/256;
TL1 = (65535-50000)%6;
i=i+1;
if(i==20)
{
i=0;
bai1=(M1/100)+48;
shi1=(M10/10)+48;
ge1=(M10)+48;
M1=0;
}
}
void main()
{
delay1ms(10);
LcdInit();
INT_Init();
T1_Init();
TR1=1;
lcd_pos(0x02);
i=0;
while(dis1[i] != '\0')
{
lcd_wdat(dis1[i]);
i++;
};
i=0;
lcd_pos(0x43);
while(dis2[i] != '\0')
{
lcd_wdat(dis2[i]);
i++;
};
while(1)
{
// TR1=1;
lcd_pos(0x48);
lcd_wdat(bai1);
lcd_pos(0x49);
lcd_wdat(shi1);
lcd_pos(0x4A);
lcd_wdat(ge1);
// delay1ms(2);
}
}
霍尔元件
关于“霍尔元件测速”的分享就先到这里了,如大家还有什么想了解的,可以登录电子元件技术网www.cntronics.com查阅。