这是原程序不知道为什么不能调速,在下只是个刚刚开始学习
单片机的菜鸟。我用的是ln298驱动。程序中的PWM定义实际是ln298中的两个ena。还请大神们指点。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit M1A = P0^3;
sbit M1B = P0^4;
sbit M2A = P0^5;
sbit M2B = P0^6;
sbit Righ
tiR=P3^6; //右侧传感器
sbit LeftIR=P3^4; //左侧传感器
sbit FontIR=P3^7; //前传感器
sbit PWM=P0^2;
sbit PWM2=P0^7;
uchar count;
/*******************************T0,T1定时 *******************************/
void T0_init(void)
{
TMOD=0x11;
TH1=(65536-200)/256;
TL1=(65536-200)%256;
TH0=(65536-500)/256;
TL0=(65536-500)%256;
ET0=1;
TR0=1;
EA=1;
ET1=1;
TR1=1;
}
/*****T0,PWM调速*******/
void time0(void) interrupt 1
{ TH0=(65536-500)/256;
TL0=(65536-500)%256;
count++;
if(count<=200) PWM=0;
else PWM=1;
if(count>=500)
count=0;
if(count<200) PWM2=0;
else PWM2=1;
if(count>=500)
count=0;
}
void tingzhi() //停止
{
M1A=0;
M1B=0;
M2A=0;
M2B=0;
}
void qianjin() //前进
{
M1A=1;
M1B=0;
M2A=1;
M2B=0;
}
void houtui() //后退
{
M1A=0;
M1B=1;
M2A=0;
M2B=1;
}
void zuozhuan() //左转
{
M1A=0;
M1B=1;
M2A=1;
M2B=0;
}
void youzhuan() //右转
{
M1A=1;
M1B=0;
M2A=0;
M2B=1;
}
void delay(unsigned int i) //定时
{ uint j=0;
for(;i>0;i--)
{ for(j=0;j<125;j++)
{;}
}
}
/*****T1中断******/
void zhixian() interrupt 3
{
if(FontIR==0&&LeftIR==0&&RightIR==1)
youzhuan();
if(FontIR==0&&LeftIR==1&&RightIR==0)
zuozhuan();
if(FontIR==1&&LeftIR==0&&RightIR==1)
qianjin();
}
void main() //主程序
{
T0_init;
while(1)
{if(FontIR==0&&LeftIR==0&&RightIR==1)
youzhuan();
if(FontIR==0&&LeftIR==1&&RightIR==0)
zuozhuan();
if(FontIR==1&&LeftIR==0&&RightIR==1)
qianjin();
}
}
一周热门 更多>