#include<reg52.h>
#include<intrins.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7; //电机口
sbit RS=P2^0; //液晶控制口
sbit RW=P2^1;
sbit E=P2^2;
sbit P25=P2^5; //循迹口
sbit P26=P2^6;
sbit P27=P2^7;
sbit P10=P1^0; //壁障口
sbit P11=P1^1;
sbit P12=P1^2;
sbit SS=P3^7; //语音控制口
sbit SCLK=P3^6;
sbit MOSI= P3^1;
sbit MISO =P3^0;
uchar SR0_L;
uchar SR0_H;
uchar SR1;
uchar c[8],dr,p,m,n,temp,d[4]={'T','i','m','e'},yy;
uint zz;
void delay1(uint z) //延时 z ms
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
// ++++++++语音控制程序+++++++++++++
void delay()
{ uchar i;
for(i=0;i<8;i++)
_nop_();
}
void delayms( uint tt)
{ uchar j;
while(tt--)
{ for(j=0;j<125;j++);}
}
uchar Senddata(uchar cmd)
{
uchar i,dat=cmd;
SCLK=1; // 悬空
SS=0;
for(i=0;i<8;i++){
SCLK=0;
delay();
if(dat&0x01){MOSI=1;}
else{MOSI=0;}
dat>>=1;
if(MISO){dat|=0x80;}
SCLK=1;
delay(); }
MOSI=0;
return(dat);
}
void ISD_Reset(void) //复位
{ Senddata(0x03);
Senddata(0x00);
SS=1;
delayms(50) ;
}
void ISD_PU(void)//上电
{ Senddata(0x01);
Senddata(0x00);
SS=1;
delayms(50);
}
void RdStatus(void)// 读取状态
{ Senddata(0x05);
Senddata(0x00);
Senddata(0x00);
SS=1;
delayms(10);//延迟10ms
SR0_L =Senddata(0x05);
SR0_H =Senddata(0x00);
SR1 =Senddata(0x00);
SS=1;
delayms(10);
}
void ClrInt(void)// 清除中断
{ Senddata(0x04);
Senddata(0x00);
SS=1;
delayms(10);//延迟10ms
}
void SetPLAY(uchar Add_ST_H, Add_ST_L, Add_ED_H, Add_ED_L)//定点播放
{
do{ RdStatus(); }while((SR0_L&0x01)||(!(SR1&0x01))); //if(SR0_L^0==1){system Err}<CMD_ERR==1>
ClrInt();
Senddata(0x90);
Senddata(0x00);
Senddata(Add_ST_L); //S7:S0 开始地址
Senddata(Add_ST_H); //S10:S8
Senddata(Add_ED_L); //E7:E0 结束地址
Senddata(Add_ED_H); //E10:E8
Senddata(0x00);
SS=1;
delayms(10); //延迟10ms
do{RdStatus();}while((SR0_L&0x01)||((SR1&0x04)));
}
void fun3() //语音控制 报时间
{
uchar e,ee;
if(zz==1)
{
TR0=0;
ISD_Reset(); //语音控制部分
ISD_PU();
delayms(50);
SetPLAY(0x00,0x85,0x00,0x8d); //总共行驶时间
switch(c[2]) //读分的个位
{
case 0: ee=1;break;
case 1: SetPLAY(0x00,0x15,0x00,0x1a),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 2: SetPLAY(0x00,0x1b,0x00,0x1f),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 3: SetPLAY(0x00,0x20,0x00,0x25),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 4: SetPLAY(0x00,0x26,0x00,0x2b),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 5: SetPLAY(0x00,0x2c,0x00,0x31),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 6: SetPLAY(0x00,0x32,0x00,0x36),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 7: SetPLAY(0x00,0x37,0x00,0x3c),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 8: SetPLAY(0x00,0x3d,0x00,0x40),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
case 9: SetPLAY(0x00,0x41,0x00,0x46),SetPLAY(0x00,0x50,0x00,0x58),ee=1;break;
}
if(ee==1)
{
switch(c[1]) //读秒十位
{
case 0:e=1;break;
case 1:SetPLAY(0x00,0x15,0x00,0x1a),SetPLAY(0x00,0x6b,0x00,0x73),e=1;break;
case 2:SetPLAY(0x00,0x1b,0x00,0x1f),SetPLAY(0x00,0x6b,0x00,0x73),e=1;break;
case 3:SetPLAY(0x00,0x20,0x00,0x25),SetPLAY(0x00,0x6b,0x00,0x73),e=1;break;
case 4:SetPLAY(0x00,0x26,0x00,0x2b),SetPLAY(0x00,0x6b,0x00,0x73),e=1;break;
case 5:SetPLAY(0x00,0x2c,0x00,0x31),SetPLAY(0x00,0x6b,0x00,0x73),e=1;break;
default:break;
}
ee=0;
}
if(e==1)
{
switch(c[0]) //读出秒的个位
{
case 1:SetPLAY(0x00,0x15,0x00,0x1a);break; //1
case 2:SetPLAY(0x00,0x1b,0x00,0x1f);break; //2
case 3:SetPLAY(0x00,0x20,0x00,0x25);break; //3
case 4:SetPLAY(0x00,0x26,0x00,0x2b);break; //4
case 5:SetPLAY(0x00,0x2c,0x00,0x31);break; //5
case 6:SetPLAY(0x00,0x32,0x00,0x36);break; //6
case 7:SetPLAY(0x00,0x37,0x00,0x3c);break; //7
case 8:SetPLAY(0x00,0x3d,0x00,0x40);break; //8
case 9:SetPLAY(0x00,0x41,0x00,0x46);break; //9
default:break;
}
e=0;
}
SetPLAY(0x00,0x62,0x00,0x6a);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++
//+++++++++液晶显示模块+++++++++++++++
void zhiling(uchar a) //写命令
{
E=0;RS=0;RW=0;
E=1;
P0=a;
delay1(5); //延时 5ms
E=0;
delay1(5); //延时 5ms
}
void shuju(uchar b) //写数据
{
E=0;RS=1;RW=0;
E=1;
P0=b;
delay1(5); //延时 5ms
E=0;
delay1(5); //延时 5ms
}
void init() //初始化
{
zhiling(0x01); //清屏
zhiling(0x38); //功能设置
zhiling(0x0e); //开/关显示设置
zhiling(0x06); //输入方式设置
}
//+++循迹子程序++++++++++++++++++++
void fun1() //循迹子程序
{
if(P25==1&&P26==0&&P27==1) //直走 P11是车前 P10 是车后
dr=1;
if(P25==0&&P26==1&&P27==1) //左拐
dr=2;
if(P25==1&&P26==1&&P27==0) //右拐
dr=3;
if(P10==0||P11==0) //进入壁障标志口
dr=4;
switch(dr)
{
case 1:P14=0,P15=1,P16=0,P17=1;break; //直走
case 2:P14=1,P15=0,P16=0,P17=1;break; // 左拐
case 3:P14=0,P15=1,P16=1,P17=0;break; // P14,P15,控制左电机
case 4:yy=1;
}
}
void fun2() //壁障子程序
{
if(P10==0&&P11==1&&P12==0) // 两边测 直走
{
P14=0,P15=1,P16=0,P17=1;
}
if(P10==0&&P11==0&&P12==1) // 左中测 右走
{
P14=1,P15=0,P16=0,P17=1;
delay1(1000);
}
if(P10==1&&P11==0&&P12==0) //右中测 左走
{
P14=0,P15=1,P16=1,P17=0;
delay1(1000);
}
if(P10==0&&P11==0&&P12==0) //全测到 直走
{
P14=0,P15=1,P16=0,P17=1,delay1(20);
}
if(P10==0&&P11==1&&P12==1)
{
P14=0,P15=1,P16=0,P17=1;
}
if(P10==1&&P11==1&&P12==0)
{
P14=0,P15=1,P16=0,P17=1;
}
if(P25==0&&P26==0&&P27==0) //车停
{
P14=1,P15=1,P16=1,P17=1;zz++;TR0=0;TR1=0;
}
}
void main()
{ char i;
TMOD=0x11; // 打开定时器和计数器
TH1=0X3c; //1ms fc18
TL1=0Xb0;
TH0=0XD8; //显示
TL0=0XF0;
EA = 1;
ET1=1;
TR1 = 1;
ET0=1;
TR0 = 1;
init();
zhiling(0x80); //第一行
for(i=0;i<4;i++)
shuju(d[i]);
shuju(' ');
shuju('1');
shuju(':');
while(1)
{ P14=1;
P15=1;
P16=1;
P17=1;
zhiling(0x80+0x08); //第一次前进时间
shuju(c[3]+'0'); //十分
shuju(c[2]+'0'); //分
shuju(':');
shuju(c[1]+'0'); //十秒
shuju(c[0]+'0'); //秒
fun3();//语音子程序
}
}
void timer1() interrupt 3 //中断 服务程序 T1
{
TH1=0X3c;
TL1=0Xb0;
if(yy==0)
fun1();
else
fun2();
}
void time0_int(void) interrupt 1 //显示中断 T0
{
TH0=0XD8;
TL0=0XF0;
p++;
if(p==130)
{
if(m==60)
{
n++;
m=0;
}
m++;
c[3]=n%100/10; //十分 前进时间显示
c[2]=n%1000%100%10; //分
c[1]=m%100/10; //十秒
c[0]=m%1000%100%10; //秒
p=0;
}
}
评论2
最新资源