#include "STC12C5A.H"
#include "lcd1602.h"
#include "delay.h"
#include "string.h"
#include "typedef.h"
#include "key.h"
#include "mp3_uart.h"
#include "24c02.h"
sbit BUZZER = P2^0; //蜂鸣器
sbit RLED = P1^0; //红灯
sbit GLED = P1^1; //绿灯
sbit TX1=P1^2;
sbit RX1=P1^3;
unsigned char OverFlag=0;
unsigned int Distance=0;
unsigned int AlarmDistance=20;
unsigned char AutoFlag=0;
uint Speed=0;
uint AlarmSpeed=80;
unsigned char Page=0;
unsigned int Pulse=0; //测速脉冲数
sbit INA=P3^4;
extern unsigned char PlayFlag;
void Timer0_Init(void); //1毫秒@11.0592MHz
void Timer1_Init(void); //定时器1初始化
void GetDistance(void); //超声波测距
void DistanceCheck(void); //测距函数
void UART_Init(void); //串口初始化
void PageInit(void);
void DataLoad(void);
void PageDisplay(void);
void PlayCheck(void)
{
static unsigned char con=0;
static unsigned char T50MS=0;
static unsigned char TINT=0;
if(KeyVal=='S')
{
KeyVal=0;
AutoFlag=(AutoFlag==1)?0:1;
}
if(AutoFlag)
GLED=0;
else
GLED=1;
if(AutoFlag)
con++;
if(con>=80)
{
con=0;
MP3_UART_Send_Cmd(0x41,15); //速度
if(Speed==0)
{
MP3_UART_Send_Cmd(0x41,1); //速度
}
else
{
MP3_UART_Send_Cmd(0x41,Speed/10+1); //速度
MP3_UART_Send_Cmd(0x41,Speed%10+1); //速度
}
/*--------------------------------------------------------*/
MP3_UART_Send_Cmd(0x41,14); //距离
MP3_UART_Send_Cmd(0x41,Distance/100+1);
MP3_UART_Send_Cmd(0x41,12); //点
MP3_UART_Send_Cmd(0x41,Distance%100/10+1);
MP3_UART_Send_Cmd(0x41,Distance%10+1);
MP3_UART_Send_Cmd(0x41,13);
/*----------------------------------------------------------*/
}
/*----------------------------小于50每10里面播报一次------------------------------------------------*/
if((TINT==0)&&(Distance%10==0)&&(Distance<51)&&(Distance>0))
{
MP3_UART_Send_Cmd(0x41,14); //距离
MP3_UART_Send_Cmd(0x41,Distance/100+1);
MP3_UART_Send_Cmd(0x41,12); //点
MP3_UART_Send_Cmd(0x41,Distance%100/10+1);
MP3_UART_Send_Cmd(0x41,Distance%10+1);
MP3_UART_Send_Cmd(0x41,13);
TINT=1;
}
if(TINT>0)
{
TINT++;
if(TINT>=30)
TINT=0;
}
/*------------------------距离太近请停车播报---------------------------------------*/
if((Distance<=10)&&(T50MS==0)&&(Distance>0))
{
MP3_UART_Send_Cmd(0x41,16);
T50MS=1;
}
if(T50MS>0)
{
T50MS++;
if(T50MS>=20)
T50MS=0;
}
if((Distance<AlarmDistance)&&(Distance>0))
{
BUZZER=0;
RLED=0;
}
else
{
BUZZER=1;
RLED=1;
}
}
void main(void)
{
Delay_1ms(500);
LCD_Init(); //LCD显示初始化
PageInit(); //页面显示初始化
Timer0_Init(); //计数器0初始化
Timer1_Init(); //定时器1初始化
DataLoad(); //报警数据读出
UART_Init(); //串口初始化
MP3_UART_SetVic(23);
Delay_1ms(200);
MP3_UART_SetVic(23);
while(1)
{
PageDisplay(); //页面显示
PlayCheck();
GetDistance();
Delay_1ms(50); //延时50毫秒
}
}
void UART_Server() interrupt 4 //串口接收中断
{
unsigned char ch=0;
EA=0;
if(RI)
{
ch = SBUF;
if(ch=='S'||ch=='T'||ch=='P')
PlayFlag=0;
RI = 0;
}
EA=1;
}
void Timer0_ISR(void) interrupt 1 //1毫秒@11.0592MHz
{
static unsigned char sta=0;
static unsigned int T1MS=0;
static unsigned char KS=0;
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
T1MS++;
if(T1MS>=1000)
{
T1MS=0;
Speed=Pulse/2;
Pulse=0;
}
/*--------------------------------------------*/
KS++;
if(KS>=20)
{
KS=0;
KEY_Scan();
}
/*---------------------------------------------*/
switch(sta)
{
/*------------------------------------------*/
case 0:
{
if(INA==0)
sta=1;
else
sta=0;
}break;
/*------------------------------------------*/
case 1:
{
if(INA==0)
{
sta=2;
Pulse++;
}
else
sta=0;
}break;
/*------------------------------------------*/
case 2:
{
if(INA==0)
sta=2;
else
sta=0;
}break;
default:sta=0;break;
}
/*----------------------------------------------*/
}
void Timer0_Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
void UART_Init(void)
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //独立波特率发生器时钟为Fosc,即1T
BRT = 0xDC; //设定独立波特率发生器重装值
AUXR |= 0x01; //串口1选择独立波特率发生器为波特率发生器
AUXR |= 0x10; //启动独立波特率发生器
ES = 1;
EA=1;
}
void Timer1_Init(void)
{
TMOD = (TMOD&0x0f)| 0x10;
TL1 = 0x00; //设定定时初值
TH1 = 0x00; //设定定时器重装值
ET1 = 1; //禁止定时器0中断
TR1 = 0; //启动定时器0
EA=1;
}
void DistanceCheck(void)
{
unsigned int retry=0;
unsigned int time=0;
float S=0;
TR1 = 0;
OverFlag=0;
TX1 = 1;
delay_1us(3);
TX1 = 0;
while(!RX1);
TH1=0;
TL1=0;
TR1 = 1;
retry=0;
while((RX1==1)&&(OverFlag==0)&&(retry<50000))
{
retry++;
delay_1us(1);
}
TR1 = 0;
/*--------------计算---------------------*/
time=TH1*256+TL1;
TH1=0;
TL1=0;
if(OverFlag==0) //没有溢出
{
S=(time*1.7)/100; //算出来是CM
if(S>300)
{
S=0;
}
}
else
{
S=0;
}
Distance=S;
}
void GetDistance(void)
{
static unsigned char con=0;
con++;
if(con>20)
{
con=0;
DistanceCheck();
}
}
void DataLoad(void)
{
U16_Read(&AlarmSpeed,0);
Delay_1ms(50);
U16_Read(&AlarmDistance,10);
}
void PageInit(void)
{
LCD_Clr();
switch(Page)
{
/*---------------------------------------------*/
case 0: //速度和距离同时显示
{
LCD_DispStr( 0,"Speed: cm/s");
LCD_DispStr(16,"Dis: cm");
}break;
/*---------------------------------------------*/
case 1: //显示距离 和设置距离
{
LCD_DispStr( 0,"Dis: cm");
LCD_DispStr(16,"Set: cm");
}break;
/*---------------------------------------------*/
case 2: //显示速度 和设置速度
{
LCD_DispStr( 0,"Speed: cm/s");
LCD_DispStr(16,"Alarm: cm/s");
}break;
/*---------------------------------------------*/
default:break;
}
}
void PageDisplay(void)
{
/*---------------页面切换----------------*/
if(KeyVal=='E')
{
KeyVal=0;
Page++;
if(Page>2)
Page=0;
PageInit();
}
switch(Page)
{
/*---------------------------------------------*/
case 0://速度和距离同时显示
{
LCD_DispU16( 6,Speed);
if(Distance>0)
LCD_DispU16(22,Distance);
else
LCD_DispStr(22,"-----");
}break;
/*---------------------------------------------*/
case 1: //显示距离 和设置距离
{
if(Distance>0)
LCD_DispU16( 6,Distance);
else
LCD_DispStr(6,"-----");
LCD_DispU16(22,AlarmDistance);
if(KeyVal=='+')
{
KeyVal=0;
AlarmDistance++;
U16_Save(&AlarmDistance,10);
}
if(KeyVal=='-')
{
KeyVal=0;
if(AlarmDistance>2)
AlarmDistance--;
U16_Save(&AlarmDistance,10);
}
}break;
/*---------------------------------------------*/
case 2: //显示速度 和设置速度
{
LCD_DispU16( 6,Speed);
LCD_DispU16(22,AlarmSpeed);
if(KeyVal=='+')
{
KeyVal=0;
AlarmSpeed++;
U16_Save(&AlarmSpeed,0);
}
if(KeyVal=='-')
{
KeyVal=0;
if(AlarmSpeed>2)
AlarmSpeed--;
U16_Save(&AlarmSpeed,0);
}
}break;
/*---------------------------------------------*/
default:
{
Page=0;
PageInit();
}break;
}
}
|