我 想用單片機設(shè)計一個日關(guān)燈控制電路 根據(jù)光線和人數(shù)控制燈的個數(shù) 其中人數(shù)計數(shù)通過兩個紅外線判斷 根據(jù)紅外線高低電平的先后順序判斷是加還是減;光敏點阻輸入高低電平,當(dāng)計數(shù)變量在0-5并且 光敏電阻是高電平時 允許一個燈亮 低電平時 都不亮;6-10 高 倆亮 11-15 高 仨亮。。。 我設(shè)計的程序如下 但是 不能如期工作 怎么回事 請高手指點!!
#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) //重置人數(shù)限數(shù),通過P38位并口輸入預(yù)置數(shù)給j {unsigned char j; j=P3; return(j); }
unsigned char initial(void) //光控輸入信號判斷電路產(chǎn)生的信號只有01,10狀態(tài) { 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]; } }
|