51单片机电机测速问题
#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharsbitdula=P2^1;sbitwela...
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^1;
sbit wela=P2^0;
uint temp,num,t1n,t0n,t0n0,t0n1,n;
uchar shiwan,wan,qian,bai,shi,ge,n0;
void delay(uint z);
void timer0();
void timer1();
void init();
void display(uchar wan,uchar qian,uchar bai,uchar shi,uchar ge);
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
init();
while(1)
{
while(TL0==0x05)
{
TR1=0;
TR0=0;
TH1=0x00;
TL1=0x00;
TL0=0x00;
n=TH1*256+TL1;
wan=n/10000%10;
qian=n/1000%10;
bai=n/100%10;
shi=n%100/10;
ge=n%10;
TH0=0x00;
TR1=1;
TR0=1;
}
display(wan,qian,bai,shi,ge);
}
}
void init()
{
TMOD=0x15;
TH1=0x00;
TH1=0x00;
TH0=0x00;
TL0=0x00;
EA=1;
ET1=1;
ET0=1;
TR1=1;
TR0=1;
n=0;
}
void timer0() interrupt 1
{
}
void timer1() interrupt 3
{
}
void display(uchar wan,uchar qian,uchar bai,uchar shi,uchar ge)
{
dula=1;
P1=table[wan];
dula=0;
// P1=0xff;
wela=1;
P1=0xf7;
wela=0;
delay(1);
dula=1;
P1=table[qian];
dula=0;
// P1=0xff;
wela=1;
P1=0xef;
wela=0;
delay(1);
dula=1;
P1=table[bai];
dula=0;
// P1=0xff;
wela=1;
P1=0xdf;
wela=0;
delay(1);
dula=1;
P1=table[shi];
dula=0;
// P1=0xff;
wela=1;
P1=0xbf;
wela=0;
delay(1);
dula=1;
P1=table[ge];
dula=0;
// P1=0xff;
wela=1;
P1=0x7f;
wela=0;
delay(1);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
我想用这个程序测电机速度。首先测霍尔元件检测到的脉冲,用T0计数,T1定时,计数一圈读出定时器的TH1和TL1然后换算显示。我之前的写的程序是定时器定时,计数器计数霍尔元件的脉冲,单位时间的脉冲数,通过计算显示,显示的要么是60的倍数要么是120的倍数,不够精确。所以想写个更精确的,请高手们帮忙看看。 展开
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^1;
sbit wela=P2^0;
uint temp,num,t1n,t0n,t0n0,t0n1,n;
uchar shiwan,wan,qian,bai,shi,ge,n0;
void delay(uint z);
void timer0();
void timer1();
void init();
void display(uchar wan,uchar qian,uchar bai,uchar shi,uchar ge);
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
init();
while(1)
{
while(TL0==0x05)
{
TR1=0;
TR0=0;
TH1=0x00;
TL1=0x00;
TL0=0x00;
n=TH1*256+TL1;
wan=n/10000%10;
qian=n/1000%10;
bai=n/100%10;
shi=n%100/10;
ge=n%10;
TH0=0x00;
TR1=1;
TR0=1;
}
display(wan,qian,bai,shi,ge);
}
}
void init()
{
TMOD=0x15;
TH1=0x00;
TH1=0x00;
TH0=0x00;
TL0=0x00;
EA=1;
ET1=1;
ET0=1;
TR1=1;
TR0=1;
n=0;
}
void timer0() interrupt 1
{
}
void timer1() interrupt 3
{
}
void display(uchar wan,uchar qian,uchar bai,uchar shi,uchar ge)
{
dula=1;
P1=table[wan];
dula=0;
// P1=0xff;
wela=1;
P1=0xf7;
wela=0;
delay(1);
dula=1;
P1=table[qian];
dula=0;
// P1=0xff;
wela=1;
P1=0xef;
wela=0;
delay(1);
dula=1;
P1=table[bai];
dula=0;
// P1=0xff;
wela=1;
P1=0xdf;
wela=0;
delay(1);
dula=1;
P1=table[shi];
dula=0;
// P1=0xff;
wela=1;
P1=0xbf;
wela=0;
delay(1);
dula=1;
P1=table[ge];
dula=0;
// P1=0xff;
wela=1;
P1=0x7f;
wela=0;
delay(1);
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
我想用这个程序测电机速度。首先测霍尔元件检测到的脉冲,用T0计数,T1定时,计数一圈读出定时器的TH1和TL1然后换算显示。我之前的写的程序是定时器定时,计数器计数霍尔元件的脉冲,单位时间的脉冲数,通过计算显示,显示的要么是60的倍数要么是120的倍数,不够精确。所以想写个更精确的,请高手们帮忙看看。 展开
3个回答
展开全部
要注意轻重缓急,改了一些如下:
void main()
{
init();
while(1) {
while(TL0 == 0x05) {
TR1 = 0;
TR0 = 0;
n = TH1 * 256 + TL1; //应该先读出数据.
TH1 = 0x00; //再对各单元清零.
TL1 = 0x00;
TH0 = 0x00;
TL0 = 0x00;
TR1 = 1; //马上启动,继续计数.
TR0 = 1;
wan = n/10000%10; //然后再慢慢算这些.
qian = n/1000%10;
bai = n/100%10;
shi = n%100/10;
ge = n%10;
}
display(wan, qian, bai, shi, ge);
}
}
void main()
{
init();
while(1) {
while(TL0 == 0x05) {
TR1 = 0;
TR0 = 0;
n = TH1 * 256 + TL1; //应该先读出数据.
TH1 = 0x00; //再对各单元清零.
TL1 = 0x00;
TH0 = 0x00;
TL0 = 0x00;
TR1 = 1; //马上启动,继续计数.
TR0 = 1;
wan = n/10000%10; //然后再慢慢算这些.
qian = n/1000%10;
bai = n/100%10;
shi = n%100/10;
ge = n%10;
}
display(wan, qian, bai, shi, ge);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询