📄 text1.c
字号:
#include"reg51.h"
#include"INTRINS.H"
//#include"math.h"
#define uchar unsigned char
#define uint unsigned int
sbit SW=P3^7;
sbit speed1=P1^5;
sbit speed2=P1^4;
sbit insert=P1^6;
sbit conti=P1^7;
sbit led=P1^3;
sbit L1=P1^0;
sbit L2=P1^1;
sbit L3=P1^2;
sbit wdi=P3^5;
uchar time_se;
uint time_mse;
bit led_c;
void watch_dog();
void delay(uint number);
one_cycle(uchar speed);
void delay(uint number)
{
while(number--)
{ // watch_dog();wdi=0;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();wdi=1;
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
wdi=0;
}
}
void one_cy(uchar speed) //单位 km/h
{
uint speed_3;
uint speed_2;
speed_2=(2*3600)/speed;
speed_3=(3*3600)/speed;
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
EA=1;
L1=0;
time_mse=0;
time_se=0;
if(insert==1)
while(((time_se*1000)+time_mse)<(speed_2+speed_3))watch_dog();
else
{ while(((time_se*1000)+time_mse)<(speed_3))watch_dog();
L1=1;
while(((time_se*1000)+time_mse)<(speed_2+speed_3))watch_dog();
L1=0;
}
L2=0;
while(((time_se*1000)+time_mse)<(speed_2+(2*speed_3)))watch_dog();
L1=1;
if(insert==0) L2=1;
while(((time_se*1000)+time_mse)<((2*speed_2)+(2*speed_3)))watch_dog();
L3=0;
if(insert==0) L2=0;
while(((time_se*1000)+time_mse)<((2*speed_2)+(3*speed_3)))watch_dog();
L2=1;
if(insert==0) L3=1;
if(insert==1)
while(((time_se*1000)+time_mse)<((3*speed_2)+(4*speed_3)))watch_dog();
else
{while(((time_se*1000)+time_mse)<((3*speed_2)+(3*speed_3)))watch_dog();
L3=0;
while(((time_se*1000)+time_mse)<((3*speed_2)+(4*speed_3)))watch_dog();
}
L3=1;
}
void watch_dog()
{
wdi=0;
wdi=1;
delay(5);
wdi=0;
}
main()
{
uchar speed=0;
uchar m_c=0;
L1=1;
L2=1;
L3=1;
led_c=0;
time_se=0;
time_mse=0;
led=0;
delay(2000);
led=1;
TMOD=0X01;
TH0=0XFc;
TL0=0X18;
ET0=1;
ET1=0;
TR0=1;
EA=1;
while(1)
{
watch_dog();
if(speed1==0) speed|=0x01;
if(speed2==0) speed|=0x02;
if((conti==1)&&(SW==0)) //不连续发
{switch(speed)
{case 0:one_cy(30); //30km/h
break;
case 1:one_cy(60); //60km/h
break;
case 2:one_cy(120); //120km/h
break;
case 3:one_cy(150); //150km/h
break;
default:break;
}
}
if(conti==0)
{
if(SW==0)
{ delay(1000);
while(1)
{
for(m_c=0;m_c<3;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_mse<600)watch_dog();
//TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<10;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_se<2)watch_dog();
// TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<7;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(((time_se*1000)+time_mse)<1500)watch_dog();
// TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<10;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(((time_se*1000)+time_mse)<2500)watch_dog();
//TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<20;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_se<3)watch_dog();
//TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<5;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_se<5)watch_dog();
//TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<15;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(((time_se*1000)+time_mse)<3500)watch_dog();
// TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<5;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_se<6)watch_dog();
// TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<5;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_se<4)watch_dog();
// TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<5;m_c++)
{
one_cy(100);
TR0=0;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(((time_se*1000)+time_mse)<5500)watch_dog();
//TR0=0;
}
if(conti==1) break;
for(m_c=0;m_c<5;m_c++)
{
one_cy(100);
TR0=1;
time_mse=0;
time_se=0;
while(time_se<1)watch_dog();
// TR0=0;
}
if(conti==1) break;
TH0=0XFc;
TL0=0X18;
TR0=1;
time_mse=0;
time_se=0;
while(time_se<5)watch_dog();
led_c=1;led=0; if(conti==1) break;
while(time_se<10)watch_dog();
// TR0=0;
led=1; led_c=0;
}
}
}
}
}
void time0_inter() interrupt 1 using 0
{
TH0=0XFc;
TL0=0X18; // 1ms
time_mse++;
if((led_c==0)&&(time_mse==500)) led=0;
if(time_mse==1000) {led=1;time_se++;time_mse=0;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -