|
本人寫了一個電機測速的程序,電機軸上裝有傳感器,轉一圈發送40個脈沖,但是1602始終顯示0.
我的程序思路:用INTO和INT1控制加減速,用T0計時0.1毫秒,T1計數脈沖,看一秒收到了多少個脈沖,就測出了轉速,1602的頭文件已經測試過了沒問題,我感覺可能是T1計數出問題,但就是找不出原因
#include <REGX52.H>
#include <lcd1602.h>
unsigned int z=50; //???????
unsigned int counter=0;
unsigned int s=0,j=0;
sbit speed=P3^5;
sbit pwm=P1^3;
sbit k1=P3^2;
sbit k2=P3^3;
void delay(unsigned int t) //延時函數
{
unsigned int t1;
t1=t;
while(t1--);
}
void Int0Init() /配置INT0
{
IT0=1;//????????????????????
EX0=1;//??INT0???ж??????
EA=1;//?????ж?
}
void Int1Init() /配置INT1
{
IT1=1;//????????????????????
EX1=1;//??INT1???ж??????
}
void Timer0Init() /配置T0,計時0.1毫秒
{
TMOD=0X51; /設置TMOD
TH0=0xFF;
TL0=0x9C;
ET0=1;//???????0?ж?????
EA=1;//?????ж?
TR0=1;//???????
}
void Timer1Init()//配置T1,計數
{
TH1=0X00;
TL1=0x00;
TR1=1;//???????
}
void dingshi() interrupt 1 //計時中斷
{
TH0=0xFF;
TL0=0x9C;
counter++;
s++;
j++;
}
void jiasu() interrupt 0 //按鍵加速
{
delay(1000);
if(k1==0)
{
z=z+5;
}
}
void jiansu() interrupt 2 //按鍵減速
{
delay(1000);
if(k2==0)
{
z=z-5;
}
}
void main()
{
Timer0Init();
Timer1Init();
Int0Init();
Int1Init();
LCD1602_INT(); //1602初始化
while(1)
{
if(j>=100)
{j=0;}
if(j<=z)
{pwm=1;}
else
{pwm=0;}//產生PWM波
if(counter>=10000)//counter循環10000次,也就是10000*0.1毫秒就是1秒
{
counter=0;
speed=TH1*256+TL1;
TH1=0X00;/清零
TL1=0x00;//清零
}
if (s>=10000)//??????????????????
{ s=0;
LCD1602_WC(0X01);/清屏
delay(700);
LCD1602_DISDAT(1,1,speed*1.5);//給1602返回速度
LCD1602_DISCHAR(1,2,'r');
LCD1602_DISCHAR(1,3,'a');
LCD1602_DISCHAR(1,4,'d');
LCD1602_DISCHAR(1,5,'/');
LCD1602_DISCHAR(1,6,'m');
LCD1602_DISCHAR(1,7,'i');
LCD1602_DISCHAR(1,8,'n');
}
}
}
|
|