您现在的位置是:首页 >学无止境 >【C51】10-基础51单片机的小车项目(51完结)网站首页学无止境
【C51】10-基础51单片机的小车项目(51完结)
简介【C51】10-基础51单片机的小车项目(51完结)
10.1小车的安装
10.2电机模块的开发(L9110S)
接通
VCC
,
GND
模块电源指示灯亮, 以下资料来源官方,但是不对,根据下节课实际调试
IA1
输入高电平,
IA1
输入低电平,【
OA1 OB1
】电机正转;
IA1
输入低电平,
IA1
输入高电平,【
OA1 OB1
】电机反转;
IA2
输入高电平,
IA2
输入低电平,【
OA2 OB2
】电机正转;
IA2
输入低电平,
IA2
输入高电平,【
OA2 OB2
】电机反转;
10.3驱动电机模块让小车动起来(前后左右)
#include "reg52.h"
#include "intrins.h"
sbit RightCon1A = P3^2;
sbit RightCon1B = P3^3;
sbit LeftCon1A = P3^4;
sbit LeftCon1B = P3^5;
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void goForward()
{
LeftCon1A = 0;
LeftCon1B = 1;
RightCon1A = 0;
RightCon1B = 1;
}
void goRight()
{
LeftCon1A = 0;
LeftCon1B = 1;
RightCon1A = 0;
RightCon1B = 0;
}
void goLeft()
{
LeftCon1A = 0;
LeftCon1B = 0;
RightCon1A = 0;
RightCon1B = 1;
}
void goBack()
{
LeftCon1A = 1;
LeftCon1B = 0;
RightCon1A = 1;
RightCon1B = 0;
}
void main()
{
while(1){
goForward();
Delay1000ms();
Delay1000ms();
goBack();
Delay1000ms();
Delay1000ms();
goLeft();
Delay1000ms();
Delay1000ms();
goRight();
Delay1000ms();
Delay1000ms();
}
}
10.4通过串口控制小车方向
1.通过串口调试工具 利用串口中断
#include "reg52.h"
#include "motor.h"
#include <string.h>
#define SIZE 12
sfr AUXR = 0x8E;
char buffer[SIZE];
void UartInit(void) //9600bps@11.0592MHz
{
AUXR = 0x01;
SCON = 0x50; //配置串口工作方式1,REN使能接收
TMOD &= 0x0F;
TMOD |= 0x20;//定时器1工作方式位8位自动重装
TH1 = 0xFD;
TL1 = 0xFD;//9600波特率的初值
TR1 = 1;//启动定时器
EA = 1;//开启总中断
ES = 1;//开启串口中断
}
//通过串口中断的方式进行 M1 前 M2 后 M3左 M4右
void Uart_Handler() interrupt 4
{
static int i = 0;//静态变量,被初始化一次
char tmp;
if(RI)//中断处理函数中,对于接收中断的响应
{
RI = 0;//清除接收中断标志位
tmp = SBUF;
if(tmp == 'M'){
i = 0;
}
buffer[i++] = tmp;
//灯控指令
if(buffer[0] == 'M'){
switch(buffer[1]){
case '1':
goForward();
break;
case '2':
goBack();
break;
case '3':
goLeft();
break;
case '4':
goRight();
break;
default:
stop();
break;
}
}
if(i == 12){
i = 0;
memset(buffer, '