基于51单片机的智能蓝牙路灯街灯控制系统proteus仿真原理图PCB

功能:
0.本系统采用STC89C52作为单片机
1.液晶实时显示时间/环境光强/工作模式
2.每隔5s向蓝牙串口发送一次信息
3.支持路灯故障检测
4.工作时间18~24时,两个路灯同时点亮,24时以后,B路灯关闭,若检测到由物体通过,路灯B点亮10s后关闭
5.非工作时间,光照强度低于设定光照阈值,两个路灯点亮
6.可蓝牙控制两个路灯状态
7.按键更改时间/工作时间/光照阈值/工作模式/路灯亮度
8.采用DC002作为电源接口可直接输入5V给整个系统供电

原理图:
在这里插入图片描述
在这里插入图片描述

PCB :
在这里插入图片描述

主程序:

#define __MAIN_C
#include <reg52.h>
#include "main.h"

bit dispFlag = 1;
bit setFlag = 0;
bit sendFlag = 0;
unsigned char setIndex = 0;
unsigned char lightValue;
unsigned char lightValueA;
unsigned char lightValueB;
unsigned char lightLimit = 70;
unsigned char timeLimit[4] = {18, 0, 5, 0};
char dispMode = DISP_NORMAL;
bit autoFlag = 0;
bit lampBDelay = 0;
bit lampAError = 0;
bit lampBError = 0;
bit lampASwitch;
bit lampBSwitch;
unsigned char lampAPWM = 3;
unsigned char lampBPWM = 3;
int cnt = 0;
unsigned int time500us = 0;
unsigned char i = 0;
unsigned char R_buf[4];

void main()
{
    //初始化
    lampASwitch = LAMP_OFF;
    lampBSwitch = LAMP_OFF;
    LCD_Init();
    DS1302_Init();
    EEPROM52_Read();
    if (isNew == 1)
    {
        DS1302_WriteTime();
    }
    DS1302_ReadTime();
    EEPROM52_Init();
    Timer0_Init();

    UART0_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);
            }
        }

        //发送一次串口信息
        if (sendFlag == 1)
        {
            sendFlag = 0;

            if (lampAError == 1)
            {
                sprintf(dispRow0, "A:%d Er  ", (int)lampAPWM);
            }
            else
            {
                sprintf(dispRow0, "A:%d Ok  ", (int)lampAPWM);
            }

            if (lampBError == 1)
            {
                sprintf(dispRow1, "B:%d Er", (int)lampBPWM);
            }
            else
            {
                sprintf(dispRow1, "B:%d Ok", (int)lampBPWM);
            }

            if (lampASwitch == LAMP_OFF)
            {
                UART0_SendStr("C",1);
            }
            else
            {
                UART0_SendStr("O",1);
            }
            UART0_SendStr(dispRow0, 8);

            if (lampBSwitch == LAMP_OFF)
            {
                UART0_SendStr("C",1);
            }
            else
            {
                UART0_SendStr("O",1);
            }

            UART0_SendStr(dispRow1,6);
            UART0_SendStr("\r\n", 2);
            
            if (autoFlag == 1)
            {
                sprintf(dispRow1, "%02d:%02d:%02d L:%02d A ", (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6], (int)lightValue);
            }
            else
            {
                sprintf(dispRow1, "%02d:%02d:%02d L:%02d M ", (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6], (int)lightValue);
            }
            UART0_SendStr(dispRow1, 16);
            UART0_SendStr("\r\n", 2);
            UART0_SendStr("\r\n", 2);
        }
        KeyProcess();
    }
}

void LampCtr()
{
    lightValue = 99 - 99 * ReadADC1(AIN0_GND) / 255; //读取当前光照强度
    if (autoFlag)
    {
        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;
            }
            
        }
    }
}

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; //关闭蜂鸣器
    }

}

void Timer0_Init(void)
{
    TMOD &= 0xF0;
    TMOD |= 0x01;
    TL0 = 0x33;		//设置定时初值
	TH0 = 0xFE;		//设置定时初值 500us
    TR0 = 1; //启动T0计时
    ET0 = 1; //打开T0中断
    EA = 1;  //打开总中断
}

void Timer0_Intterupt(void) interrupt 1
{
    static unsigned char ACount = 0;
    static unsigned char BCount = 0;

    TL0 = 0x33;		//设置定时初值
	TH0 = 0xFE;		//设置定时初值 500us

    if (cnt > 10000) //5s发送一次串口信息
    {
        sendFlag = 1;
        cnt = 0;
    }
    else
    {
        cnt++;
    }
    
    if (autoFlag)
    {
        if (lampBDelay == 1)
        {
            time500us++;
            lampBSwitch = LAMP_ON;
            if (time500us >= 20000) //定时10s关闭路灯B
            {
                lampBDelay = 0;
                lampBSwitch = LAMP_OFF;
                time500us = 0;
            }
        }
    }

    if (cnt % 500 == 0) //250ms刷新一次状态
    {
        dispFlag = 1;
        if (setFlag == 1)
        {
            LCD_WriteCommand(0x0F, 0);
            LCD_WriteCommand(0x0F, 0);
        }
        else
        {
            LCD_WriteCommand(0x0C, 0);
            LCD_WriteCommand(0x0C, 0);
        }
    }
    
    if (lampASwitch == LAMP_ON)
    {
        ACount++;
        if (ACount <= lampAPWM) //占空比调节
        {
            LAMPA = LAMP_ON;
        }
        else if ((ACount > lampAPWM) && (ACount <= 3))
        {
            LAMPA = LAMP_OFF;
        }
        else
        {
            ACount = 0;
        }
    }
    else
    {
        LAMPA = LAMP_OFF;
    }   
    
    if (lampBSwitch == LAMP_ON)
    {
        BCount++;
        if (BCount <= lampBPWM) //占空比调节
        {
            LAMPB = LAMP_ON;
        }
        else if ((BCount > lampBPWM) && (BCount <= 3))
        {
            LAMPB = LAMP_OFF;
        }
        else
        {
            BCount = 0;
        }
    }
    else
    {
        LAMPB = LAMP_OFF;
    }      
}

void UART0_Init()
{
    SCON = 0x50;
    TMOD &= 0x0F;
    TMOD |= 0x20; //8位自动重装
    TH1  = RH_UART;
    TL1  = TH1;
    ET1  = 0; //禁止T1中断
    TR1  = 1; //启动T1计时
    ES   = 1; //打开串口中断
    EA   = 1; //打开总中断
}

void UART0_SendByte(unsigned char dat) //串口发送单字节数据
{
	unsigned char time_out;
	time_out = 0x00;
	SBUF = dat;						  //将数据放入SBUF中
	while ((!TI) && (time_out < 100)) //检测是否发送出去
	{
		time_out++;
		DelayUs10x(2);
	}		//未发送出去 进行短暂延时
	TI = 0; //清除ti标志
}

void UART0_SendStr(unsigned char *str, unsigned char length) //发送定长度字符串
{
// 	unsigned char *tmp;
	unsigned char cnt = 0;
//    unsigned char length = 0;

//    tmp = str;
//    while (*(tmp++) != '\0')
//    {
//        length++;
//    }

	while (cnt < length) //发送长度对比
	{
		UART0_SendByte(*str); //发送单字节数据
		str++;		  //指针++
		cnt++;		  //下一个++
	}
}

void UART0_Interrupt() interrupt 4 
{

    if (RI)
    {
        RI = 0;
        if (SBUF == '*')
        {
            i = 0;
        }
        R_buf[i] = SBUF;
        SBUF = SBUF;
        i++;
        if (i == 4)
        {
            i = 0;
            if (R_buf[0] == '*' && R_buf[3] == '#')
            {
                if (R_buf[1] == 'A')
                {
                    if (R_buf[2] == '0')
                    {
                        lampASwitch = LAMP_OFF;
                        lampAPWM = 0;
                    }
                    else if (R_buf[2] == '1')
                    {
                        lampASwitch = LAMP_ON;
                        lampAPWM = 1;
                    }
                    else if (R_buf[2] == '2')
                    {
                        lampASwitch = LAMP_ON;
                        lampAPWM = 2;
                    }
                    else if (R_buf[2] == '3')
                    {
                        lampASwitch = LAMP_ON;
                        lampAPWM = 3;
                    }
                }
                else if (R_buf[1] == 'B')
                {
                    if (R_buf[2] == '0')
                    {
                        lampBSwitch = LAMP_OFF;
                        lampBPWM = 0;
                    }
                    else if (R_buf[2] == '1')
                    {
                        lampBSwitch = LAMP_ON;
                        lampBPWM = 1;
                    }
                    else if (R_buf[2] == '2')
                    {
                        lampBSwitch = LAMP_ON;
                        lampBPWM = 2;
                    }
                    else if (R_buf[2] == '3')
                    {
                        lampBSwitch = LAMP_ON;
                        lampBPWM = 3;
                    }
                }
                else if (R_buf[1] == 'M')
                {
                    if (R_buf[2] == 'A')
                    {
                        autoFlag = 1;
                    }
                    else if (R_buf[2] == 'M')
                    {
                        autoFlag = 0;
                    }
                }

            }
        }
    }
    
    if (TI)
    {
        TI = 0;
    }
}

仿真演示视频:
https://www.bilibili.com/video/BV1jf4y1b71a/

实物演示视频:
https://www.bilibili.com/video/BV1Su411q7v1/

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值