基于51单片机转速测量的程序,望高手帮我解释一下。
这段程序是基于51单片机的霍尔转速测量的代码,传感器脉冲信号给单片机经此程序传给4位数码管,显示转速,单位是rpm,望高手能不能把重点地方给我解释下原理,尤其是转速是如何...
这段程序是基于51单片机的霍尔转速测量的代码,传感器脉冲信号给单片机经此程序传给4位数码管,显示转速,单位是rpm,望高手能不能把重点地方给我解释下原理,尤其是转速是如何计算的。
#include <reg51.h>
#include <INTRINS.H>
#define FOSC 11.0592
unsigned int speed;
unsigned char key;
unsigned char led_table[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xff,0xf6};
long speed_count,speed_count1;
unsigned char speed_check;
unsigned int second_flag;
unsigned char speed_table[4];
unsigned char speed_table1[10];
//定时器频率1000Hz
void timer1_init(void)
{
EA=0;
TMOD=0x10;
//TH1=1000*FOSC/12/256;
//TL1=(unsigned int)(1000*FOSC/12)%256;
TH1=254;
TL1=51;
TR1=1;
TCON|=0X04; //外部中断下降触发
EX1=1; //允许外部中断
ET1=1;
EA=1;
}
void int1_routine(void) interrupt 2
{
speed_count++;
P1^=0X02;
}
void timer1_routine(void) interrupt 3
//void timer1_routine(void)
{
TH1=254;
//TH1=0xf0;
TL1=51;
//speed_count++;
second_flag++;
}
void main(void)
{
unsigned int i,j,k;
// unsigned char sum;
unsigned speed_table[3];
long time_id;
timer1_init();
speed=0;
P2=0x0f;
P0=0xf0;
P3=0x08;
key=0;
P1=0x00;
speed_check=0;
while(1)
{
//P1^=0XFE;
j=1;
//speed=1234;
speed_table[0]=speed/1000;
speed_table[1]=speed%1000/100;
speed_table[2]=speed%100/10;
speed_table[3]=speed%10;
for(i=0;i<4;i++)
{
P2=j^0xff;
j<<=1;
P0=(led_table[speed_table[i]])&0xfe;
for(k=0;k<250;k++) ;
}
//speed=speed_count;
if(second_flag>=1000)
{
second_flag=0;
time_id++;
speed_table1[time_id%10]=speed_count;
speed_count=0;
speed=(speed_table1[0]+speed_table1[1]+speed_table1[2]+speed_table1[3]+speed_table1[4]+speed_table1[5]+speed_table1[6]+speed_table1[7]+speed_table1[8]+speed_table1[9])*6;
//speed=speed/2+(speed_table[0]+speed_table[1]+speed_table[2])*6;
}
}
} 展开
#include <reg51.h>
#include <INTRINS.H>
#define FOSC 11.0592
unsigned int speed;
unsigned char key;
unsigned char led_table[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xff,0xf6};
long speed_count,speed_count1;
unsigned char speed_check;
unsigned int second_flag;
unsigned char speed_table[4];
unsigned char speed_table1[10];
//定时器频率1000Hz
void timer1_init(void)
{
EA=0;
TMOD=0x10;
//TH1=1000*FOSC/12/256;
//TL1=(unsigned int)(1000*FOSC/12)%256;
TH1=254;
TL1=51;
TR1=1;
TCON|=0X04; //外部中断下降触发
EX1=1; //允许外部中断
ET1=1;
EA=1;
}
void int1_routine(void) interrupt 2
{
speed_count++;
P1^=0X02;
}
void timer1_routine(void) interrupt 3
//void timer1_routine(void)
{
TH1=254;
//TH1=0xf0;
TL1=51;
//speed_count++;
second_flag++;
}
void main(void)
{
unsigned int i,j,k;
// unsigned char sum;
unsigned speed_table[3];
long time_id;
timer1_init();
speed=0;
P2=0x0f;
P0=0xf0;
P3=0x08;
key=0;
P1=0x00;
speed_check=0;
while(1)
{
//P1^=0XFE;
j=1;
//speed=1234;
speed_table[0]=speed/1000;
speed_table[1]=speed%1000/100;
speed_table[2]=speed%100/10;
speed_table[3]=speed%10;
for(i=0;i<4;i++)
{
P2=j^0xff;
j<<=1;
P0=(led_table[speed_table[i]])&0xfe;
for(k=0;k<250;k++) ;
}
//speed=speed_count;
if(second_flag>=1000)
{
second_flag=0;
time_id++;
speed_table1[time_id%10]=speed_count;
speed_count=0;
speed=(speed_table1[0]+speed_table1[1]+speed_table1[2]+speed_table1[3]+speed_table1[4]+speed_table1[5]+speed_table1[6]+speed_table1[7]+speed_table1[8]+speed_table1[9])*6;
//speed=speed/2+(speed_table[0]+speed_table[1]+speed_table[2])*6;
}
}
} 展开
3个回答
展开全部
#include <reg51.h>
#include <INTRINS.H>
#define FOSC 11.0592
unsigned int speed;
unsigned char key;
unsigned char led_table[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xff,0xf6};
long speed_count,speed_count1;
unsigned char speed_check;
unsigned int second_flag;
unsigned char speed_table[4];
unsigned char speed_table1[10];
//定时器频率1000Hz
void timer1_init(void)
{
EA=0;
TMOD=0x10;
//TH1=1000*FOSC/12/256;
//TL1=(unsigned int)(1000*FOSC/12)%256;
TH1=254;
TL1=51;
TR1=1;
TCON|=0X04; //外部中断下降触发
EX1=1; //允许外部中断
ET1=1;
EA=1;
}
void int1_routine(void) interrupt 2
{
speed_count++;
P1^=0X02;
}
void timer1_routine(void) interrupt 3
//void timer1_routine(void)
{
TH1=254;
//TH1=0xf0;
TL1=51;
//speed_count++;
second_flag++;
}
void main(void)
{
unsigned int i,j,k;
// unsigned char sum;
unsigned speed_table[3];
long time_id;
timer1_init();
speed=0;
P2=0x0f;
P0=0xf0;
P3=0x08;
key=0;
P1=0x00;
speed_check=0;
while(1)
{
//P1^=0XFE;
j=1;
//speed=1234;
speed_table[0]=speed/1000;
speed_table[1]=speed%1000/100;
speed_table[2]=speed%100/10;
speed_table[3]=speed%10;
for(i=0;i<4;i++)
{
P2=j^0xff;
j<<=1;
P0=(led_table[speed_table[i]])&0xfe;
for(k=0;k<250;k++) ;
}
//speed=speed_count;
if(second_flag>=1000) /*中断1000次进入 */
{
second_flag=0;
time_id++;
speed_table1[time_id%10]=speed_count;/*外部中断量转存(是很聪明的方法学习了)*/
speed_count=0;
speed=(speed_table1[0]+speed_table1[1]+speed_table1[2]+speed_table1[3]+speed_table1[4]+speed_table1[5]+speed_table1[6]+speed_table1[7]+speed_table1[8]+speed_table1[9])*6; /*(10X6)10秒的外部中断量乘以6就是转速了*/
//speed=speed/2+(speed_table[0]+speed_table[1]+speed_table[2])*6;
}
}
}
#include <INTRINS.H>
#define FOSC 11.0592
unsigned int speed;
unsigned char key;
unsigned char led_table[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xff,0xf6};
long speed_count,speed_count1;
unsigned char speed_check;
unsigned int second_flag;
unsigned char speed_table[4];
unsigned char speed_table1[10];
//定时器频率1000Hz
void timer1_init(void)
{
EA=0;
TMOD=0x10;
//TH1=1000*FOSC/12/256;
//TL1=(unsigned int)(1000*FOSC/12)%256;
TH1=254;
TL1=51;
TR1=1;
TCON|=0X04; //外部中断下降触发
EX1=1; //允许外部中断
ET1=1;
EA=1;
}
void int1_routine(void) interrupt 2
{
speed_count++;
P1^=0X02;
}
void timer1_routine(void) interrupt 3
//void timer1_routine(void)
{
TH1=254;
//TH1=0xf0;
TL1=51;
//speed_count++;
second_flag++;
}
void main(void)
{
unsigned int i,j,k;
// unsigned char sum;
unsigned speed_table[3];
long time_id;
timer1_init();
speed=0;
P2=0x0f;
P0=0xf0;
P3=0x08;
key=0;
P1=0x00;
speed_check=0;
while(1)
{
//P1^=0XFE;
j=1;
//speed=1234;
speed_table[0]=speed/1000;
speed_table[1]=speed%1000/100;
speed_table[2]=speed%100/10;
speed_table[3]=speed%10;
for(i=0;i<4;i++)
{
P2=j^0xff;
j<<=1;
P0=(led_table[speed_table[i]])&0xfe;
for(k=0;k<250;k++) ;
}
//speed=speed_count;
if(second_flag>=1000) /*中断1000次进入 */
{
second_flag=0;
time_id++;
speed_table1[time_id%10]=speed_count;/*外部中断量转存(是很聪明的方法学习了)*/
speed_count=0;
speed=(speed_table1[0]+speed_table1[1]+speed_table1[2]+speed_table1[3]+speed_table1[4]+speed_table1[5]+speed_table1[6]+speed_table1[7]+speed_table1[8]+speed_table1[9])*6; /*(10X6)10秒的外部中断量乘以6就是转速了*/
//speed=speed/2+(speed_table[0]+speed_table[1]+speed_table[2])*6;
}
}
}
艾普斯
2024-07-18 广告
2024-07-18 广告
稳频稳压电源哪家好?艾普斯电源(苏州)有限公司开始专业研发、制造及营销交流稳压电源,满足全球电子及信息业对电源设备日益蓬勃的市场需求。迄今为止,业已发展成为交流不间断电源、稳压电源、变频电源、中频航空- 军事专用电源、直流电源、逆变电源等产...
点击进入详情页
本回答由艾普斯提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询