找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3293|回复: 1
打印 上一主题 下一主题
收起左侧

自做蓝牙小车,代码源程序

[复制链接]
跳转到指定楼层
楼主
ID:188276 发表于 2017-4-10 21:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
简单实现前后左右

#include<AT89x51.H>

//HL-1小车驱动接线定义
#define A_left_moto_go  {P2_0 = 1,P2_1 = 0;}
#define A_left_moto_back {P2_0 = 0,P2_1 = 1;}
#define A_left_moto_stop {P2_0 = 0,P2_1 = 0;}
#define A_right_moto_go {P2_2 = 1,P2_3 = 0;}
#define A_right_moto_back {P2_2 = 0,P2_3 = 1;}
#define A_right_moto_stop {P2_2 = 0,P2_3 = 0;}

#define B_left_moto_go {P1_0 = 1,P1_1 = 0;}
#define B_left_moto_back {P1_0 = 0,P1_1 = 1;}
#define B_left_moto_stop {P1_0 = 0,P1_1 = 0;}
#define B_right_moto_go {P1_2 = 1,P1_3 = 0;}
#define B_right_moto_back {P1_2 = 0,P1_3 = 1;}
#define B_right_moto_stop {P1_2 = 0,P1_3 = 0;}                   

#define uint    unsigned int                          //重定义无符号整数类型
#define uchar   unsigned char                         //重定义无符号字符类型

#define led P0_0

/************************************************************************/       
//延时函数       
   void delay(unsigned int k)
{   
     unsigned int x,y;
         for(x=0;x<k;x++)
           for(y=0;y<2000;y++);
}

/************************************************************************/
void run()
{
        A_left_moto_go;
        A_right_moto_go;
        B_left_moto_go;
        B_right_moto_go;
}

void back_run()
{
        A_left_moto_back;
        A_right_moto_back;
        B_left_moto_back;
        B_right_moto_back;
}

void stop_run()
{
        A_left_moto_stop;
        A_right_moto_stop;
        B_left_moto_stop;
        B_right_moto_stop;
}

void left_run()
{
        A_left_moto_back;
        A_right_moto_go;
        B_left_moto_back;
        B_right_moto_go;
}

void right_run()
{
        A_left_moto_go;
        A_right_moto_back;
        B_left_moto_go;
        B_right_moto_back;
}



char uart_data; // 函数变量
char temp;

void main(void) //主函数
{       
        SCON=0X50; //设置串口方式为1 方式1
        TMOD=0X20;//设置为定时器1,
        TH1= 0x0FD; //赋初值
        TL1= 0x0FD;
        ES=1; //开串口中断
        EA=1; //开中中断
        TR1=1; //启动定时器1
        led = 1; //p2.0置1,灯灭
        while(1);
}


void serial_IT(void) interrupt 4 //中断程序
{
        if(RI==1) //R1置1.向cpu 申请中断,循环
{
        RI=0; //软件清零,取消申请
        uart_data = SBUF; //蓝牙输入数据赋值
        SBUF = uart_data; //返回终端
        temp = uart_data; //赋值
        if(temp == 0x00 ) led = ~led;
        switch(temp)
        {
                case 'a'         :        run();                 break;// 前进
                case 'b'        :         back_run();        break;//后退
                case 'c'        :        left_run();        break;//左转
                case 'd'        :        right_run();break;//右转
                case 'e'        :        stop_run();        break;//停止
        }
        }
        else T1 = 0; //中断关闭

}
         

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:268015 发表于 2018-1-15 19:45 | 只看该作者
有蓝牙接收代码吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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