基于单片机的智能路灯控制系统...docx
元件清单名称型号数量(个)单片机AT89S521RS-232标准串口设计的接口电路MAX2321串口DB91光耦M0C30521双电压比较器LM3931稳压管LM78051变压器220V转9V1二极管IN40072三极管90131可控硅BTA081LED灯发光二极管1灯泡220V40W的白炽灯1滑动变阻器100K1光敏电阻LG44162电阻10013301IOK5IK1150K2电容103110421054电解电容IOpF2IOuF11000uF25F12200uF25F1V CPrivate Declare Sub Sleep Lib "Kernel32" (ByVal dwMiIlisecondsAsLong)'通信DimcommflagsBooleanDimcommstr(OTo3)AsByte'灯泡状态true亮false灭DimlightstateAsBoolean,故障指示灯状态true亮false灭DimerrorledstatesBoolean,联机指示DimconnectionAsBooleanDimconfigAsString当点击“联机”按钮时或动其他按钮时,假如计算机和硬件未连接,会出现“端口打开错误”的吩咐窗口;假如计算机和硬件连接了,会出现”系统已联机”的字样。PrivateSubCommandlClick()OnErrorGoTooutIfNotconnectionThenMe.MSComml.CommPort=Me.Combol.Listindex+1Me.MSComml.PortOpen=TrueIfMe.MSComml.PortOpen=FalseThenMsgBox端口打开错误,VboKOnly,错误Elseconnection=NotconnectionMe.Label8.Caption=系统已联机Me.Label8.ForeColor=RGB(255,O,O)Me.Commandl.Caption=断开Commstr(O)=&HFFcommstr(l)=&H1commstr(2)=&H0commstr(3)=commstr(O)Xorcommstr(1)Xorcommstr(2)Commflag=TrueMe.MSComml.Output=commstrEndIfElseconnection=Notconnectionlightstate=FalseMe. MSComml. PortOpenMe.Label8. Caption =Me. Label8. ForeColorMe. Commandl. Caption=False系统未联机=RGB(255, 255, 255)=联机errorledstate=FalseMe.Iederror(O).FillColor=RGB(100,100,100)Me.Iedstate(O).FillColor=RGB(100,100,100)EndIfGoToout2out:MsgBox端口打开错误",VbOKonly,错误out2:EndSubPrivateSubForm_Load()DimtimestrAsStringMe.Iedstate(O).FillColor=RGB(100,100,100)Me.Iederror(O).FillColor=RGB(100,100,100)Me.Label3.Caption-Now()Me.Combol.Listindex=O端口的参数设置'串口MSComml.Settings=zz9600,n,8,1MSComml.InputMode=O'采纳文本接收MSComml.InBufferCount=O'清空接受缓冲区MSComml.OutBufferCount=O'清空传输缓冲区MSComml.RThreshold=1'产生MSComm事务复原时间config=App.Path+,zconfig.iniOpenconfigForInputAs#1'读取该汉字在16点阵字库中的原始字模1.ineInput#1,timestrMe.DTPickerl.Value=timestr1.ineInput#1,timestrClose#1'清发送标记commflag=FalseMe.Textl.Text=Str(Me.UpDownl.Value)errorledstate=Falselightstate=Falseconnection=FalseEndSubPrivateSubFormJnload(CancelsInteger)'保存时间DimtimestrAsStringconfig=App.Path+“config.iniOpenconfigForOutputAs#1'读取该汉字在16点阵字库中的原始字模Close#1EndSub当PC机发送“开灯”恳求时,单片机回送“tuon”吩咐,开灯胜利;当PC机发送“故障检测”恳求时,单片机回送“trou”吩咐,说明灯泡故障,VB界面中的故障灯会亮,反之单片机回送“norm”吩咐,说明灯泡是好的,正常的,VB界面种的故障灯不会发生改变。PrivateSubMSComml_OnComm()DimStrBuffAsStringSelectCaseMSComml.CommEventCase2StrBuff=MSComml.InputIfStrBuff=ok"Thencommflag=FalseElseIfStrBuff=ThenMe.Iedstate(O).FillColor=RGB(100,100,100)lightstate=FalseCommflag=FalseElseIfStrBuff=tuonThenMe.Iedstate(O),FillColor=RGB(255,O,O)lightstate=Truecommflag=FalseElseIfStrBuff=trouThenMe.Iederror(O).FillColor=RGB(255,O,O)ElseIfStrBuff=normThenMe.Iederror(O).FillColor=RGB(100,100,100)EndIfEndSelectEndSubPrivateSubOptionl_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,YAsSingle)IfconnectionThencommstr(O)=&HFFcommstr(l)=&H56commstr(2)=&II78commstr(3)=commstr(O)Xorcommstr(1)Xorcommstr(2)commflag=TrueMe.MSComml.Output=commstrElseMe.0ption2.Value=TrueMsgBox请先联机工作EndIfEndSubShift Ascommstr(2)PrivateSubOption2_MouseDown(ButtonAsInteger,Integer,XAsSingle,YAsSingle)IfconnectionThencommstr(O)=&HFFCommstr(I)=&H12commstr(2)=&II34commstr(3)=commstr(O)Xorcommstr(1)Xorcommflag=TrueMe.MSComml.Output=commstrElseMe.Optionl.Value=TrueMSgBOX请先联机工作EndIfEndSub以下程序为VB界面中的取当前时间程序PrivateSubTimerl_Timer()Ifcommflag=TrueAndconnectionThenMe.MSComml.Output=commstrEndIf'取当前时间MeIfMe.Optionl.Value=TrueAndconnectionThenontimeEndIfEndSub以下程序为定时模式时灯泡的状态,在定时模式内时,在输入的时间范围内,灯泡亮。不在定时模式时,灯泡熄灭。PrivateSubontime()IfCDate(Me.DTPickerl.Value)>CDate(Me.DTPicker2.Value)ThenIfCDate(Time()>CDate(Me.DTPickerl.Value)OrCDate(Time()<CDate(Me.DTPicker2.Value)Thencontrol("开灯)Elsecontrol(关灯)EndIfElseIfCDate(Time()>CDate(Me.DTPickerl.Value)AndCDate(TimeO)<CDate(Me.DTPicker2.Value)Thencontrol("开灯)Elsecontrol("关灯)EndIfEndIfEndSub以下为开关灯吩咐,当计算机吩咐为“开灯”时,灯泡亮起;当计算机吩咐为“关灯”时,灯泡熄灭。PrivateSubcontrol(codeAsString)Ifcode=开灯Andlightstate=FalseThenlightstate=TrueMe.Iedstate(O).FillColor=RGB(255,0,O)Commstr(O)=&HFFCommstr(I)=&H1commstr(2)=&IIFcommstr(3)=commstr(O)Xorcommstr(1)Xorcommstr(2)commflag=TrueMe.MSComml.Output=commstrElseIfcode=关灯”Andlightstate=TrueThenlightstate=FalseMe.Iedstate(O).FillColor=RGB(100,100,100)Commstr(O)=&HFFCommstr(I)=&H1commstr(2)=&H0commstr(3)=commstr(O)Xorcommstr(1)Xorcommstr(2)Commflag=TrueMe.MSComml.Output=commstrEndIfEndSubPrivateSubUpDownl_Change()Me.Textl.Text=Str(Me.UpDownl.Value)Commstr(O)=&HFFCommstr(I)=&H2commstr(3)=commstr(O)Xorcommstr(1)Xorcommstr(2)commflag=TrueMe.MSComml.Output=commstrEndSub4.3单片机程序#include<reg51.h>/通信#defineINBUF_LEN4数据长度#defineOUTBUFJLEN4数据长度unsignedcharINBUFINBUF-LEN=O;unsigned char codetuon=tuon关闭灯故障unsignedcharcodeSUCC=succunsignedcharcodetoff=,ztoffzzunsignedcharcodetrou=trouunsignedcharcodenorm=normunsignedcharvolatilechecksum,count;bitIightstate=O;bitmode=0x00;biterrorstate=0;/1故障0正常bitread-flag=0;bitrefresh=0;/通信unsignedcharvolatilecounter=0;unsignedintvolatilemaincounter=0;unsignedcharlevelval=70;sbitCON=PO;sbitenvir=P00;sbitIightCheCk=POc4;以下为实现单片机和计算机之间的通信程序。voidinit()TMOD=0x22;SCON=0x50;THl=Oxfd;TLl=Till;TRl=1;THO=0xa4;TLO=0xa4;TRO=1;ES=1;ETO=1;ITO=I;EXO=I;EA=1;CON=I;向串口发送一个字符voidsend_char_com(unsignedcharch)SBUF=ch;while(!TI);TI=0;向串口发送一个字符串,StrIen为该字符串长度voidsend_string_com(unsignedchar*str,unsignedcharstrlen)unsignedchark=0;dosend_char_com(*(str+k);k+;while(k<strlen);串口接收中断函数voidserial()interrupt4using1if(RI)unsignedcharch;RI=0;Ch=SBUF;if(ch=0xff)count=0;INBUFcount=ch;checksum=ch;elsecount+;INBUFcount=ch;checksum=ch;if(COiint=INBUF-LENT&&!checksum)read_flag=1;voidTimerOInterrupt(void)interrupt1using3if(counter<3&&counter>0)CON=Ilightstate;elseCON=I;if(counter!=0)counter;voidINTOInterrupt(void)interrupt0using2Counter=Ievelval;调光程序,灯光的亮度等级分为5级当路灯为1级时候达最亮,2级次之,5级为最暗。voidmain()init();while(1)if(read_flag)read_fIag=O;send_string_com(succ,OUTBUF_LEN);if(INBUFl=0xl2&&INBUF2=0x34)mode=l;refresh=1;"/智能elseif(INBUFl=0x56&&INBUF2=0x78)mode=0;if(INBUFl=0x01)lightstate=INBUF2;elseif(INBUFl=0x02)switch(INBUF2)case1:levelval=30;break;case2:levelval=40;break;case3:levelval=50;break;case4:levelval=60;break;case5:levelval=70;break;自然光检测程序。if(refresh)refresh二。;if(Iightstate)send_string_com(tuon,4);elseif(!lightstate)send_string_com(toff,4);if(mode)if(envir&&!lightstate)send_string_com(tuon,4);Iightstate=I;elseif(lightstate&&!envir)send_string_com(toff,4);Iightstate=O;故障检测程序。if(Iightstate)if(lightcheck&&!errorstate)send_string_com(trou,4);errorstate=l;elseif(errorstate&&!lightcheck)send_string_com(norm,4);errorstate=0;elseif(errorstate)send_string_com(norm,4);errorstate=0;