找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 828|回复: 0
打印 上一主题 下一主题
收起左侧

循迹小车程序

[复制链接]
跳转到指定楼层
楼主
ID:633408 发表于 2019-10-31 18:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit Motor_left_vcc=P1^2;  //控制左电机正极
sbit Motor_left_gnd=P1^3;  //控制左电机负极
sbit Motor_right_vcc=P1^1;  //控制右电机正极
sbit Motor_right_gnd=P1^0;  //控制右电机负极
sbit Left=P1^4;  //寻迹左  碰到黑线输入低电平
sbit Right=P1^5;  //寻迹右          高电平1 低电平0
void delay(uint x)
{
        uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void Motor_Pwm_Go(uint x)  // 直走占空比 1/x+10
{
        Motor_left_vcc=0;
        Motor_left_gnd=1;
        Motor_right_vcc=0;
        Motor_right_gnd=1;
        delay(5);
        Motor_left_vcc=0;
        Motor_left_gnd=0;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        delay(x);
}
void Motor_Pwm_Left(uint x)  //左转
{
        Motor_left_vcc=0;
        Motor_left_gnd=0;
        Motor_right_vcc=0;   //左转:右边电机转,左边电机不转
        Motor_right_gnd=1;
        delay(5);
        Motor_left_vcc=0;
        Motor_left_gnd=0;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        delay(x);
}
void Motor_Pwm_Right(uint x)  //右转
{
        Motor_left_vcc=0;
        Motor_left_gnd=1 ;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        delay(5);
        Motor_left_vcc=0;
        Motor_left_gnd=0;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        delay(x);
}
/*void Motor_Pwm_Stop()  //停
{
        Motor_left_vcc=0;
        Motor_left_gnd=0;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        }
void Motor_Pwm_Down(uint x)  //后退
{
        Motor_left_vcc=0;
        Motor_left_gnd=1;
        Motor_right_vcc=0;
        Motor_right_gnd=1;
        delay(6);
        Motor_left_vcc=0;
        Motor_left_gnd=0;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        delay(x);
}
*/
void main()
{
        if(Left==0)   //左边碰到黑线,右边不可能碰到黑线,所以不需要检测右边传感器的值
{
        Motor_Pwm_Left(10);  //左转
}
        if(Right==0)   //右边碰到黑线,
{
        Motor_Pwm_Right(10);  //左转
}
   if(Left==0&&Right==0)  //都没碰到黑线,直走
{
        Motor_Pwm_Go(10)  ;
}


       
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表