找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1512|回復: 0
收起左側

尋跡 小車程序

[復制鏈接]
ID:250208 發(fā)表于 2018-3-31 20:59 | 顯示全部樓層 |閱讀模式

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit Motor_left_vcc=P1^0;  //控制左電機正極
sbit Motor_left_gnd=P1^1;  //控制左電機負極
sbit Motor_right_vcc=P1^2;  //控制右電機正極
sbit Motor_right_gnd=P1^3;  //控制右電機負極
sbit Left=P1^4;  //尋跡左  碰到黑線輸入低電平
sbit Right=P1^5;  //尋跡右
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+1
{
        Motor_left_vcc=1;
        Motor_left_gnd=0;
        Motor_right_vcc=1;
        Motor_right_gnd=0;
        delay(1);
        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=1;   //左轉:右邊電機轉,左邊電機不轉
        Motor_right_gnd=0;
        delay(1);
        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=1;
        Motor_left_gnd=0;
        Motor_right_vcc=0;
        Motor_right_gnd=0;
        delay(1);
        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(1);
        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==1&&Right==1)  //都沒碰到黑線,直走
{
        Motor_Pwm_Go(10) ;
}
  
       
}
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表