sbit Left_moto_pwm=P1^6 ;
sbit Right_moto_pwm=P1^7;
sbit p34=P3^4;
sbit p35=P3^5;
sbit p36=P3^6;
sbit p37=P3^7;
void Left_moto_go() //左电机正转
{p34=0;p35=1;}
void Left_moto_back() //左电机反转
{p34=1;p35=0;}
void Left_moto_stp() //左电机停转
{p34=1;p35=1;}
void Right_moto_go() //右电机正转
{p36=0;p37=1;}
void Right_moto_back() //右电机反转
{p36=1;p37=0;}
void Right_moto_stp() //右电机停转
{p36=1;p37=1;}
bit Left_moto_stop =1;
bit Right_moto_stop =1;
unsigned char pwm_val_left =0;
unsigned char push_val_left =0;
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;
void pwm_out_left_moto(void) //左电机调速
{
if(Left_moto_stop)
{
if(pwm_val_left<=push_val_left)
Left_moto_pwm=1;
else
Left_moto_pwm=0;
if(pwm_val_left>=10)
pwm_val_left=0;
}
else
Left_moto_pwm=0;
}
void pwm_out_right_moto(void) //右电机调速
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
Right_moto_pwm=1;
else
Right_moto_pwm=0;
if(pwm_val_right>=10)
pwm_val_right=0;
}
else
Right_moto_pwm=0;
}
unsigned char Left_Speed_Ratio; //左电机转速的设定值
unsigned char Right_Speed_Ratio; //右电机转速的设定值
void run(void) //小车前行
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_go();
Right_moto_go();
}
void back(void) //小车后退
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_back();
Right_moto_back();
}
void left(void) //小车左转
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Right_moto_go();
Left_moto_stp();
}
void right(void) //小车右转
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Right_moto_stp();
Left_moto_go();
}
void stop(void) //小车停止
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_stp();
Right_moto_stp();
}
void rotate(void) //小车原地转圈
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_back();
Right_moto_go();
}
void Timer0Init() //定时器初始化函数
{
TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X18;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
}
void timer0()interrupt 1 using 2 //定时器中断函数,此处配置为1ms产生一次中断,对PWM的输出进行控制
{
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X18;
time++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
void delay1s(void)
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
_nop_();
}
void main()
{
Timer0Init();
Left_Speed_Ratio=5; //设置左电机车速为最大车速的50%
Right_Speed_Ratio=5; 设置右电机车速为最大车速的50%
while(1)
{
run();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
back();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
left();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
right();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
stop();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
rotate();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
}
}
#include
unsigned char pwm_val_left =0;
unsigned char push_val_left =0;
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;
unsigned char Left_Speed_Ratio;
unsigned char Right_Speed_Ratio;
bit Left_moto_stop =1;
bit Right_moto_stop =1;
void Left_moto_go() //左电机正转
{p34=0;p35=1;}
void Left_moto_back() //左电机反转
{p34=1;p35=0;}
void Left_moto_stp() //左电机停转
{p34=1;p35=1;}
void Right_moto_go() //右电机正转
{p36=0;p37=1;}
void Right_moto_back() //右电机反转
{p36=1;p37=0;}
void Right_moto_stp() //右电机停转
{p36=1;p37=1;}
void pwm_out_left_moto(void) //左电机PWM
{
if(Left_moto_stop)
{
if(pwm_val_left<=push_val_left)
Left_moto_pwm=1;
else
Left_moto_pwm=0;
if(pwm_val_left>=10)
pwm_val_left=0;
}
else
Left_moto_pwm=0;
}
void pwm_out_right_moto(void) //右电机PWM
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
Right_moto_pwm=1;
else
Right_moto_pwm=0;
if(pwm_val_right>=10)
pwm_val_right=0;
}
else
Right_moto_pwm=0;
}
void run(void) //小车前行
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_go();
Right_moto_go();
}
void back(void) //小车后退
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_back();
Right_moto_back();
}
void left(void) //小车左转
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Right_moto_go();
Left_moto_stp();
}
void right(void) //小车右转
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Right_moto_stp();
Left_moto_go();
}
void stop(void) //小车停止
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_stp();
Right_moto_stp();
}
void rotate(void) //小车原地转圈
{
push_val_left =Left_Speed_Ratio;
push_val_right =Right_Speed_Ratio;
Left_moto_back();
Right_moto_go();
}
#include
extern unsigned char Left_Speed_Ratio;
extern unsigned char Right_Speed_Ratio;
unsigned int time=0;
extern unsigned char pwm_val_left;
extern unsigned char pwm_val_right;
void delay1s(void)
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
_nop_();
}
void Timer0Init()
{
TMOD|=0X01;//选择为定时器0模式,工作方式1,仅用TR0打开启动。
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X18;
ET0=1;//打开定时器0中断允许
EA=1;//打开总中断
TR0=1;//打开定时器
}
void timer0()interrupt 1 using 2
{
TH0=0XFC; //给定时器赋初值,定时1ms
TL0=0X18;
time++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}
void main()
{
Timer0Init();
Left_Speed_Ratio=5; //设置左电机车速为最大车速的50%
Right_Speed_Ratio=5; 设置右电机车速为最大车速的50%
while(1)
{
run();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
back();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
left();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
right();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
stop();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
rotate();
delay1s(); delay1s(); delay1s(); delay1s(); delay1s();
}
}
#ifndef __car_H
#define __car_H
#include
#include
sbit Left_moto_pwm=P1^6 ;
sbit Right_moto_pwm=P1^7;
sbit p34=P3^4;
sbit p35=P3^5;
sbit p36=P3^6;
sbit p37=P3^7;
void Left_moto_go() ;
void Left_moto_back() ;
void Left_moto_stp() ;
void Right_moto_go();
void Right_moto_back();
void Right_moto_stp();
void delay(unsigned int k) ;
void delay1s(void) ;
void pwm_out_left_moto(void) ;
void pwm_out_right_moto(void);
void run(void);
void back(void);
void left(void);
void right(void);
void stop(void);
void rotate(void);
本文到这里就结束了,本文完整的工程文件我会放在附件里,需要者自取,我放的时候都是免费的,但是过段时间它会自己涨…需要的在评论区留言我可以直接发给你,欢迎大家继续阅读本系列的后续文章“详细介绍如何从零开始制作51单片机控制的智能小车(二)———超声波模块、漫反射光电管、4路红外传感器的介绍和使用”
欢迎大家积极交流,本文未经允许谢绝转载