搜档网
当前位置:搜档网 › 三轮智能小车程序

三轮智能小车程序

#include
#include <1602.h>
#include
sbit Right_moto_pwm = P1^4; //接驱动模块ENA使能端,输入PWM信号调节速度
sbit Left_moto_pwm = P1^5; //接驱动模块ENB使能端,输入PWM 信号调节速度
sbit IN1 = P1^0;
sbit IN2 = P1^1;
sbit IN3 = P1^2;
sbit IN4 = P1^3;
sbit Left_1_led = P2^0; //四路寻迹模块接口第一路
sbit Left_2_led = P2^1; //四路寻迹模块接口第二路
sbit zhong_3_led = P2^2; //四路寻迹模块接口第三路
sbit Right_4_led = P2^3; //四路寻迹模块接口第四路
#define Left_moto_go {IN1=0,IN2=1;} //左电机前进 0 ,1 口控制左电机
#define Left_moto_stop {IN1=1,IN2=1;} //左电机停转
#define Right_moto_go {IN3=0,IN4=1;} //右电机前转 2 3口控制右电机
#define Right_moto_stop {IN3=1,IN4=1;} //右电机停转
#define Left_moto_tui {IN1=1,IN2=0;} //左电机后退
#define Right_moto_tui {IN3=1,IN4=0;} //右电机后退
#define uchar unsigned char
#define uint unsigned int
uint t=0;
uchar pwm_val_left =0; //左电机占空比N/10
uchar push_val_left =0;
uchar pwm_val_right =0; //右电机占空比N/10
uchar push_val_right=0;
bit Right_moto_stp=1;
bit Left_moto_stp =1;
bit tui_flag = 1;
/********************前进********************/
void run(void) //前进函数
{
push_val_left = 6; //PWM 调节参数1-20 1为最慢,20是最快 改这个值可以改变其速度
push_val_right = 6; //PWM 调节参数1-20 1为最慢,20是最快 改这个值可以改变其速度
Left_moto_go //左电机前进
Right_moto_go //右电机前进bit Left_moto_stp ==1;
}
/************************************************************************/
void bleft(void) //大左转函数
{
push_val_left = 6;
push_val_right = 5;
Right_moto_go; //右电机继续
Left_moto_stop; //左电机停走
}

/************************************************************************/
void left(void) //小左转函数
{
push_val_left = 4;
push_val_right = 2;
Right_moto_go; //右电机继续
Left_moto_go; //左电机停走
}

/************************************************************************/
void bright(void) //大右转函数
{
push_val_left =6;
push_val_right = 5;
Right_moto_stop; //右电机停走
Left_moto_go; //左电机继续
}
/************************************************************************/
void right(void) //右转函数
{
push_val_left =2;
push_val_right = 4;
Right_moto_go; //右电机停走
Left_moto_go; //左电机继续
}
/************************************************************************/
void dao_run(void) //倒退函数
{
push_val_left = 6; //PWM 调节参数1-20 1为最慢,20是最快 改这个值可以改变其速度
push_val_right = 6; //PWM 调节参数1-20 1为最慢,20是最快 改这个值

可以改变其速度
Left_moto_tui ; //左电机前进
Right_moto_tui ; //右电机前进bit Left_moto_stp ==1;
}
void dao_right(void) //右转函数
{
push_val_left =2;
push_val_right = 4;
Right_moto_tui; //右电机停走
Left_moto_tui; //左电机继续
}
void dao_left(void) //小左转函数
{
push_val_left = 4;
push_val_right = 2;
Right_moto_tui; //右电机继续
Left_moto_tui; //左电机停走
}
///******************毫秒延时函数*********************/
//void Delayms(uint x)
//{
// uchar i;
// while(x--)
// for(i=0;i<120;i++);
//}
void stop(void)
{
Right_moto_stop; //右电机停走
Left_moto_stop; //左电机停走
//run();
}


/*************************PWM调制电机转速********************************/
void pwm_out_left_moto(void) //左电机调速,调节push_val_left的值改变电机转速,占空比
{
if(Left_moto_stp)
{
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) //右电机调速,调节push_val_left的值改变电机转速,占空比
{
if(Right_moto_stp)
{
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 xunji()
{
if(tui_flag==1)
{
switch(P2&0x0F)
{
case 0x0f: //全部不压线,直走
run(); break;
case 0x07: // 左压线,大左转
bleft(); break;
case 0x0b: // 左压线,左转
left(); break;
case 0x0E: // 右压线,大转右
bright(); break;
case 0x0D: // 右压线,转右
right(); break;
case 0x00:
stop(); break; //全部压线 停止
default:
run(); break;
}
}
else
{
switch(P2&0x0F)
{
case 0x0f: //全部不压线,直走
dao_run(); break;
// case 0x07: // 左压线,大左转
// bleft(); break;
case 0x0b: // 左压线,左转
dao_left(); break;
// case 0x0E: // 右压线,大转右
// tui_bright(); break;
case 0x0D: // 右压线,转右
dao_right(); break;
case 0x00:
stop(); break; //全部压线 停止
default:
dao_run(); break;
}
}

}
/***********TIMER0中断服务子函数产生PWM信号**********/
void timer0()interrupt 1
{
TH0=(65536-2000)/256; //0XF8; //2Ms定时
TL0=(65536-2000)%256; //0X30;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
if(P2&0x0f==0x00)
{
t++;
if(t==4)
{
tui_flag=0;
}
}
}


/***************************************************/
void main(void)
{
TMOD=0X01;
TH0= (65536-2000)/256; //0XF8; //2ms定时
TL0=

(65536-2000)%256; //0X30;
TR0= 1;
ET0= 1;
EA = 1;
while(1) /*无限循环*/
{
xunji();

}
}

相关主题