|
#include <reg52.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
/*****按照原圖接線定義******/
sbit L293D_IN1=P0^0; //左電機(jī)
sbit L293D_IN2=P0^1;
sbit L293D_IN3=P0^2; //右電機(jī)
sbit L293D_IN4=P0^3;
sbit L293D_EN1=P0^4;
sbit L293D_EN2=P0^5;
sbit BUZZ=P0^6; //蜂鳴器
sbit S4=P3^2;
uchar pwm,count;
void Delay1ms(unsigned int x)
{
uchar y,z;
for(y=x;y>0;y--)
for(z=120;z>0;z--);
}
//=========================================================================================================================
void Forward()// 前進(jìn)
{
L293D_IN1=1;
L293D_IN2=0;
L293D_IN3=1;
L293D_IN4=0;
}
void stop() //停止
{
L293D_IN1=0;
L293D_IN2=0;
L293D_IN3=0;
L293D_IN4=0;
}
void keyscan()
{
if(S4==0)
{
Delay1ms(10);
if(S4==0)
{
TR0=1;
Forward();
BUZZ=0; //50次檢測(cè)K3確認(rèn)是按下之后,蜂鳴器發(fā)出“滴”聲響,然后啟動(dòng)小車。
Delay1ms(50);
BUZZ=1;//響50ms后關(guān)閉蜂鳴器 f
}
}
}
/*************主程序********************/
void main(void)
{
stop();
TMOD=0x02;
TH0=210; //50us進(jìn)入中斷
TL0=210;
ET0=1;
EA=1;
pwm=200;
while(1)
{
keyscan();
}
}
void time0() interrupt 1
{
count++;
if(count==pwm)
{
L293D_IN2=1;
L293D_IN4=1;
}
if(count==250)
{
count=0;
L293D_IN2=0;
L293D_IN4=0;
}
}
利用PWM波控制轉(zhuǎn)速,結(jié)果左右輪胎總是無法同步,求解
|
|