proteus:教室自动感应照明控制系统 要闻速递
教室自动感应照明控制系统是基于单片机对学校教室照明设备进行控制和管
2023-06-14 12:19:19
【资料图】
教室自动感应照明控制系统是基于单片机对学校教室照明设备进行控制和管理,教室亮度的调节、自动与手动状态的切换。整个系统由红外对管电路、光线检测电路、独立按键操作电路组成。单片机可以实现对教室内的教室检测控制元件所采集到的信息的接收、处理,从而可以很轻松的通过自习学生人数对教室光照强度进行实时调节。教室照明节电控制部分采用STC89C52单片机作为核心控制芯片。教室入口装有两个激光传感器通过高低电平判断人数输出信号。楼道和洗手间采用热释电传感器触发电路使LED灯发亮。系统实现的主要功能有:1)红外对管电路,也是人数检测电路,可以对进入教室的人数进行统计,然后在光线黑暗的条件下,根据教室的人数决定照明灯的亮度。2)光敏电阻检测光线强度,对照明现场的亮度进行检测,如果现场亮度符合照明要求,系统应关闭照明设备实现智能控制。3)独立按键操作电路,可以切换自动状态和手动状态。手动状态就是可以手动控制教室的照明灯。4)楼道部分采用热释电传感器检测该区域是否有人通过,控制LED灯的开关。
#include "reg52.h"#define uchar unsigned char#define uint unsigned inttypedef unsigned int u16; //对数据类型进行声明定义typedef unsigned char u8;#define ulong unsigned longulong volt;//测量的电压值sbit wr=P2^0;//数据写sbit rd=P2^1;//数据读sbit ce=P2^2;//片选sbit cd=P2^3;//指令数据通道,1指令,0数据sbit rst=P2^4;//复位信号sbit CLK=P1^3;//定义时钟信号口sbit DIN=P1^1;//定义2543数据写入口sbit DOUT=P1^0;//定义2543数据读取口sbit CS=P1^2;//定义2543片选信号口sbit LED1 = P1^4;//教室灯光sbit LED2 = P1^5;//走廊灯光sbit KEY1 = P3^0;sbit KEY2 = P3^1;sbit KEY3 = P3^4;sbit KEY4 = P3^5;sbit KEY5 = P3^6;uchar Peo_Number;uint LDR_value;uchar Mode = 0; //当前控制模式 0 为 自动 1 为手动uchar flag; //当前黑暗程度 0 为 明亮 1 为黑暗uchar PWM_SET = 0;uchar PWM_Value;uchar LED_flag;//走廊灯uchar code HZ0[][32]={{0x08,0x20,0x08,0x20,0x7E,0xA0,0x09,0x3E,0x0A,0x44,0xFF,0x44,0x08,0x44,0x7E,0xA4,0x24,0x28,0x48,0x28,0x8F,0x10,0x78,0x10,0x08,0x28,0x08,0x48,0x28,0x84,0x11,0x02},/*"教",0*/{0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x3F,0xF8,0x04,0x00,0x08,0x20,0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00},/*"室",1*/{0x00,0x00,0x7D,0xFC,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x94,0x7D,0x08,0x44,0xFC,0x44,0x84,0x44,0x84,0x44,0x84,0x7C,0xFC,0x00,0x00,0x48,0x88,0x44,0x44,0x84,0x44},/*"照",2*/{0x00,0x00,0x00,0xFC,0x7C,0x84,0x44,0x84,0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,0x44,0x84,0x44,0xFC,0x7C,0x84,0x44,0x84,0x01,0x04,0x01,0x04,0x02,0x14,0x04,0x08},/*"明",3*/{0x00,0xF8,0x3F,0x00,0x04,0x00,0x08,0x20,0x10,0x40,0x3F,0x80,0x01,0x00,0x06,0x10,0x18,0x08,0x7F,0xFC,0x01,0x04,0x09,0x20,0x11,0x10,0x21,0x08,0x45,0x04,0x02,0x00},/*"系",4*/{0x10,0x40,0x10,0x20,0x20,0x20,0x23,0xFE,0x48,0x40,0xF8,0x88,0x11,0x04,0x23,0xFE,0x40,0x92,0xF8,0x90,0x40,0x90,0x00,0x90,0x19,0x12,0xE1,0x12,0x42,0x0E,0x04,0x00},/*"统",5*/{0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x08,0x20,0x10,0x10,0x20,0x08,0x40,0x04,0x80,0x02},/*"人",6*/{0x08,0x20,0x49,0x20,0x2A,0x20,0x08,0x3E,0xFF,0x44,0x2A,0x44,0x49,0x44,0x88,0xA4,0x10,0x28,0xFE,0x28,0x22,0x10,0x42,0x10,0x64,0x28,0x18,0x28,0x34,0x44,0xC2,0x82},/*"数",7*/{0x01,0x00,0x02,0x00,0x04,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10},/*"自",8*/{0x00,0x40,0x00,0x40,0x7C,0x40,0x00,0x40,0x01,0xFC,0x00,0x44,0xFE,0x44,0x20,0x44,0x20,0x44,0x20,0x84,0x48,0x84,0x44,0x84,0xFD,0x04,0x45,0x04,0x02,0x28,0x04,0x10},/*"动",9*/{0x00,0x10,0x00,0xF8,0x3F,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00},/*"手",10*/{0x00,0x40,0x00,0x40,0x7C,0x40,0x00,0x40,0x01,0xFC,0x00,0x44,0xFE,0x44,0x20,0x44,0x20,0x44,0x20,0x84,0x48,0x84,0x44,0x84,0xFD,0x04,0x45,0x04,0x02,0x28,0x04,0x10},/*"动",11*/{0x11,0x10,0x11,0x10,0x17,0xFC,0x11,0x10,0xFC,0x00,0x13,0xF8,0x32,0x08,0x3B,0xF8,0x56,0x08,0x53,0xF8,0x90,0x40,0x17,0xFC,0x10,0xA0,0x11,0x10,0x12,0x08,0x14,0x06},/*"模",12*/{0x00,0x48,0x00,0x44,0x00,0x44,0x00,0x40,0xFF,0xFE,0x00,0x40,0x00,0x40,0x3E,0x40,0x08,0x40,0x08,0x40,0x08,0x20,0x08,0x22,0x0F,0x12,0x78,0x0A,0x20,0x06,0x00,0x02},/*"式",13*/{0x01,0x00,0x21,0x08,0x11,0x08,0x09,0x10,0x09,0x20,0x01,0x00,0xFF,0xFE,0x04,0x40,0x04,0x40,0x04,0x40,0x04,0x40,0x08,0x42,0x08,0x42,0x10,0x42,0x20,0x3E,0xC0,0x00},/*"光",14*/{0x40,0x20,0x40,0x20,0x7F,0x20,0x80,0x3E,0x7E,0x44,0x42,0x44,0x52,0x44,0x4A,0xA4,0xFF,0xA8,0x42,0x28,0x92,0x10,0x8A,0x10,0xFF,0x28,0x02,0x48,0x14,0x84,0x09,0x02},/*"敏",15*/{0x00,0x00,0x7D,0xF8,0x45,0x08,0x49,0x08,0x49,0x08,0x51,0xF8,0x49,0x08,0x49,0x08,0x45,0x08,0x45,0xF8,0x45,0x08,0x69,0x08,0x51,0x08,0x41,0x08,0x47,0xFE,0x40,0x00},/*"阻",16*/{0x08,0x40,0x08,0x40,0x0F,0xFC,0x10,0x40,0x10,0x40,0x33,0xF8,0x32,0x08,0x53,0xF8,0x92,0x08,0x13,0xF8,0x12,0x08,0x13,0xF8,0x12,0x08,0x12,0x08,0x1F,0xFE,0x10,0x00},/*"值",17*/};uchar code SZ[][16]={{0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00},/*"0",0*/{0x00,0x00,0x00,0x08,0x38,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x3E,0x00,0x00},/*"1",1*/{0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x02,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00},/*"2",2*/{0x00,0x00,0x00,0x3C,0x42,0x42,0x02,0x04,0x18,0x04,0x02,0x42,0x42,0x3C,0x00,0x00},/*"3",3*/{0x00,0x00,0x00,0x04,0x0C,0x0C,0x14,0x24,0x24,0x44,0x7F,0x04,0x04,0x1F,0x00,0x00},/*"4",4*/{0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x78,0x44,0x02,0x02,0x42,0x44,0x38,0x00,0x00},/*"5",5*/{0x00,0x00,0x00,0x18,0x24,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x22,0x1C,0x00,0x00},/*"6",6*/{0x00,0x00,0x00,0x7E,0x42,0x04,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x00,0x00},/*"7",7*/{0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00},/*"8",8*/{0x00,0x00,0x00,0x38,0x44,0x42,0x42,0x42,0x46,0x3A,0x02,0x02,0x24,0x18,0x00,0x00},/*"9",9*/{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",10*/{0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00},/*":",11*/};void delay(uint j) //1ms{ uchar i=250; for(;j>0;j--){ while(--i); i=249; while(--i); i=250; }}void Delay_xms(uint x){ uint i,j; for(i=0;i>=1;volt=ad;//取走转换结果volt=volt*1221;//例子的满量程为5V,转换分辩率为12位(2的12次方=4096) 。即最大值是255,5/4096=1221mV,即例子中的1V代表实际1221mVLDR_value = volt / 1000;}void KEY_Scan(){if(KEY1 == 0){delay(20);if(KEY1 == 0){while(KEY1==0);display_HZ(1,0,HZ0[10]);display_HZ(1,1,HZ0[11]);display_HZ(1,2,HZ0[12]);display_HZ(1,3,HZ0[13]);Mode = 1;}}if(KEY2 == 0){delay(20);if(KEY2 == 0){while(KEY2==0);display_HZ(1,0,HZ0[8]);display_HZ(1,1,HZ0[9]);display_HZ(1,2,HZ0[12]);display_HZ(1,3,HZ0[13]);Mode = 0;}}if(KEY3 == 0){delay(20);if(KEY3 == 0){while(KEY3==0);if(Mode==1){LED1 = 0;}}}if(KEY4 == 0){delay(20);if(KEY4 == 0){while(KEY4==0);if(Mode==1){LED1 = 1;}}}if(KEY5 == 0){delay(20);if(KEY5 == 0){while(KEY5==0);LED2 = 0;delay(1000);LED2 = 1;}}}void main(){TMOD |= 0x01; //开启定时器0 0x10时使用定时器1 0x11时启动两个TH0=0XFC;//给定时器赋初值,定时1ms TH1TL0=0X18; //TL1ET0=1; //定时器0中断打开 ET1TR0=1; //定时器0开关打开 TR1EA=1; //全局中断开EX0=1; //外部中断0开() EX1 为外部中断1 (P3^3)IT0=1; //低电平触发 IT1为中断1EX1=1; //外部中断0开() EX1 为外部中断1 (P3^3)IT1=1; //低电平触发 IT1为中断1init();Delay_xms(10);display_HZ(0,1,HZ0[0]);display_HZ(0,2,HZ0[1]);display_HZ(0,3,HZ0[2]);display_HZ(0,4,HZ0[3]);display_HZ(0,5,HZ0[4]);display_HZ(0,6,HZ0[5]);display_HZ(1,0,HZ0[8]);display_HZ(1,1,HZ0[9]);display_HZ(1,2,HZ0[12]);display_HZ(1,3,HZ0[13]);display_HZ(2,0,HZ0[6]);display_HZ(2,1,HZ0[7]);display_SZ(2,4,SZ[11]);display_SZ(2,5,SZ[Peo_Number/100]);display_SZ(2,6,SZ[Peo_Number/10%10]);display_SZ(2,7,SZ[Peo_Number%10]);read2543(0);display_HZ(3,0,HZ0[14]);display_HZ(3,1,HZ0[15]);display_HZ(3,2,HZ0[16]);display_HZ(3,3,HZ0[17]);display_SZ(3,8,SZ[11]);display_SZ(3,9,SZ[LDR_value/1000]);display_SZ(3,10,SZ[LDR_value/100%10]);display_SZ(3,11,SZ[LDR_value/10%10]);display_SZ(3,12,SZ[LDR_value%10]);while(1){KEY_Scan();}}void K_INT1( ) interrupt 0 // 这里0对应下表{ Peo_Number++; }void K_INT2( ) interrupt 2 // 这里0对应下表{ if(Peo_Number!=0) {Peo_Number--; } }void Timer0() interrupt 1 //参考上图 定时器1时为3{static uint i; //定义一个自变形变量TH0=0XFC;//给定时器赋初值,定时1ms TH1TL0=0X18; //TL1i++;PWM_Value++;if(i==200){i=0;read2543(0);display_SZ(3,9,SZ[LDR_value/1000]);display_SZ(3,10,SZ[LDR_value/100%10]);display_SZ(3,11,SZ[LDR_value/10%10]);display_SZ(3,12,SZ[LDR_value%10]);display_SZ(2,5,SZ[Peo_Number/100]);display_SZ(2,6,SZ[Peo_Number/10%10]);display_SZ(2,7,SZ[Peo_Number%10]);if(Peo_Number == 1){PWM_SET = 1;}else if(Peo_Number > 20){PWM_SET = 5;}else if(Peo_Number < 20 && Peo_Number>1){PWM_SET = Peo_Number/5 + 1;//234}else if(Peo_Number==0){PWM_SET = 0;}if(LDR_value<1500){flag = 0;}else{flag = 1;}}if(PWM_Value==5){PWM_Value = 0;}if(flag == 1 && Mode == 0)//光线不充足 并且在自动模式下{if(PWM_Value >= PWM_SET)//1{LED1 = 1;}else{LED1 = 0;}}else if(flag == 0 && Mode == 0){LED1 = 1;}}
审核编辑:汤梓红
标签: