我 想用单片机设计一个日关灯控制电路 根据光线和人数控制灯的个数 其中人数计数通过两个红外线判断 根据红外线高低电平的先后顺序判断是加还是减;光敏点阻输入高低电平,当计数变量在0-5并且 光敏电阻是高电平时 允许一个灯亮 低电平时 都不亮;6-10 高 俩亮 11-15 高 仨亮。。。 我设计的程序如下 但是 不能如期工作 怎么回事 请高手指点!!
#include<AT89X51.H> #include<stdio.h> sbit L1=P1^0; sbit L2=P1^1; sbit K1=P1^2; sbit K2=P1^3; sbit M1=P1^4; sbit M2=P1^5; sbit M3=P1^6; unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f} ; unsigned char Count;
unsigned char rebuild(void) //重置人数限数,通过P38位并口输入预置数给j {unsigned char j; j=P3; return(j); }
unsigned char initial(void) //光控输入信号判断电路产生的信号只有01,10状态 { if(K1==1) { if(K2==0) return(1); } if(K1==0) { if(K2==1) return(0); } }
void delay10ms(void) { unsigned char i,j,k; for(i=20;i>0;i--) {for(j=20;j>0;j--) {for(k=248;k>0;k--); } } }
void main(void) { Count=0; P0=table[Count/10]; P2=table[Count%10]; while(1) { if(L1==1) {delay10ms(); if(L2==1) Count++; if(Count<=rebuild()) {if(initial()==1) { M1=1; M2=0; M3=0; } else { M1=0; M2=0; M3=0; } } if(Count<=2*rebuild()) {if(initial()==1) { M1=1; M2=1; M3=0; } else { M1=0; M2=0; M3=0; } } if(Count>2*rebuild()) {if(initial()==1) { M1=1; M2=1; M3=1; } else { M1=0; M2=0; M3=0; } } } if(L2==1) {delay10ms(); if(L1==1) Count--; if(Count<=rebuild()) {if(initial()==1) { M1=1; M2=0; M3=0; } else { M1=0; M2=0; M3=0; } } if(Count<=2*rebuild()) {if(initial()==1) { M1=1; M2=1; M3=0; } else { M1=0; M2=0; M3=0; } } if(Count>2*rebuild()) {if(initial()==1) { M1=1; M2=1; M3=1; } else { M1=0; M2=0; M3=0; } } } P0=table[Count/10]; P2=table[Count%10]; } }
|