功能:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示当前时间/环境光强/工作模式
2.支持路灯故障检测
3.工作时间内(17~24时),两个路灯同时点亮,24时以后,B路灯关闭,若检测到由物体通过,路灯B点亮10s后关闭
4.非工作时间,光照强度低于所设定的光照阈值,两个路灯点亮
5.可通过按键更改当前时间/路灯工作时间/光照阈值/工作模式/路灯光照强度
按键功能描述:
a)非设置状态下按键功能
切换/确定键:切换全自动/定时模式/手动模式
上/下键:更换界面
左键:关闭所有路灯
右键:打开所有路灯
设置键:进入对应设置界面
b)设置界面
切换/确定键:保存设置并退出设置状态
上/下键:加/减
左/右键:切换设置位置
6.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
#define __MAIN_C
#include <reg52.h>
#include "main.h"
enum _MODE_DF_ dispMode; //显示模式
bit dispFlag = 1; //显示标志
bit setFlag = 0; //设定标志
bit sendFlag = 0; //发送标志
unsigned char setIndex = 0; //设置坐标
unsigned char lightValue; //环境光强
unsigned char lightValueA; //灯光A光强
unsigned char lightValueB; //灯光B光强
unsigned char lightLimit = 40; //光强阈值
unsigned char timeLimit[4] = {17, 0, 5, 0}; //工作时间
unsigned char modeFlag = 0; //模式标志
bit lampBDelay = 0; //灯光B延时
bit lampAError = 0; //灯光A故障标志
bit lampBError = 0; //灯光B故障标志
bit lampASwitch; //灯光A开关
bit lampBSwitch; //灯光B开关
void main()
{
//初始化
lampASwitch = LAMP_OFF;
lampBSwitch = LAMP_OFF;
Timer0_Init();
LCD_Init();
DS1302_Init();
DelayMs(20);
//开机显示
LCD_DispStr(0, 0, " Welcome! ");
DelayS(2);
LCD_Clear();
DS1302_ReadTime();
DispNormal(setIndex);
while(1)
{
// LCD1602液晶显示
if (dispFlag == 1 && setFlag == 0)
{
dispFlag = 0;
LampCtr(); //灯光自动控制
CheckLamp(); //灯光检测
DS1302_ReadTime(); //读取时间
if (dispMode == DISP_NORMAL) //正常显示模式
{
DispNormal(setIndex);
}
else if (dispMode == SET_TIME_LIMIT) //工作时间设定
{
DispSetLimit(setIndex);
}
else if (dispMode == SET_LIGHT) //光强阈值设定
{
DispSetLight(setIndex);
}
else if (dispMode == CHECK_LIGHT) //灯光检测界面
{
DispCheckLight(setIndex);
}
}
KeyProcess(); //按键处理
}
}
void LampCtr()
{
lightValue = 99 - 99 * ReadADC1(AIN0_GND) / 255; //读取当前光照强度
if (modeFlag == 1) //在定时模式下
{
if ((timeBufDec[4] == timeLimit[0] && timeBufDec[5] >= timeLimit[1]) || (timeBufDec[4] > timeLimit[0]) \
|| (timeBufDec[4] == timeLimit[2] && timeBufDec[5] < timeLimit[3]) || (timeBufDec[4] < timeLimit[2])) //工作时间内路灯操作
{
lampASwitch = LAMP_ON;
if ((timeBufDec[4] >= 0 && timeBufDec[4] < timeLimit[2]) || (timeBufDec[4] == timeLimit[2] && timeBufDec[5] < timeLimit[3]))
{
if (!lampBDelay)
{
lampBSwitch = LAMP_OFF;
if (IRFLAG == 0) //检测到有人或车经过 启动10s定时
{
lampBDelay = 1;
}
}
}
else
{
lampBSwitch = LAMP_ON;
lampBDelay = 0;
}
}
else
{
lampASwitch = LAMP_OFF;
lampBSwitch = LAMP_OFF;
}
}
if (modeFlag == 2) //在自动模式下
{
if ((timeBufDec[4] == timeLimit[0] && timeBufDec[5] >= timeLimit[1]) || (timeBufDec[4] > timeLimit[0]) \
|| (timeBufDec[4] == timeLimit[2] && timeBufDec[5] < timeLimit[3]) || (timeBufDec[4] < timeLimit[2])) //工作时间内路灯操作
{
lampASwitch = LAMP_ON;
if ((timeBufDec[4] >= 0 && timeBufDec[4] < timeLimit[2]) || (timeBufDec[4] == timeLimit[2] && timeBufDec[5] < timeLimit[3]))
{
if (!lampBDelay)
{
lampBSwitch = LAMP_OFF;
if (IRFLAG == 0) //检测到有人或车经过 启动10s定时
{
lampBDelay = 1;
}
}
}
else
{
lampBSwitch = LAMP_ON;
lampBDelay = 0;
}
}
else //工作时间外路灯操作
{
if (lightValue < lightLimit) //光照强度低于设定阈值
{
lampASwitch = LAMP_ON;
lampBSwitch = LAMP_ON;
}
else
{
lampASwitch = LAMP_OFF;
lampBSwitch = LAMP_OFF;
}
}
}
if (lampASwitch == LAMP_ON)
{
LAMPA = LAMP_ON;
}
else
{
LAMPA = LAMP_OFF;
}
if (lampBSwitch == LAMP_ON)
{
LAMPB = LAMP_ON;
}
else
{
LAMPB = LAMP_OFF;
}
}
void CheckLamp()
{
if (lampASwitch == LAMP_ON) //路灯A打开时,检测对应光强
{
lightValueA = 99 - 99 * ReadADC2(AIN1_GND) / 255; //读取当前光照强度
if (lightValueA < 50) //路灯A故障声光报警 阈值可根据实际情况调整
{
lampAError = 1;
BUZZER = 0; //打开蜂鸣器
}
else
{
lampAError = 0;
}
}
if (lampBSwitch == LAMP_ON) //路灯B打开时,检测对应光强
{
lightValueB = 99 - 99 * ReadADC2(AIN0_GND) / 255; //读取当前光照强度
if (lightValueB < 50) //路灯B故障声光报警 阈值可根据实际情况调整
{
lampBError = 1;
BUZZER = 0; //打开蜂鸣器
}
else
{
lampBError = 0;
}
}
if (lampAError == 0 && lampBError == 0) //灯都没有故障
{
BUZZER = 1; //关闭蜂鸣器
}
if (lampASwitch == LAMP_OFF && lampBSwitch == LAMP_OFF) //灯全部关闭
{
BUZZER = 1; //关闭蜂鸣器
}
}
//定时器0初始化
void Timer0_Init(void)
{
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0x00; //设置定时初值
TH0 = 0x4C; //设置定时初值 50ms
TR0 = 1; //启动T0计时
ET0 = 1; //打开T0中断
EA = 1; //打开总中断
}
//定时器0中断
void Timer0_Intterupt(void) interrupt 1
{
static unsigned char cnt = 0;
static unsigned int time50ms = 0;
TL0 = 0x00; //设置定时初值
TH0 = 0x4C; //设置定时初值 50ms
cnt++;
if (cnt >= 5) //250ms刷新一次状态
{
cnt = 0;
dispFlag = 1;
}
if (modeFlag == 1 || modeFlag == 2) //在自动模式或定时模式下
{
if (lampBDelay == 1)
{
time50ms++;
lampBSwitch = LAMP_ON;
if (time50ms >= 200) //定时10s关闭路灯B
{
lampBDelay = 0;
lampBSwitch = LAMP_OFF;
time50ms = 0;
}
}
}
}
实物视频:
https://www.bilibili.com/video/BV17g411e7Td/
仿真视频:
https://www.bilibili.com/video/BV15S4y1z7tw/