剛學(xué)單片機(jī)的時候做的一個智能小車,現(xiàn)在把當(dāng)時的一些資料整理了一下,希望對剛?cè)腴T的童鞋有幫助!
1、下圖為單片機(jī)的最小系統(tǒng),可以自己用萬用板焊接,順便練練焊接能力也不錯的。本設(shè)計采用了51單片機(jī),網(wǎng)上關(guān)于51單片機(jī)的資料有一大堆,大伙可以從它來入門,推薦郭天祥的視頻(我就是跟著他學(xué)的),講的挺詳細(xì)的。




4、下面我把源代碼貼出來,感興趣的童鞋可以看看希望有豪幫助!
- 以下是“CarDrive.c”文件中的源程序:
#include "CarDrive.h"
//左電機(jī)正轉(zhuǎn)
void LeftMotorCorotation(void)
{
LeftMotor_1 = 1;
LeftMotor_2 = 0;
}
//左電機(jī)反轉(zhuǎn)
void LeftMotorRollback(void)
{
LeftMotor_1 = 0;
LeftMotor_2 = 1;
}
//右電機(jī)正轉(zhuǎn)
void RightMotorCorotation(void)
{
RightMotor_1 = 1;
RightMotor_2 = 0;
}
//右電機(jī)反轉(zhuǎn)
void RightMotorRollback(void)
{
RightMotor_1 = 0;
RightMotor_2 = 1;
}
//小車前進(jìn)
void CarGoAhead(void)
{
LeftMotorCorotation();
RightMotorRollback();
}
//小車慢速左拐
void CarTurnLeft_Low(void)
{
LeftMotor_1 = 0;
LeftMotor_2 = 0;
RightMotorRollback();
}
//小車慢速右拐
void CarTurnRight_Low(void)
{
RightMotor_1 = 0;
RightMotor_2 = 0;
LeftMotorCorotation();
}
//小車快速左拐
void CarTurnLeft_High(void)
{
LeftMotorRollback();
RightMotorRollback();
}
//小車快速右拐
void CarTurnRight_High(void)
{
LeftMotorCorotation();
RightMotorCorotation();
}
//小車道路判斷
void CarRoadJudge(void)
{
if(LeftPhotoelectricCell_1&&(!LeftPhotoelectricCell_2)&&(!RightPhotoelectricCell_2)&&RightPhotoelectricCell_1)
CarGoAhead();
if(LeftPhotoelectricCell_1&&(!LeftPhotoelectricCell_2)&&RightPhotoelectricCell_2&&RightPhotoelectricCell_1)
CarGoAhead();
if(LeftPhotoelectricCell_1&&LeftPhotoelectricCell_2&&(!RightPhotoelectricCell_2)&&RightPhotoelectricCell_1)
CarGoAhead();
if((!LeftPhotoelectricCell_1)&&(!LeftPhotoelectricCell_2)&&RightPhotoelectricCell_2&&RightPhotoelectricCell_1)
CarTurnLeft_Low();
if(LeftPhotoelectricCell_1&&LeftPhotoelectricCell_2&&(!RightPhotoelectricCell_2)&&(!RightPhotoelectricCell_1))
CarTurnRight_Low();
if((!LeftPhotoelectricCell_1)&&LeftPhotoelectricCell_2&&RightPhotoelectricCell_2&&RightPhotoelectricCell_1)
CarTurnLeft_High();
if(LeftPhotoelectricCell_1&&LeftPhotoelectricCell_2&&RightPhotoelectricCell_2&&(!RightPhotoelectricCell_1))
CarTurnRight_High();
if(LeftPhotoelectricCell_1&&LeftPhotoelectricCell_2&&RightPhotoelectricCell_2&&RightPhotoelectricCell_1)
//CarTurnRight_Low();
CarGoAhead();
if((!LeftPhotoelectricCell_1)&&(!LeftPhotoelectricCell_2)&&(!RightPhotoelectricCell_2)&&(!RightPhotoelectricCell_1))
CarGoAhead();
}
- 以下是“CarDrive.h”文件中的源程序:
#ifndef _CarDrive_H_
#define _CarDrive_H_
#include
#define uint unsigned int
#define uchar unsigned char
sbit LeftMotor_1 = P0^0;
sbit LeftMotor_2 = P0^1;
sbit RightMotor_1 = P0^2;
sbit RightMotor_2 = P0^3;
sbit LeftPhotoelectricCell_1 = P3^0;
sbit LeftPhotoelectricCell_2 = P3^1;
sbit RightPhotoelectricCell_2 = P3^2;
sbit RightPhotoelectricCell_1 = P3^3;
void CarGoAhead(void);
//void CarBackOff(void);
void CarTurnLeft_Low(void);
void CarTurnRight_Low(void);
void CarTurnLeft_High(void);
void CarTurnRight_High(void);
//void CarBrake(void);
void CarRoadJudge(void);
#endif
- 以下是“main.h”中的程序:
#ifndef _main_H_
#define _main_H_
#include
#include "CarDrive.h"
#define uint unsigned int
#define uchar unsigned char
#endif
- 以下是“main.c”中的程序:
#include "main.h"
void main(void)
{
while(1)
{
CarRoadJudge();
}
}
5、下面是小車的視頻
總結(jié):好了,電路圖、程序源碼、視頻都在這了,大家多動手才是,最后感謝童鞋們花費寶貴的時間來瀏覽我的博客,我是“香蓮清風(fēng)”咱們下次見!