附:http://www.jh-tec.cn/archives/7133
硬件设计
下边讲一下遥控小车,同样是基于51单片机,利用超声波进行测距,利用蓝牙模块实现遥控(仿真里边没有蓝牙,所以采用的是RS485通信模拟)
仿真图:
由LCD1602作为显示器,具有蜂鸣器报警,ULN2003A作为电机驱动,多个按键控制小车的方向和加减速,ADC0832作为ADC采集芯片。
程序设计:
#include<main.h>
#include<1602.h>
#include<motor.h>
#include<juli.h>
uint Flag_Front,Flag_Back,Flag_Left,Flag_Right,Flag_Jiasu,Flag_Jiansu,Flag_Stop;
/****
******* 延时1ms函数
*****/
void delay(uint x)
{
uint m,n;
for(m=x;m>0;m--)
for(n=110;n>0;n--);
}
/****
******* 串口初始化函数
*****/
void uart_init()
{
TMOD=0x20; //9600bps
SCON=0x50;
TH1=0xFD;
TL1=0xFD;
TR1=1;
ES=1;
EA=1;
UART_EN=0; //串口一直处于接收状态
}
/****
******* 主函数
*****/
void main()
{
uint dis_l,dis_r;
uart_init();
lcd_init();
// lcd_write_str(0x85, "--Welcome--");
// lcd_write_str(0x86+0x40, "--An Ke--");
lcd_write_str(0x80, "Status:Stop");
lcd_write_str(0x80+0x40, "Dis_L: Dis_R:");
while(1)
{
dis_l=Get_AD_JL_Left_Result();//获取浓度采样值
dis_l=dis_l*99/255;
write_str3(2, 0x06, dis_l);
delay(50);
dis_r=Get_AD_JL_Right_Result();//获取浓度采样值
dis_r=dis_r*99/255;
write_str3(2, 0x10, dis_r);
delay(50);
if(dis_l > 1 && dis_l < 10)
{
Flag_Front = 0;
Flag_Back = 0;
Flag_Left = 0;
Flag_Right = 1;
Flag_Jiasu = 0;
Flag_Jiansu = 0;
Flag_Stop = 0;
}
if(dis_l <= 1)
{
Flag_Front = 0;
Flag_Back = 0;
Flag_Left = 0;
Flag_Right = 0;
Flag_Jiasu = 0;
Flag_Jiansu = 0;
Flag_Stop = 1;
}
if(dis_r > 1 && dis_r < 10)
{
Flag_Front = 0;
Flag_Back = 0;
Flag_Left = 1;
Flag_Right = 0;
Flag_Jiasu = 0;
Flag_Jiansu = 0;
Flag_Stop = 0;
}
if(dis_r <= 1)
{
Flag_Front = 0;
Flag_Back = 0;
Flag_Left = 0;
Flag_Right = 0;
Flag_Jiasu = 0;
Flag_Jiansu = 0;
Flag_Stop = 1;
}
if((dis_l > 1 && dis_l < 10) || (dis_r > 1 && dis_r < 10))
BEEP = 0;
else
BEEP = 1;
if(Flag_Front == 1)
{
lcd_write_str(0x87, "Front ");
MotorFront(30);
}
if(Flag_Back == 1)
{
lcd_write_str(0x87, "Back ");
MotorBack(30);
}
if(Flag_Left == 1)
{
lcd_write_str(0x87, "Left ");
MotorLeft(30);
}
if(Flag_Right == 1)
{
lcd_write_str(0x87, "Right ");
MotorRight(30);
}
if(Flag_Jiasu == 1)
{
lcd_write_str(0x87, "Jiasu ");
MotorFront(20);
}
if(Flag_Jiansu == 1)
{
lcd_write_str(0x87, "Jiansu");
MotorFront(50);
}
if(Flag_Stop == 1)
{
lcd_write_str(0x87, "Stop ");
MotorStop();
}
}
}
/****
******* 串口中断服务程序函数
*****/