单片机利用pwm控制电机!速度,L298N有四个输入端,两个使能端,求产生两路pwm波的程序!
答案:3 悬赏:60 手机版
解决时间 2021-04-16 13:32
- 提问者网友:末路
- 2021-04-16 03:37
单片机利用pwm控制电机!速度,L298N有四个输入端,两个使能端,求产生两路pwm波的程序!
最佳答案
- 五星知识达人网友:老鼠爱大米
- 2021-04-16 04:19
这是利用L298驱动两个直流电机的(来源于智能车循迹)程序,
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit en1=P1^0;
sbit en2=P1^1;
sbit s1=P1^2;
sbit s2=P1^3;
sbit s3=P1^4;
sbit s4=P1^5;
uchar t=0;
uchar m1=0;
uchar m2=0;
uchar tmp1,tmp2;
void motor(uchar index, char speed)
{
if(speed>=-100 && speed<=100)
{
if(index==1)
{
m1=abs(speed);
if(speed<0)
{
s1=0;
s2=1;
}
else
{
s1=1;
s2=0;
}
}
if(index==2)
{
m2=abs(speed);
if(speed<0)
{
s3=0;
s4=1;
}
else
{
s3=1;
s4=0;
}
}
}
}
void delay(uint j)
{
for(j;j>0;j--);
}
void main()
{
uchar i;
TMOD=0x02;
TH0=0x9B;
TL0=0x9B;
EA=1;
ET0=1;
TR0=1;
while(1)
{
for(i=0;i<=100;i++)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=0;i<=100;i++)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
}
}
void timer0() interrupt 1
{
if(t==0)
{
tmp1=m1;
tmp2=m2;
}
if(t if(t t++;
if(t>=100) t=0;
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit en1=P1^0;
sbit en2=P1^1;
sbit s1=P1^2;
sbit s2=P1^3;
sbit s3=P1^4;
sbit s4=P1^5;
uchar t=0;
uchar m1=0;
uchar m2=0;
uchar tmp1,tmp2;
void motor(uchar index, char speed)
{
if(speed>=-100 && speed<=100)
{
if(index==1)
{
m1=abs(speed);
if(speed<0)
{
s1=0;
s2=1;
}
else
{
s1=1;
s2=0;
}
}
if(index==2)
{
m2=abs(speed);
if(speed<0)
{
s3=0;
s4=1;
}
else
{
s3=1;
s4=0;
}
}
}
}
void delay(uint j)
{
for(j;j>0;j--);
}
void main()
{
uchar i;
TMOD=0x02;
TH0=0x9B;
TL0=0x9B;
EA=1;
ET0=1;
TR0=1;
while(1)
{
for(i=0;i<=100;i++)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=0;i<=100;i++)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
}
}
void timer0() interrupt 1
{
if(t==0)
{
tmp1=m1;
tmp2=m2;
}
if(t
if(t>=100) t=0;
全部回答
- 1楼网友:北方的南先生
- 2021-04-16 05:07
你要说清楚用什么单片机,51要用两个定时器,AvR可以直接输出PWM
- 2楼网友:逃夭
- 2021-04-16 04:59
这是利用L298驱动两个直流电机的(来源于智能车循迹)程序,
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit en1=P1^0;
sbit en2=P1^1;
sbit s1=P1^2;
sbit s2=P1^3;
sbit s3=P1^4;
sbit s4=P1^5;
uchar t=0;
uchar m1=0;
uchar m2=0;
uchar tmp1,tmp2;
void motor(uchar index, char speed)
{
if(speed>=-100 && speed<=100)
{
if(index==1)
{
m1=abs(speed);
if(speed<0)
{
s1=0;
s2=1;
}
else
{
s1=1;
s2=0;
}
}
if(index==2)
{
m2=abs(speed);
if(speed<0)
{
s3=0;
s4=1;
}
else
{
s3=1;
s4=0;
}
}
}
}
void delay(uint j)
{
for(j;j>0;j--);
}
void main()
{
uchar i;
TMOD=0x02;
TH0=0x9B;
TL0=0x9B;
EA=1;
ET0=1;
TR0=1;
while(1)
{
for(i=0;i<=100;i++)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=0;i<=100;i++)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
}
}
void timer0() interrupt 1
{
if(t==0)
{
tmp1=m1;
tmp2=m2;
}
if(t if(t t++;
if(t>=100) t=0;
有两路PWM,希望对你有帮助!
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit en1=P1^0;
sbit en2=P1^1;
sbit s1=P1^2;
sbit s2=P1^3;
sbit s3=P1^4;
sbit s4=P1^5;
uchar t=0;
uchar m1=0;
uchar m2=0;
uchar tmp1,tmp2;
void motor(uchar index, char speed)
{
if(speed>=-100 && speed<=100)
{
if(index==1)
{
m1=abs(speed);
if(speed<0)
{
s1=0;
s2=1;
}
else
{
s1=1;
s2=0;
}
}
if(index==2)
{
m2=abs(speed);
if(speed<0)
{
s3=0;
s4=1;
}
else
{
s3=1;
s4=0;
}
}
}
}
void delay(uint j)
{
for(j;j>0;j--);
}
void main()
{
uchar i;
TMOD=0x02;
TH0=0x9B;
TL0=0x9B;
EA=1;
ET0=1;
TR0=1;
while(1)
{
for(i=0;i<=100;i++)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,i);
motor(2,i);
delay(5000);
}
for(i=0;i<=100;i++)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
for(i=100;i>0;i--)
{
motor(1,-i);
motor(2,-i);
delay(5000);
}
}
}
void timer0() interrupt 1
{
if(t==0)
{
tmp1=m1;
tmp2=m2;
}
if(t
if(t>=100) t=0;
有两路PWM,希望对你有帮助!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯