找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 902|回复: 0
打印 上一主题 下一主题
收起左侧

单片机BH1750光强控灯程序

[复制链接]
跳转到指定楼层
楼主
ID:1015204 发表于 2022-4-11 14:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg51. h>
#include <intrins. h>
#include <LCD1602.h>
#include "BH1750. h"
#include "keys. h"
/*Declare SFR associated with the PCA */
sfr CCON=0xD8;  //PCA control register
sbit CCF0=CCON^0;  //PCA module-0 interrupt flag
sbit CCF1=CCON^1;  //PCA module- 1 interrupt flag
sbit CR=CCON^6;  //PCA timer run control bit
sbit CF=CCON^7;  //PCA timer overflow flag
sfr CMOD=0xD9;  //PCA mode register
sfr CL=0xE9;  //PCA base timer LOW
sfr CH=0xF9;  //PCA base timer HIGH
sfr CCAPMO=0xDA;  //PCA module-0 mode regi ster
sfr CCAPOL=0xEA;  //PCA module-0 capture register LOW
sfr CCAPOH=0xFA;  //PCA module 0 capture register HIGH
sfr CCAPM1=0xDB;  //PCA module-1 mode register
sfr CCAP1L=0xEB;  //PCA module-1 capture register LOW
sfr CCAP1H=0xFB;  //PCA module-1 capture register HIGH
sfr PCAPWMO=0xf2;
sfr PCAPWM1=0xf3;
uchar BH1750_ Con=0;
uchar BH1750_ Change=0;
uchar View_ Change=0;
void InitTimer0 (void)
{
TMOD = 0x01;
THO = 0xD8;
TL0 = 0xF0;
EA=1;
ETO = 1;
TRO=1;
}
void PWM_ Init()
{
CCON = 0;  //Initial PCA control register
CL= 0;  //Reset PCA base timer
CH=0;
CMOD = 0x02;  //Set PCA timer clock source as Fosc/2
CCAPOH = CCAPOL = 128;  //PWMO port output 50% duty cycle square
wave
CCAPMO = 0x42;  //PCA module-0 work in 8-bit PWM mode
and no PCA interrupt
CR=1;  //PCA timer start run
//PCA timer start run
}
uint Light_ Set=2000;
uchar Set_ Value_ View[7];
uint Bijiao_ Num_ Collect=0;
uint Bijiao_ Num_ Set=0;
void main()
{
PWM_ Init(); //PWM ???
InitLcd() ;  //?????
Init_BH1750();//??????????
InitTimer0();  //??????
Lcd_ 1602_ word(0x80, 6, "Light:");
Lcd_ 1602_ _word(0xc0, 16, "L_ Set:2000XL");
while(1)
{
if(Key_ Change) //? ?????
Key_ _Change=0;
if(Key_ Value==1) //?? ?+
{
if (Light_ _Set<60000)
Light_ Set=Light_ Set+100;
}
else
{
if(Light_ Set>1000) //?? ?-
Light_ Set=Light_ Set-100;
Set_ _Value_ View[O]=Light_ Set/10000+0x30;
Set_ Value_ View[1]=Light_ Set%10000/ 1000+0x30;
Set_ Value_ View[2]=Light_ Set%1000/ 100+0x30;
Set_ Value_ View[3]=Light_ Set%100/ 10+0x30;
Set_ Value_ View[4]=Light_ Set%10+0x30;
Set_ Value_ View[5]='X';
Set_ Value_ View[6]=' L' ;
if(Set_ _Value_ View[0]==0x30)
{
Set_ Value_ View[0]='' ;
if (Set_Value_ View[1]==0x30)
{
Set_ Value_ View[1]='' ;
if (Set_Value_ View[2]==0x30)
{
        Set_ Value_ View[2]='' ;
if (Set_Value_ View[3]==0x30)
{
        Set_ Value_ View[3]='' ;
if (Set_Value_ View[4]==0x30)
        Set_ Value_ View[4]='' ;
}
}
}
}
Lcd_ 1602_ word(0xc9, 7, Set_Value_View) ;
}
if (BH1750_Change) //? ???????
{
BH1750_ Change=0;
Read_ LightValue();
Lcd_ 1602_ _word(0x89, 7, Light_ Value_ View);
Bi jiao_ Num_ Collect=BH1750_ Data;
Bijiao_ Num_ Set=Light_ Set;
Bijiao_ Num_ Collect=Bijiao_ Num_ _Collect/100;
Bijiao_ _Num_ Set=Bijiao_ Num_ Set/100; .
if((Bijiao_ _Num_ _Collect<(Bijiao_ Num_ Set-1))|| (Bijiao_ _Num_ _Collect<10))
//????
{
if (CCAPOH>2)
{
CCAPOH=CCAP0H-2;
}
else
CCAPOH=0;
}
if(Bijiao_ _Num_ Collect>(Bijiao_ Num_ Set+1))//????
{
if (CCAPOH<253)
{
CCAPOH=CCAPOH+2;
CCAP0H=CCAP0H+2;
}
else
CCAPOH=0xff;
}
}
}
}
void Timer0Interrupt (void) interrupt 1
{
THO = 0xD8;
TL0 = 0xF0;
Key_ Acquisition(); //????
BH1750_ _Con++;
if (BH1750_ _Con==10)
{
BH1750_ Con=0;
BH1750_ Change=1;
}
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表