兼职项目 智能自行车
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
2.1 KiB

#include "pwm.h"
unsigned char pwm_val_left =0;//变量定义
unsigned char push_val_left =0;// 左电机占空比N/20
unsigned char pwm_val_right =0;
unsigned char push_val_right=0;// 右电机占空比N/20
unsigned char pwm_left=0;
bit Right_moto_stop=1;
bit Left_moto_stop =1;
unsigned int time1=0;
/************************************************************************/
//延时函数
void delay(unsigned int k)
{
unsigned int x,y;
for(x=0;x<k;x++)
for(y=0;y<2000;y++);
}
/************************************************************************/
//前速前进
void run(void)
{
push_val_left=3; //速度调节变量 0-20。。。0最小,20最大
push_val_right=pwm_left;
Left_moto_go ; //左电机往前走
Right_moto_go ; //右电机往前走
}
void backrun(void)
{
push_val_left=3; //速度调节变量 0-20。。。0最小,20最大
push_val_right=4;
Left_moto_back; //左电机往前走
Right_moto_back; //右电机往前走
}
//左转
void leftrun(void)
{
push_val_left=0;
push_val_right=4;
Right_moto_go ; //右电机往前走
}
//右转
void rightrun(void)
{
push_val_right=0;
push_val_left=4;
Left_moto_go ; //左电机往前走
}
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>=20)
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>=20)
pwm_val_right=0;
}
else
{
Right_moto_pwm=0;
}
}
void timer1()interrupt 3
{
TH1=0XFc; //1Ms定时
TL1=0X18;
time1++;
// pwm_val_left++;
pwm_val_right++;
// pwm_out_left_moto();
pwm_out_right_moto();
}