智能照明控制系统源代码.doc
Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date智能照明控制系统源代码智能照明控制系统源代码附录4软件设计源程序 题 目 教学楼智能照明控制系统软件设计学生姓名 李鑫 专业班级 电子信息工程04级2 学 号 204401030222 院 (系) 电气信息工程学院 指导教师 曹祥红 完成时间 2008年 6月 10 日 -1主机程序#include <reg52.h>#include <math.h>#include <VIIC_C51.H>#include <zlg7290.H>#include <LCD12864.H>#define uchar unsigned char#define uint unsigned intvoid msec(uint x);uchar rec_es(void);void sent_es(uchar schar);sbit KEY_INT = P33; /键盘中断sbit WARN = P34; /报警控制uchar data key_buf; /定义键值寄存器uchar idata room_disbuf16="教室号:000 "uchar idata JSRS_disbuf16="当前使用:000 "uchar idata GXDJ_disbuf16="光线等级:0 "uchar idata time_disbuf16="00:00:00 "uchar idata KDSJ_disbuf16="00:00 "uchar idata GDSJ_disbuf16="00:00 "uchar data ZMKG;uchar data room_buf3;uchar data cou1,cou2;long int data com1,com2,com3,com4;bit bdata flag1,flag2;uchar code ZMK_disbuf16="照明: 开 "uchar code ZMG_disbuf16="照明: 关 "main() msec(1000); /开机延时 /EN=0;/上电液晶屏禁能 EA=1;/开所有中断 ET0=1;/开定时器0 TMOD=0x21;/设置T1为方式2 TH1=0Xfa;/设置波特率为9600b/s TL1=0Xfa; TH0=0x3c; TL0=0xb0; SCON=0xd0;/设置串口位方式3 PCON=0X80;/波特率加倍 RI=0; TI=0; cou2=0; flag2=0;/=开机动态提示= LCD_Ini(); /LCD初始化 WARN=0; /系统启动提示 msec(200); WARN=1; EX1=1;/开外部中断1 TR0=1; TR1=1;/定时器1开始计数 /= /=LCD初始化及开机提示= LCD_Wrhz(0, "郑州轻工业学院 "); /系统加载完毕界 LCD_Wrhz(16," 毕业设计 "); LCD_Wrhz(8, "教学楼智能照明 "); LCD_Wrhz(24," 控制系统 "); KEY_INT=1; while(KEY_INT); key_buf=0; while(1) LCD_Wrchar(0, "1.查看教室状态 "); /显示主菜单 LCD_Wrchar(16,"2.设置 "); LCD_Wrchar(8, "3.控制 "); LCD_Wrchar(24,"4.关于本系统 "); msec(10); if(flag2) sent_es(5); msec(10); sent_es(time_disbuf0); msec(2); sent_es(time_disbuf1); msec(2); sent_es(time_disbuf3); msec(2); sent_es(time_disbuf4); flag2=0; if(key_buf=1) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(16,"输入教室号 "); LCD_Wrchar(8,room_disbuf); key_buf=0; flag1=0; while(key_buf!=15) cou1=7; while(key_buf!=11&&key_buf!=15) if(flag1&&key_buf!=11&&key_buf!=15) if(key_buf<10&&key_buf>0) room_disbufcou1=key_buf+0x30; room_bufcou1-7=key_buf; if(key_buf=13) room_bufcou1-7=0; room_disbufcou1=0x30; if(cou1=9) cou1=7; else cou1+; LCD_Wrchar(8,room_disbuf); msec(50); /key_buf=0; flag1=0; if(key_buf=11) sent_es(1);/发读教室状态指令 msec(10);/等待从机判断 sent_es(room_buf0);/发送教室号 msec(2); sent_es(room_buf1); msec(2); sent_es(room_buf2); / msec(20);/等待从机判断教室号 com1=0; while(com1<1000&&RI=0) com1+; if(RI=1) JSRS_disbuf9=rec_es()+0x30; JSRS_disbuf10=rec_es()+0x30; JSRS_disbuf11=rec_es()+0x30; GXDJ_disbuf9=rec_es()+0x30; ZMKG=rec_es(); LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,room_disbuf);/显示主菜单 LCD_Wrchar(16,JSRS_disbuf); LCD_Wrchar(8,GXDJ_disbuf); if(ZMKG=1) LCD_Wrchar(24,"照明: 开 "); else LCD_Wrchar(24,"照明: 关 "); else LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0," 通信失败 ");/显示通信失败 LCD_Wrchar(16,"该从机不存在或者"); LCD_Wrchar(8,"连接故障 "); while(key_buf!=15); /选功能菜单1 if(key_buf=2) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"1.当前时间设定 "); LCD_Wrchar(16,"2.开灯时间设定 "); LCD_Wrchar(8,"3.关灯时间设定 "); key_buf=0; while(key_buf!=15&&key_buf!=11) if(key_buf=1) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"时间设定 ");/显示通信失败 LCD_Wrchar(16,time_disbuf); cou1=0; key_buf=0; flag1=0; while(key_buf!=11&&key_buf!=15) if(flag1&&key_buf!=11&&key_buf!=15) if(key_buf<10&&key_buf>0) time_disbufcou1=key_buf+0x30; if(key_buf=13) time_disbufcou1=0x30; if(cou1=4) cou1=0; else if(cou1>=0&&cou1<1)|(cou1>=3&&cou1<4) cou1+; else if(cou1=1) cou1+=2; LCD_Wrchar(16,time_disbuf); msec(50); /key_buf=0; flag1=0; if(key_buf=11) sent_es(5); msec(10); sent_es(time_disbuf0); msec(2); sent_es(time_disbuf1); msec(2); sent_es(time_disbuf3); msec(2); sent_es(time_disbuf4); flag2=0; /功能2子功能1 if(key_buf=2) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"开灯时间设定 ");/显示通信失败 LCD_Wrchar(16,KDSJ_disbuf); cou1=0; key_buf=0; flag1=0; while(key_buf!=11&&key_buf!=15) if(flag1&&key_buf!=11&&key_buf!=15) if(key_buf<10&&key_buf>0) KDSJ_disbufcou1=key_buf+0x30; if(key_buf=13) KDSJ_disbufcou1=0x30; if(cou1=4) cou1=0; else if(cou1>=0&&cou1<1)|(cou1>=3&&cou1<4) cou1+; else if(cou1=1) cou1+=2; LCD_Wrchar(16,KDSJ_disbuf); msec(50); /key_buf=0; flag1=0; /功能2子功能2 if(key_buf=3) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"关灯时间设定 ");/显示通信失败 LCD_Wrchar(16,GDSJ_disbuf); cou1=0; key_buf=0; flag1=0; while(key_buf!=11&&key_buf!=15) if(flag1&&key_buf!=11&&key_buf!=15) if(key_buf<10&&key_buf>0) GDSJ_disbufcou1=key_buf+0x30; if(key_buf=13) GDSJ_disbufcou1=0x30; if(cou1=4) cou1=0; else if(cou1>=0&&cou1<1)|(cou1>=3&&cou1<4) cou1+; else if(cou1=1) cou1+=2; LCD_Wrchar(16,GDSJ_disbuf); msec(50); /key_buf=0; flag1=0; /功能2子功能3 /功能菜单2 if(key_buf=3) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"1.允许照明 ");/显示通信失败 LCD_Wrchar(16,"2.照明关闭 "); LCD_Wrchar(8,"3.所有分机复位 "); key_buf=0; while(key_buf!=15) if(key_buf=1) sent_es(2);/发读教室状态指令 msec(10);/等待从机判断 LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"提示: ");/操作提示 LCD_Wrchar(16,"指令已发送 "); LCD_Wrchar(8,"全局照明允许开 "); while(key_buf!=15); key_buf=0; if(key_buf=2) sent_es(3);/发读教室状态指令 msec(10);/等待从机判断 LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"提示: ");/操作提示 LCD_Wrchar(16,"指令已发送 "); LCD_Wrchar(8,"全局照明允许关 "); while(key_buf!=15); key_buf=0; if(key_buf=3) sent_es(4);/发读教室状态指令 msec(10);/等待从机判断 LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"提示: ");/操作提示 LCD_Wrchar(16,"指令已发送 "); LCD_Wrchar(8,"所有分机已复位 "); while(key_buf!=15); key_buf=0; /功能菜单3 if(key_buf=4) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0," 本系统为2008");/显示通信失败 LCD_Wrchar(16,"年郑州轻工业学院"); LCD_Wrchar(8,"毕业设计题目:"); LCD_Wrchar(24,"教学楼智能照明控"); while(key_buf!=15) if(key_buf=14) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0," 本系统为2008");/显示通信失败 LCD_Wrchar(16,"年郑州轻工业学院"); LCD_Wrchar(8,"毕业设计题目:"); LCD_Wrchar(24,"教学楼智能智能控"); key_buf=0; if(key_buf=16) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"制系统。 ");/显示通信失败 LCD_Wrchar(16,"学生:李鑫 "); LCD_Wrchar(8,""); LCD_Wrchar(24," "); key_buf=0; /功能菜单4 if(key_buf=12) LCD_WrCommand(0x01);/清屏并归零地址计数器 msec(1); LCD_Wrchar(0,"当前时间: "); key_buf=0; while(key_buf=0) LCD_Wrchar(8,time_disbuf); msec(10); if(flag2) sent_es(5); msec(10); sent_es(time_disbuf0); msec(2); sent_es(time_disbuf1); msec(2); sent_es(time_disbuf3); msec(2); sent_es(time_disbuf4); flag2=0; /显示时间子功能 /= key_buf=0; /main /=延时子程序=/说明:函数传递的数值即延时时间,单位为ms/创建日期:2008.5.10/入口参数:X/=void msec(uint x) unsigned char k; while(x-) for(k=0;k<125;k+); /=发送串行数据=void sent_es(uchar schar)SBUF=schar;while(TI=0&&key_buf!=15);TI=0;/=/=接收串行数据=uchar rec_es(void) uchar recnum; while(RI=0&&key_buf!=15); recnum=SBUF; RI=0; return recnum; /=void getkey(void)interrupt 2 /取键值服务程序 TR0=0; EX1=0; if(TR0=0) key_buf=ZLG7290_GetKey(); WARN=0; msec(10); WARN=1; EX1=1; flag1=1; TR0=1; /=void time0(void)interrupt 1TH0=0x3c; TL0=0xb0; cou2+; if(cou2=20) cou2=0; if(time_disbuf7=0x39) time_disbuf7=0x30; if(time_disbuf6=0x35) time_disbuf6=0x30; if(time_disbuf4=0x39) time_disbuf4=0x30; if(time_disbuf3=0x35) time_disbuf3=0x30; if(time_disbuf1=0x39) time_disbuf1=0x30; time_disbuf0+; else if(time_disbuf1=0x33&&time_disbuf0=0x32) time_disbuf0=0x30; time_disbuf1=0x30; else time_disbuf1+; else time_disbuf3+; else time_disbuf4+; flag2=1; com2=(time_disbuf0-0x30)*1000+(time_disbuf1-0x30)*100+(time_disbuf3-0x30)*10+time_disbuf4-0x30; com3=(KDSJ_disbuf0-0x30)*1000+(KDSJ_disbuf1-0x30)*100+(KDSJ_disbuf3-0x30)*10+KDSJ_disbuf4-0x30; com4=(GDSJ_disbuf0-0x30)*1000+(GDSJ_disbuf1-0x30)*100+(GDSJ_disbuf3-0x30)*10+GDSJ_disbuf4-0x30; if(com2=com3) sent_es(2); if(com2=com4) sent_es(3); else time_disbuf6+; else time_disbuf7+; 2从机主程序#include "reg52.h"#include "math.h"#include"VIIC_C51.h"#define uchar unsigned char#define uint unsigned intvoid led_scan(void);/函数声明段void delay_ms(uint x);void key_scan(void);void ad_convert(uchar channel);void pass_dis(uchar *dis);void sent_es(uchar schar);uchar rec_es(void);void time_dis(void);sbit keyin=P17;sbit adc_flag=P32;sbit buzzer=P34;sbit relay=P35;sbit HW1=P12;sbit HW2=P13;uchar data dis_buf6=0x14,0x14,0x14,0x14,0x14,0x14;/定义显示缓冲区uchar data time_buf6=0x19,0x17,0,0,0,0;/时间缓冲区uchar data gx_buf6=0x20,0x17,0x17,0x17,0x17,0;/时间缓冲区uchar data rs_buf6=0x1b,0x18,0x17,0,0,0;/时间缓冲区uchar data room_buf3;uchar data JSRS;/教室人数uchar data GXDJ;/光线等级uchar data wink_con;/闪烁控制位uchar data key_buf;/定义键值缓冲区uchar data wink_speed;/闪烁速度,值越小闪烁速度越快uchar data wink_spbuf;uchar data GXJC_cou;/光线检测定时uchar data key_cou;/键盘扫描定时unsigned long data CSXS_cou;/参数显示定时unsigned long data QZKD_cou;/强制开灯时间uchar data menu_num;/菜单号uchar data es_buf;/通用变量bit bdata ZMKG;/照明开关bit bdata KDYX;/开灯允许标志bit bdata off_flag,key;/开关标志位uchar dat