教室自動感應照明控制系統是基于單片機對學校教室照明設備進行控制和管理,教室亮度的調節、自動與手動狀態的切換。整個系統由紅外對管電路、光線檢測電路、獨立按鍵操作電路組成。單片機可以實現對教室內的教室檢測控制元件所采集到的信息的接收、處理,從而可以很輕松的通過自習學生人數對教室光照強度進行實時調節。
教室照明節電控制部分采用STC89C52單片機作為核心控制芯片。教室入口裝有兩個激光傳感器通過高低電平判斷人數輸出信號。樓道和洗手間采用熱釋電傳感器觸發電路使LED燈發亮。
系統實現的主要功能有:
1)紅外對管電路,也是人數檢測電路,可以對進入教室的人數進行統計,然后在光線黑暗的條件下,根據教室的人數決定照明燈的亮度。
2)光敏電阻檢測光線強度,對照明現場的亮度進行檢測,如果現場亮度符合照明要求,系統應關閉照明設備實現智能控制。
3)獨立按鍵操作電路,可以切換自動狀態和手動狀態。手動狀態就是可以手動控制教室的照明燈。
4)樓道部分采用熱釋電傳感器檢測該區域是否有人通過,控制LED燈的開關。
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
typedef unsigned int u16; //對數據類型進行聲明定義
typedef unsigned char u8;
#define ulong unsigned long
ulong 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代表實際1221mV
LDR_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 TH1
TL0=0X18; //TL1
ET0=1; //定時器0中斷打開 ET1
TR0=1; //定時器0開關打開 TR1
EA=1; //全局中斷開
EX0=1; //外部中斷0開() EX1 為外部中斷1 (P3^3)
IT0=1; //低電平觸發 IT1為中斷1
EX1=1; //外部中斷0開() EX1 為外部中斷1 (P3^3)
IT1=1; //低電平觸發 IT1為中斷1
init();
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 TH1
TL0=0X18; //TL1
i++;
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_value1500)
{
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;
}
}
審核編輯:湯梓紅-
傳感器
+關注
關注
2550文章
51035瀏覽量
753068 -
控制系統
+關注
關注
41文章
6604瀏覽量
110576 -
Proteus
+關注
關注
79文章
1692瀏覽量
106515
發布評論請先 登錄
相關推薦
評論