基于单片机的门控系统电路原理图如下:
制作出来的实物图如下:
元件清单:
单片机*1
11.0592晶振*1
瓷片电容22*2
电阻10k*1
电解电容10μF*2
四脚按键*3
DC电源座(3.5mm)*1、
LED红灯(3mm)*1
电阻1k*3
DIP16底座*1
ULN2003AN芯片*1
步进电机*1
红外人体传感器*1
LED红灯(5mm)*1
LED绿灯(5mm)*1
排针*9
杜邦线*7
洞洞板*1
焊接跳线(不计)
焊锡丝(不计)
单片机源程序如下:
- #include
- #define uint unsigned int
- #define uchar unsigned char
- unsigned char code RUN1[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};//正转
- unsigned char code RUN2[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};//反转
- sbit led1=P0^1; //绿色的
- sbit led2=P0^0; //红色的
- sbit hongwai=P1^0;
- sbit kaibtn=P1^1;
- sbit guanbtn=P1^2;
- uint ji=0;
- uint miao=0;
- uint ji2=0;
- uint miao2=0;
- bit flag=1;
- bit flag2=0;
- /******延时函数****************/
- void delay(unsigned int t)
- {
- unsigned int k;
- while(t--)
- {
- for(k=0; k<60; k++)//用for的空循环延长程序的执行时间
- { }
- }
- }
- //步进电机驱动 正转
- void zz()
- {
- unsigned char i;
-
- for (i=0; i<8; i++) //一个周期转3.75*8=30度
- {
- P2 = RUN1[i]&0x1f; //取数据
- delay(2); //调节转速
- }
- }
- //步进电机驱动 反转
- void fz()
- {
- unsigned char i;
-
- for (i=0; i<8; i++) //一个周期转3.75*8=30度
- {
- P2 = RUN2[i]&0x1f; //取数据
- delay(2); //调节转速
- }
- }
- void dingshi1()
- {
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- EA=1;
- ET0=0;
- TR0=0;
- ET1=0;
- TR1=0;
- }
- void main()
- {
- dingshi1();
-
- while(1)
- {
- if(kaibtn==0)
- {
- fz();
- }
- if(guanbtn==0)
- {
- zz();
- }
-
- if(flag==1)
- {
- if(hongwai==1||kaibtn==0) //检测到有人的情况下 打开门
- {
-
- ET0=1;
- TR0=1;
- fz();
- led1=0;
- led2=1;
- }
- }
-
-
-
- if(flag2==1)
- {
- if(hongwai==0||guanbtn==0) //检测到无人的情况下
- {
-
- ET1=1;
- TR1=1;
- zz();
- led1=1;
- led2=0;
- }
- }
-
-
- if(miao==5) //
- {
- miao=0;
- P2=0x00;
- ET0=0;
- TR0=0;
- flag=0;
- flag2=1;
- led1=1;
- led2=1;
- }
- if(miao2==30) //停止的时间调节的地方-具体时间可以自己实际操作计时计算
- {
- miao2=0;
- ET1=0;
- TR1=0;
- flag2=0;
- flag=1;
- led1=1;
- led2=1;
- }
-
-
- }
- }
- void timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- ji++;
- if(ji==20)
- {
- ji=0;
- miao++;
- }
- }
- void timer1() interrupt 3
- {
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- ji2++;
- if(ji2==20)
- {
- ji2=0;
- miao2++;
- }
- }