📄 smartcar.txt
字号:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit beep=P2^6;
char code tab0[]={0,1,2,3,4,5,6,7,8,9,10};//************
char code tab1[]={10,9,8,7,6,5,4,3,2,1,0};//************
uchar num,position=5,beep_num,temp0=0x11,temp1=0x11,i,j;
void pos() interrupt 1
{
uchar temp;
temp=P3&0x3f;
switch(temp)
{
case 1:position=10 ;break;
case 3:position=9 ;break;
case 2:position=8 ;break;
case 6:position=7 ;break;
case 4:position=6 ;break;
case 12:position=5 ;break;
case 8:position=4 ;break;
case 24:position=3 ;break;
case 16:position=2 ;break;
case 48:position=1 ;break;
case 32:position=0 ;break;
case 63:num++ ;break;
default: break;
}
}
void delay(uint t) //单位延时程序
{
uchar x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
void motor(uchar position) //电机程序
{
uchar delay_right,delay_left;
i++;
j++;
delay_right=tab0[position];
delay_left =tab1[position];
if(i>=delay_left)
{
i=0;
temp0=_crol_(temp0,1);
}
P0=temp0;
if(j>=delay_right)
{
j=0;
temp1=_crol_(temp1,1);
}
P1=temp1;
delay(10);
}
void slow() //减速程序
{
delay(20);
motor(position);
}
void t1_beeper() interrupt 3 //关蜂鸣器
{
beep_num++;
if(beep_num==100)
{
beep_num=0;
beep=1;
TR1=0;
}
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
}
void init() //初始化程序
{
EA=1;
ET0=1;
ET1=1;
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TR0=1;
}
void delay_t1(uchar n) //到站停5S
{
uchar x;
ET0=0;
for(x=0;x<n;x++)
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
while(!TF0);
TF0=0;
}
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
}
void main()
{
uchar stop,i=10;
init();
while(!stop) //判断是否到了终点
{
motor(position); //电机转动
if(num%2==1) //是否快到站
{
TR1=1;
beep=0;
while(num%2==1); //快到站就减速
{
slow();
} //减完速后等到站
delay_t1(100); //到站了延时5s
}
if(num==6)
stop=1;
}
while(1); //停机
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -