自己程序如下,能达到测速和控速,上位机通讯规则这卡住,求大佬
#include <AT89X51.H>
unsigned char t,zkb = 2,num;
unsigned int count,speed;
bit flag;
time0() interrupt 1 using 0
{
TH0=(65536-1000)/256;//1ms
TH0=(65536-1000)%256;
t++;
num++;
if(num>4)//周期4ms
num=1;
if(num<=zkb)
P1_1=0;
else
P1_1=1;
}
void main()
{
TMOD=0X21;
SCON=0X50;
PCON=0X80;
TH1=0XF3;
TL1=0XF3;
TH0=(65536-1000)/256;//25ms
TH0=(65536-1000)%256;
EA=1;
ET0=1;
ET1=0;
TR0=1;
TR1=1;
while(1)
{
if((!P1_0)&&flag)//不要用while 不好
{
count++;
flag = 0;
}
else if(P1_0)
flag = 1;
if(t>=200)//1s
{
t=0;speed=count*6;
TI=0;SBUF='h';while(!TI);
TI=0;SBUF=speed/256;while(!TI);
TI=0;SBUF='l';while(!TI);
TI=0;SBUF=speed%256;while(!TI);
speed=0;count=0;
}
}
}
|