📄 text1.c
字号:
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
/**********************************************************************/
#include <reg52.h>
#include <intrins.h>
/**********************************************************************/
//控制前进后退
//sbit MO_A=P1^4;
//sbit MO_B=P1^5;
//控制左右转
/*sbit LR_A=P1^0;
sbit LR_B=P1^2;
sbit LR_C=P1^1;
sbit LR_D=P1^3;*/
sbit P11=P1^1;
sbit M0=P0^0; //前进
sbit M1=P0^1 ; //后退
sbit LR_A=P0^2;//控制电机管脚
sbit LR_B=P0^3;//控制电机管脚
sbit LA=P0^4; //左边光敏电阻标志
sbit LB=P0^5; //右边
sbit ZD=P0^7; //中断判断标志
/*********************************************************************/
uchar n;
uchar time; //后轮马达变量
uchar period=100;
uchar high=64;
uchar d; //停车中断使用的延时变量
/*********************************************************************/
uchar tingche; // 开门时停车标志位
uchar zhongduancishu;//防止中断干扰标志位
/*********************************************************************/
/*
uchar count;
void timer0()interrupt 1 using 2 // 12M晶振 延时50ms程序
{count++;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
}
/*********************************************************************/
void delay()
{
uchar i,j;
for(i=0;i<255;i++)
{for(j=0;j<10;j++);
}
}
delay5s()
{
uint i,j;
for(i=0;i<350;i++)
{
for(j=0;j<2500;j++)
{;}
}
}
hongwaizhongduan() interrupt 0 using 2
{
if(ZD==0){
zhongduancishu++;
for(d=8;d>0;d--)
delay();
}
if(zhongduancishu==6)
{tingche=1;}
}
timer1() interrupt 3 using 0 // 进车库
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
n++;
}
timer0() interrupt 1 using 1 // 前进
{
TH0=(65536-500)/256;
TL0=(65536-500)%256;
if (++time==high)
M1=0;
else if(time==period)
{
time=0;
M1=1;
}
}
void left()
{uchar q;
LR_B=1;
delay();
LR_A=0;
//M1=0;
for(q=8;q>0;q--)delay();
//M1=1;
//_nop_();
}
void right()
{uchar q;
LR_A=1;
delay();
LR_B=0;
for(q=8;q>0;q--)delay();
//M1=0;
//M1=1;
//_nop_();
}
void main(void)
{
TMOD=0x11; //定时器0和1都工作在方式1 16位
TH0=(65536-500)/256;
TL0=(65536-500)%256;
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
P11=0;
IT0=0;
EX0=1; //外部中断0
ET0=1; //开启定时器0
TR0=1;
M0=1;
//TMOD=0x01;
//TH0=(65536-50000)/256;
// TL0=(65536-50000)%256;
// IE=0x83;
// IT0=1;
//TR0=1;
//M1=0;
//delay();
//M1=1;
EA=1;
while(1)
{
//LR_D=1;
//LR_C=0;
LR_A=1;
LR_B=1;
if(LA==1)
{left();}
if(LB==1)
{right();
//count=0;
}
if(tingche==1)
{ ET0=0;
TR0=0;
EX0=0;
M1=1;
M0=1;
tingche=0;
delay5s();
delay5s();
delay5s();
ET1=1;
TR1=1;
ET0=1;
TR0=1;
}
if(n==103)
{
ET1=0;
TR1=0;
ET0=0;
TR0=0;
EA=0;
n=0;
M0=1;
M1=1;
LR_A=1;
LR_B=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -