找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1096|回复: 0
收起左侧

51单片机控制小车

[复制链接]
ID:419541 发表于 2018-11-27 19:47 | 显示全部楼层 |阅读模式
/*****************
小车运动
*****************/
#include<reg52.h>
#include<..\CONFIG\XIAOCHE51.h>
#define uint unsigned int
#define uchar unsigned char

void delay(uint z)
{
         uint x,y;
         for(x = z;x > 0;x++)
         for(y = 114;y > 0;y--);
}
/*小车前进*/
void forward()
{
    LEFT_MOTOR_EN;//左电机使能
        RIGHT_MOTOR_EN;//右电机使能
        LEFT_MOTOR_GO;// 左电机正转
        RIGHT_MOTOR_GO;//右电机正转       
}

/*小车后退*/
void backward()
{
        LEFT_MOTOR_EN;
        RIGHT_MOTOR_EN;
        LEFT_MOTOR_BACK;
        RIGHT_MOTOR_BACK;       
}

/*小车左转*/
void left()
{
        LEFT_MOTOR_STOPS;
        RIGHT_MOTOR_EN;
        RIGHT_MOTOR_GO;
}

/*小车右转*/
void right()
{
   RIGHT_MOTOR_STOPS;
   LEFT_MOTOR_EN;
   LEFT_MOTOR_GO;
}
/**************
小车初始化
**************/
#ifndef _XIAOCHE51_H_
#define _XIAOCHE51_H_
/*电机驱动IO定义*/
sbit ENA = P2^2;         //为1左电机使能
sbit IN1 = P2^3;        //为1左电机反转
sbit IN2 = P2^4;        //为1左电机正转
sbit IN3 = P2^5;         //为1右电机使能
sbit IN4 = P2^6;        //为1右电机正转
sbit ENB = P2^7;        //为1右电机反转

#define LEFT_MOTOR_EN       ENA = 1;//左电机使能
#define LEFT_MOTOR_STOPS    ENA = 0;//左电机停止
#define RIGHT_MOTOR_EN      ENB = 1;//右电机使能
#define RIGHT_MOTOR_STOPS   ENB = 0;//右电机停止

#define LEFT_MOTOR_GO       IN1 = 1,IN2 = 0;//左电机正转
#define LEFT_MOTOR_BACK     IN1 = 0,IN2 = 1;//左电机反转
#define RIGHT_MOTOR_GO      IN3 = 0,IN4 = 1;//右电机正转
#define RIGHT_MOTOR_BACK    IN3 = 1,IN4 = 0;//右电机反转


#endif


回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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