基于单片机的智能路灯控制系统.doc

上传人:美****子 文档编号:58092428 上传时间:2022-11-06 格式:DOC 页数:16 大小:180KB
返回 下载 相关 举报
基于单片机的智能路灯控制系统.doc_第1页
第1页 / 共16页
基于单片机的智能路灯控制系统.doc_第2页
第2页 / 共16页
点击查看更多>>
资源描述

《基于单片机的智能路灯控制系统.doc》由会员分享,可在线阅读,更多相关《基于单片机的智能路灯控制系统.doc(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、元件清单名称型号数量(个)单片机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/SCK8RST9

2、P3.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/AD039VCC40U1AT89S52D3IN400

3、7L1LEDR11KR21KC1104C2104C5104Vin1GND2Vout3U5LM7805R310KR8100R410KVCC213T1BTA081264U2MOC3052C10103R6330R7330VCC C1+1V+2C1-3C2+4C2-5V-6T2out7R2in8R2out9T2in10T1in11R1out12R1in13T1out14GND15VCC16U4MAX232C6105C7105C9105C8105162738495J1DB9VCCVCCR510KS1SW-PBVCCC1230pFC1330pF12J29VVCCOUTPUT A1IUTPUT A+2 IU

4、TPUT 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灯泡

5、状态 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 =

6、 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) = commst

7、r(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

8、 = 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.ListInde

9、x = 0/端口的参数设置串口MSComm1.Settings = 9600,n,8,1MSComm1.InputMode = 0 采用文本接收MSComm1.InBufferCount = 0 清空接受缓冲区MSComm1.OutBufferCount = 0 清空传输缓冲区MSComm1.RThreshold = 1 产生MSComm事件恢复时间config = App.Path + + config.iniOpen config For Input As #1 读取该汉字在16点阵字库中的原始字模Line Input #1, timestrMe.DTPicker1.Value = tim

10、estrLine 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.iniOpen config For O

11、utput 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 MSComm1mEve

12、nt 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 =

13、 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 com

14、mstr(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 c

15、ommstr(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 = commstrE

16、nd 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.DTPicker1.Value) And C

17、Date(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 comms

18、tr(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) =

19、 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

20、 Me.MSComm1.Output = commstrEnd Sub4.3 单片机程序#include /通信#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;unsign

21、ed 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 ini

22、t()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 str

23、len) 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 ) rea

24、d_flag= 1 ; void Timer0Interrupt(void) interrupt 1 using 3if(counter0)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

25、_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;/自然光检测程序。 i

26、f(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 页

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 文案大全

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁