#include "config.h"
#include "delay.h"
#define S1_S0 0x40 //P_SW1.6
#define S1_S1 0x80 //P_SW1.7
#define uchar unsigned char
#define uint unsigned int
uchar USARTbuf; //设置8位的unsigend char型寄存器用来暂存串口接收内容
uchar a,b,c;
uint distance[5];
float S = 0;
bit flag = 0;
sbit RX = P1^1;
sbit TX = P1^2;
sbit front = P2^0;
sbit queen = P2^1;
sbit wrong = P2^2;
sbit dextro = P2^3;
sbit LED0 = P5^5; //运行指示灯
sbit LED1 = P2^4;
sbit LED2 = P2^5;
sbit LED3 = P2^6;
//sbit LED4 = P2^7;
//定时器初始化子程序
void initializer()
{
////***定时器0初始化***////
TH0 = 0;
TL0 = 0; //定时器0赋值
ET0 = 1; //定时器0中断开
TR0 = 1; //定时器0启动
////***串口初始化***////
SCON = 0x50; //8位数据,波特率9600
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
ACC = P_SW1; //
ACC &= ~(S1_S0 | S1_S1); //S1_S0=1 S1_S1=0
ACC |= S1_S0; //串口1切换到(P3.6/RxD_2, P3.7/TxD_2)
P_SW1 = ACC;
TR1 = 1; //启动定时器1
ES = 1; //串口中断开启
}
void front_and_back() //蓝牙控制
{
switch(USARTbuf)
{
case 0x32: //前ASCII码‘2’
{
front = 0;
queen = 1;
wrong = 1;
dextro = 1;
}
break;
case 0x38: //后ASCII码‘8’
{
front = 1;
queen = 0;
wrong = 1;
dextro = 1;
}
break;
case 0x34: //左ASCII码‘4’
{
wrong = 0;
dextro = 1;
}
break;
case 0x36: //右 ASCII码‘6’
{
wrong = 1;
dextro = 0;
}
break;
case 0x30: //前 ASCII码‘0’
{
front = 1;
queen = 1;
wrong = 1;
dextro = 1;
}
}
}
void LED_glint() //LED状态闪烁
{
if(!front && (wrong && dextro))
{
LED1 = 1;
LED2 = 0;
LED3 = 0;
}
if(!queen && (wrong && dextro))
{
LED1 = !LED1;
LED2 = 0;
LED3 = 0;
}
if(!front && !wrong)
{
LED1 = 1;
LED2 = !LED2;
LED3 = 0;
}
if(!front &&!dextro)
{
LED1 = 1;
LED2 = 0;
LED3 = !LED3;
}
if(!queen && !wrong)
{
LED1 = !LED1;
LED2 = !LED2;
LED3 = 0;
}
if(!queen && !dextro)
{
LED1 = !LED1;
LED2 = 0;
LED3 = !LED3;
}
if((queen && dextro) && (front && wrong) )
{
LED1 = !LED1;
LED2 = !LED2;
LED3 = !LED3;
}
}
void Delay20us()//@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 52;
while (--i);
}
void ultrasonic()
{
uint time;
TX = 0; //启动超声波模块
Delay20us();
TX = 1; //关闭超声波模块
while(!RX);//当RX为零时等待
TR0 = 1; //开启计数
while(RX);//当RX为1计数并等待
TR0 = 0;//关闭计数
time = TH0*256+TL0;
TH0 = 0;
TL0 = 0;
S = (time*1.87)/100; //算出来是CM
b = S;
}
void transform(uint temp)
{
uchar ge, shi, bai;
bai = temp / 100;
temp = temp % 100; //取余运算
shi = temp / 10;
temp = temp % 10; //取余运算
ge = temp;
if(S >= 100) distance[0] = bai + 0x30;
else distance[0] = 0x00; //百位没有数据时清零
if(S >= 10) distance[1] = shi + 0x30;
else distance[1] = 0x00; //十位没有数据时清零
//distance[0] = bai + 0x30;
//distance[1] = shi + 0x30;
distance[2] = ge + 0x30; //ASC码转换
distance[3] = 0x63; //‘c’
distance[4] = 0x6d; //‘m'
distance[5] = 0x2f; //'/'分隔符
}
void main()
{
initializer(); //定时器初始化
EA = 1;//开总中断
LED1 = 0;
LED2 = 0;
LED3 = 0;
while(1) //神奇的大循环
{
delay_ms(200);
LED_glint();
ultrasonic();
LED0 = !LED0;
transform(S);
c++;
if(c == 3) //大约1s发送一次
{
uchar i;
for(i = 0; i < 6; i++) //发送字符串ASC码
{
SBUF = distance[i];
while(!TI);
TI = 0;
}
c = 0;
}
if(b < 30) //防止撞车判断
{
front = 1;
queen = 0;
wrong = 1;
dextro = 1;
delay_ms(100);
queen = 1;
}
}
}
void UART () interrupt 4
{
if(RI) //如果接收标志位为1,说明有数据接收完毕
{ //RCIF在寄存器被读出后自动清零
USARTbuf=SBUF; //将接收缓冲区内容转至USARTbuf寄存器中
RI=0; //清除接收标志位
SBUF = USARTbuf;
while(!TI);
TI = 0;
}
front_and_back(); //蓝牙控制子程序
}
void zd0 (void) interrupt 1
{
flag = 1;
}
#include "delay.h"
#define S1_S0 0x40 //P_SW1.6
#define S1_S1 0x80 //P_SW1.7
#define uchar unsigned char
#define uint unsigned int
uchar USARTbuf; //设置8位的unsigend char型寄存器用来暂存串口接收内容
uchar a,b,c;
uint distance[5];
float S = 0;
bit flag = 0;
sbit RX = P1^1;
sbit TX = P1^2;
sbit front = P2^0;
sbit queen = P2^1;
sbit wrong = P2^2;
sbit dextro = P2^3;
sbit LED0 = P5^5; //运行指示灯
sbit LED1 = P2^4;
sbit LED2 = P2^5;
sbit LED3 = P2^6;
//sbit LED4 = P2^7;
//定时器初始化子程序
void initializer()
{
////***定时器0初始化***////
TH0 = 0;
TL0 = 0; //定时器0赋值
ET0 = 1; //定时器0中断开
TR0 = 1; //定时器0启动
////***串口初始化***////
SCON = 0x50; //8位数据,波特率9600
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
ACC = P_SW1; //
ACC &= ~(S1_S0 | S1_S1); //S1_S0=1 S1_S1=0
ACC |= S1_S0; //串口1切换到(P3.6/RxD_2, P3.7/TxD_2)
P_SW1 = ACC;
TR1 = 1; //启动定时器1
ES = 1; //串口中断开启
}
void front_and_back() //蓝牙控制
{
switch(USARTbuf)
{
case 0x32: //前ASCII码‘2’
{
front = 0;
queen = 1;
wrong = 1;
dextro = 1;
}
break;
case 0x38: //后ASCII码‘8’
{
front = 1;
queen = 0;
wrong = 1;
dextro = 1;
}
break;
case 0x34: //左ASCII码‘4’
{
wrong = 0;
dextro = 1;
}
break;
case 0x36: //右 ASCII码‘6’
{
wrong = 1;
dextro = 0;
}
break;
case 0x30: //前 ASCII码‘0’
{
front = 1;
queen = 1;
wrong = 1;
dextro = 1;
}
}
}
void LED_glint() //LED状态闪烁
{
if(!front && (wrong && dextro))
{
LED1 = 1;
LED2 = 0;
LED3 = 0;
}
if(!queen && (wrong && dextro))
{
LED1 = !LED1;
LED2 = 0;
LED3 = 0;
}
if(!front && !wrong)
{
LED1 = 1;
LED2 = !LED2;
LED3 = 0;
}
if(!front &&!dextro)
{
LED1 = 1;
LED2 = 0;
LED3 = !LED3;
}
if(!queen && !wrong)
{
LED1 = !LED1;
LED2 = !LED2;
LED3 = 0;
}
if(!queen && !dextro)
{
LED1 = !LED1;
LED2 = 0;
LED3 = !LED3;
}
if((queen && dextro) && (front && wrong) )
{
LED1 = !LED1;
LED2 = !LED2;
LED3 = !LED3;
}
}
void Delay20us()//@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 52;
while (--i);
}
void ultrasonic()
{
uint time;
TX = 0; //启动超声波模块
Delay20us();
TX = 1; //关闭超声波模块
while(!RX);//当RX为零时等待
TR0 = 1; //开启计数
while(RX);//当RX为1计数并等待
TR0 = 0;//关闭计数
time = TH0*256+TL0;
TH0 = 0;
TL0 = 0;
S = (time*1.87)/100; //算出来是CM
b = S;
}
void transform(uint temp)
{
uchar ge, shi, bai;
bai = temp / 100;
temp = temp % 100; //取余运算
shi = temp / 10;
temp = temp % 10; //取余运算
ge = temp;
if(S >= 100) distance[0] = bai + 0x30;
else distance[0] = 0x00; //百位没有数据时清零
if(S >= 10) distance[1] = shi + 0x30;
else distance[1] = 0x00; //十位没有数据时清零
//distance[0] = bai + 0x30;
//distance[1] = shi + 0x30;
distance[2] = ge + 0x30; //ASC码转换
distance[3] = 0x63; //‘c’
distance[4] = 0x6d; //‘m'
distance[5] = 0x2f; //'/'分隔符
}
void main()
{
initializer(); //定时器初始化
EA = 1;//开总中断
LED1 = 0;
LED2 = 0;
LED3 = 0;
while(1) //神奇的大循环
{
delay_ms(200);
LED_glint();
ultrasonic();
LED0 = !LED0;
transform(S);
c++;
if(c == 3) //大约1s发送一次
{
uchar i;
for(i = 0; i < 6; i++) //发送字符串ASC码
{
SBUF = distance[i];
while(!TI);
TI = 0;
}
c = 0;
}
if(b < 30) //防止撞车判断
{
front = 1;
queen = 0;
wrong = 1;
dextro = 1;
delay_ms(100);
queen = 1;
}
}
}
void UART () interrupt 4
{
if(RI) //如果接收标志位为1,说明有数据接收完毕
{ //RCIF在寄存器被读出后自动清零
USARTbuf=SBUF; //将接收缓冲区内容转至USARTbuf寄存器中
RI=0; //清除接收标志位
SBUF = USARTbuf;
while(!TI);
TI = 0;
}
front_and_back(); //蓝牙控制子程序
}
void zd0 (void) interrupt 1
{
flag = 1;
}