最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

蓝牙小车51单片机C语言代码

来源:动视网 责编:小OO 时间:2025-09-23 06:43:45
文档

蓝牙小车51单片机C语言代码

#include#include#defineucharunsignedchar#defineuintunsignedintucharBuffer[4]={0};//从串口接收的数据uinti,j;sbitLeft_Positive=P1^4;sbitLeft_Negative=P1^5;sbitRight_Positive=P1^6;sbitRight_Negative=P1^7;sbitLeftLight=P2^1;sbitRightLight=P2^2;/****************
推荐度:
导读#include#include#defineucharunsignedchar#defineuintunsignedintucharBuffer[4]={0};//从串口接收的数据uinti,j;sbitLeft_Positive=P1^4;sbitLeft_Negative=P1^5;sbitRight_Positive=P1^6;sbitRight_Negative=P1^7;sbitLeftLight=P2^1;sbitRightLight=P2^2;/****************
#include

#include

#define uchar unsigned char

#define uint unsigned int

uchar Buffer[4] = {0}; //从串口接收的数据

uint i,j;

sbit Left_Positive=P1^4;

sbit Left_Negative=P1^5;

sbit Right_Positive=P1^6;

sbit Right_Negative=P1^7;

sbit LeftLight=P2^1;

sbit RightLight=P2^2;

/********************************************************************

* 名称 : Delay_1ms()

* 功能 : 延时子程序,延时时间为 1ms * x

* 输入 : x (延时一毫秒的个数)

* 输出 : 无

***********************************************************************/

void Delay_1ms(uint i)//1ms延时

{

uchar x,j;

for(j=0;jfor(x=0;x<=148;x++);

}

/********************************************************************

* 名称 : Com_Int()

* 功能 : 串口中断子函数

* 输入 : 无

* 输出 : 无

***********************************************************************/

void Com_Int(void) interrupt 4

{

EA = 0;

if(RI == 1) //当硬件接收到一个数据时,RI会置位

{

if(SBUF==65) //这里减去48是因为从电脑中发送过来的数据是ASCII码。 Buffer[0] = SBUF - 48;

{

Buffer[0] = 3; //A

}

if(SBUF==68)

{

Buffer[0] = 4; // D

}

if(SBUF==83)

{

Buffer[0] = 2; //S

}

if(SBUF==87)

{

Buffer[0] = 1; //W

}

if(SBUF==69)

{

Buffer[0] = 0; //E

}

RI = 0;

}

EA = 1;

}

/********************************************************************

* 名称 : Com_Init()

* 功能 : 串口初始化,晶振11.0592,波特率9600,使串口中断

* 输入 : 无

* 输出 : 无

***********************************************************************/

void Com_Init(void)

{

TMOD = 0x20;

PCON = 0x00;

SCON = 0x50;

TH1 = 0xFd; //设置波特率 9600

TL1 = 0xFd;

TR1 = 1; //启动定时器1

ES = 1; //开串口中断

EA = 1; //开总中断

}

/********************************************************************

* 名称 :qianjin()

* 功能 : 电机1、2启动,都是前进,整车表现为前进。

* 输入 : 无

* 输出 : 无 问题

***********************************************************************/

void qianjin()

{

Right_Negative=0;

Left_Positive=0;

Left_Negative=1;

Right_Positive=1;

}

/********************************************************************

* 名称 :houtui()

* 功能 : 电机1、2启动,都是后退,整车表现为后退。

* 输入 : 无

* 输出 : 无

***********************************************************************/

void houtui()

{

Left_Positive=0;

Right_Positive=0;

Right_Negative=1;

Left_Negative=1;

}

/********************************************************************

* 名称 :zuozhuan()

* 功能 : 电机1后退,电机2前进,整车表现为左转。

* 输入 : 无

* 输出 : 无

***************************************************

********************/

void zuozhuan()

{

Left_Negative=0;

Right_Positive=0;

Right_Negative=1;

Left_Positive=1;

}

/********************************************************************

* 名称 :youzhuan()

* 功能 : 电机1前进,电机2后退,整车表现为右转。

* 输入 : 无

* 输出 : 无

***********************************************************************/

void youzhuan()

{

Right_Negative=0;

Left_Negative=0;

Left_Positive=1;

Right_Positive=1;

}

/********************************************************************

* 名称 :tingche()

* 功能 : 电机1停止,电机2停止,整车表现为停止。

* 输入 : 无

* 输出 : 无

***********************************************************************/

void tingche()

{

Right_Negative=0;

Left_Negative=0;

Left_Positive=0;

Right_Positive=0;

}

void main()

{

Delay_1ms(100);

Com_Init();//串口初始化

while(1)//循环结构,选择函数控制小车的状态

{

switch(Buffer[0])

{

case 0: tingche(); break;

case 1: qianjin(); break;

case 2: houtui(); break;

case 3: zuozhuan(); break;

case 4: youzhuan(); break;

default:break;

}

}

}

文档

蓝牙小车51单片机C语言代码

#include#include#defineucharunsignedchar#defineuintunsignedintucharBuffer[4]={0};//从串口接收的数据uinti,j;sbitLeft_Positive=P1^4;sbitLeft_Negative=P1^5;sbitRight_Positive=P1^6;sbitRight_Negative=P1^7;sbitLeftLight=P2^1;sbitRightLight=P2^2;/****************
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top