基于单片机的智能路灯控制系统.doc
元件清单名称型号数量(个)单片机AT89S521RS-232标准串口设计的接口电路MAX2321串口DB91光耦MOC30521双电压比较器LM3931稳压管LM78051变压器220V转9V1二极管IN40072三极管90131可控硅BTA081LED灯发光二极管1灯泡220V 40W的白炽灯1滑动变阻器100K1光敏电阻LG44162电阻1001330110K51K1150K2电容103110421054电解电容10pF210uF11000uF/25F12200uF/25F1P1.01P1.12P1.23P1.34P1.45P1.5/MOSI6P1.6/MISO7P1.7/SCK8RST9P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.5/T115P3.6/WR16P3.7/RD17XTAL218XTAL119GND20A8/P2.021A9/P2.122A10/P2.223A11/P2.324A12/P2.425A13/P2.526A14/P2.627A15/P2.728PESN29ALE/PROG30EA/VPP31P0.7/AD732P0.6/AD633P0.5/AD534P0.4/AD435P0.3/AD336P0.2/AD237P0.1/AD138P0.0/AD039VCC40U1AT89S52D3IN4007L1LEDR11KR21KC1104C2104C5104Vin1GND2Vout3U5LM7805R310KR8100R410KVCC213T1BTA081264U2MOC3052C10103R6330R7330VCC C1+1V+2C1-3C2+4C2-5V-6T2out7R2in8R2out9T2in10T1in11R1out12R1in13T1out14GND15VCC16U4MAX232C6105C7105C9105C8105162738495J1DB9VCCVCCR510KS1SW-PBVCCC1230pFC1330pF12J29VVCCOUTPUT A1IUTPUT A+2 IUTPUT A-3GND4 IUTPUT B-5 IUTPUT B+6OUTPUT B7VCC8J3LM393R1010KR14150KR910KR11150KVCC12J4220VR13100KR15100KC31000uF/25VC42200uF/25VY111.0592MU3LAMPC1110uFQ190135VR16LRR12LR1234J5K1Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)'通信Dim commflag As BooleanDim commstr(0 To 3) As Byte'灯泡状态 true 亮 false 灭Dim lightstate As Boolean'故障指示灯状态 true 亮 false 灭Dim errorledstate As Boolean'联机指示Dim connection As BooleanDim config As String/当点击“联机”按钮时或动其他按钮时,如果计算机和硬件未连接,会出现”端口打开错误”的命令窗口;如果计算机和硬件连接了,会出现”系统已联机”的字样。Private Sub Command1_Click()On Error GoTo out If Not connection ThenMe.MSComm1mPort = Mebo1.ListIndex + 1 Me.MSComm1.PortOpen = True If Me.MSComm1.PortOpen = False Then MsgBox "端口打开错误", vbOKOnly, "错误" Else connection = Not connection Me.Label8.Caption = "系统已联机" Me.Label8.ForeColor = RGB(255, 0, 0) Memand1.Caption = "断开" commstr(0) = &HFF commstr(1) = &H1 commstr(2) = &H0 commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2) commflag = True Me.MSComm1.Output = commstr End If Else connection = Not connection Me.MSComm1.PortOpen = False Me.Label8.Caption = "系统未联机" Me.Label8.ForeColor = RGB(255, 255, 255) Memand1.Caption = "联机" lightstate = False errorledstate = False Me.lederror(0).FillColor = RGB(100, 100, 100)Me.ledstate(0).FillColor = RGB(100, 100, 100) End If GoTo out2out: MsgBox "端口打开错误", vbOKOnly, "错误"out2:End SubPrivate Sub Form_Load()Dim timestr As StringMe.ledstate(0).FillColor = RGB(100, 100, 100)Me.lederror(0).FillColor = RGB(100, 100, 100)Me.Label3.Caption = Now()Mebo1.ListIndex = 0/端口的参数设置'串口MSComm1.Settings = "9600,n,8,1"MSComm1.InputMode = 0 '采用文本接收MSComm1.InBufferCount = 0 '清空接受缓冲区MSComm1.OutBufferCount = 0 '清空传输缓冲区MSComm1.RThreshold = 1 '产生MSComm事件'恢复时间config = App.Path + "" + "config.ini"Open config For Input As #1 '读取该汉字在16点阵字库中的原始字模Line Input #1, timestrMe.DTPicker1.Value = timestrLine Input #1, timestrMe.DTPicker2.Value = timestrClose #1'清发送标志commflag = FalseMe.Text1.Text = Str(Me.UpDown1.Value)errorledstate = Falselightstate = Falseconnection = FalseEnd SubPrivate Sub Form_Unload(Cancel As Integer)'保存时间Dim timestr As Stringconfig = App.Path + "" + "config.ini"Open config For Output As #1 '读取该汉字在16点阵字库中的原始字模Print #1, Me.DTPicker1.ValuePrint #1, Me.DTPicker2.ValueClose #1End Sub/当PC机发送“开灯”请求时,单片机回送“tuon”命令,开灯成功;当PC机发送“故障检测”请求时,单片机回送“trou”命令,说明灯泡故障,VB界面中的故障灯会亮,反之单片机回送“norm”命令,说明灯泡是好的,正常的,VB界面种的故障灯不会发生变化。Private Sub MSComm1_OnComm()Dim strBuff As StringSelect Case MSComm1mEvent Case 2 strBuff = MSComm1.Input If strBuff = "ok" Then commflag = False ElseIf strBuff = "toff" Then Me.ledstate(0).FillColor = RGB(100, 100, 100) lightstate = False commflag = False ElseIf strBuff = "tuon" Then Me.ledstate(0).FillColor = RGB(255, 0, 0) lightstate = True commflag = FalseElseIf strBuff = "trou" Then Me.lederror(0).FillColor = RGB(255, 0, 0) ElseIf strBuff = "norm" Then Me.lederror(0).FillColor = RGB(100, 100, 100) End IfEnd SelectEnd SubPrivate Sub Option1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If connection Then commstr(0) = &HFF commstr(1) = &H56 commstr(2) = &H78 commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2) commflag = True Me.MSComm1.Output = commstr Else Me.Option2.Value = True MsgBox "请先联机工作" End IfEnd SubPrivate Sub Option2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If connection Then commstr(0) = &HFF commstr(1) = &H12 commstr(2) = &H34 commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2)commflag = True Me.MSComm1.Output = commstr Else Me.Option1.Value = True MsgBox "请先联机工作" End IfEnd Sub/以下程序为VB界面中的取当前时间程序Private Sub Timer1_Timer()If commflag = True And connection Then Me.MSComm1.Output = commstrEnd If'取当前时间MeIf Me.Option1.Value = True And connection Then ontimeEnd IfEnd Sub/以下程序为定时模式时灯泡的状态,在定时模式内时,在输入的时间范围内,灯泡亮。不在定时模式时,灯泡熄灭。Private Sub ontime() If CDate(Me.DTPicker1.Value) > CDate(Me.DTPicker2.Value) Then If CDate(Time() > CDate(Me.DTPicker1.Value) Or CDate(Time() < CDate(Me.DTPicker2.Value) Then control ("开灯") Else control ("关灯") End If ElseIf CDate(Time() > CDate(Me.DTPicker1.Value) And CDate(Time() < CDate(Me.DTPicker2.Value) Then control ("开灯") Else control ("关灯") End If End IfEnd Sub/以下为开关灯命令,当计算机命令为“开灯”时,灯泡亮起;当计算机命令为“关灯”时,灯泡熄灭。Private Sub control(code As String) If code = "开灯" And lightstate = False Then lightstate = True Me.ledstate(0).FillColor = RGB(255, 0, 0) commstr(0) = &HFF commstr(1) = &H1 commstr(2) = &HF commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2) commflag = True Me.MSComm1.Output = commstr ElseIf code = "关灯" And lightstate = True Then lightstate = False Me.ledstate(0).FillColor = RGB(100, 100, 100) commstr(0) = &HFF commstr(1) = &H1 commstr(2) = &H0 commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2) commflag = True Me.MSComm1.Output = commstr End IfEnd SubPrivate Sub UpDown1_Change() Me.Text1.Text = Str(Me.UpDown1.Value) commstr(0) = &HFF commstr(1) = &H2 commstr(2) = Me.UpDown1.Value commstr(3) = commstr(0) Xor commstr(1) Xor commstr(2) commflag = True Me.MSComm1.Output = commstrEnd Sub4.3 单片机程序#include <reg51.h>/通信#define INBUF_LEN 4 /数据长度 #define OUTBUF_LEN 4 /数据长度unsigned char INBUFINBUF_LEN=0; unsigned char code succ="succ"unsigned char code toff="toff" /关闭灯unsigned char code tuon="tuon"unsigned char code trou="trou" /故障unsigned char code norm="norm"unsigned char volatile checksum,count;bit lightstate=0; bit mode=0x00;bit errorstate=0; /1 故障 0 正常bit read_flag= 0 ; bit refresh=0;/通信unsigned char volatile counter=0;unsigned int volatile maincounter=0;unsigned char levelval=70;sbit CON=P10;sbit envir=P00;sbit lightcheck=P04;/以下为实现单片机和计算机之间的通信程序。void init()TMOD = 0x22; SCON = 0x50; TH1 = 0xfd; TL1 = TH1; TR1 = 1; TH0 = 0xa4; TL0 = 0xa4; TR0 = 1; ES = 1; ET0 = 1;IT0=1;EX0=1; EA = 1;CON=1;/向串口发送一个字符 void send_char_com( unsigned char ch) SBUF=ch; while (!TI); TI= 0 ; /向串口发送一个字符串,strlen为该字符串长度 void send_string_com( unsigned char *str, unsigned char strlen) unsigned char k= 0 ; do send_char_com(*(str + k); k+; while (k < strlen); /串口接收中断函数 void serial () interrupt 4 using 1 if (RI) unsigned char ch; RI = 0 ; ch=SBUF; if (ch=0xff) count=0 ; INBUFcount=ch; checksum=ch ; else count+; INBUFcount=ch; checksum= ch; if ( count=INBUF_LEN-1 && !checksum ) read_flag= 1 ; void Timer0Interrupt(void) interrupt 1 using 3if(counter<3 && counter>0)CON=!lightstate;elseCON=1; if(counter!=0)counter-;void INT0Interrupt(void) interrupt 0 using 2counter=levelval;/调光程序,灯光的亮度等级分为5级当路灯为1级时候达最亮,2级次之,5级为最暗。void main()init();while (1)if(read_flag)read_flag=0;send_string_com(succ,OUTBUF_LEN);if(INBUF1=0x12 && INBUF2=0x34)mode=1; refresh=1;/智能else if(INBUF1=0x56 && INBUF2=0x78)mode=0;if(INBUF1=0x01)lightstate=INBUF2;else if(INBUF1=0x02)switch(INBUF2)case 1:levelval=30;break;case 2:levelval=40;break;case 3:levelval=50;break;case 4:levelval=60;break;case 5:levelval=70;break;/自然光检测程序。 if(refresh) refresh=0;if(lightstate)send_string_com(tuon,4);else if(!lightstate)send_string_com(toff,4); if(mode)if(envir && !lightstate)send_string_com(tuon,4);lightstate=1;else if(lightstate && !envir)send_string_com(toff,4);lightstate=0;/故障检测程序。if(lightstate)if(lightcheck && !errorstate)send_string_com(trou,4);errorstate=1;else if(errorstate && !lightcheck)send_string_com(norm,4);errorstate=0;else if(errorstate)send_string_com(norm,4);errorstate=0;第 16 页