You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
7.9 KiB

#ifndef __LIGHT_H_
#define __LIGHT_H_
#include <REG51.H>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#define SlaveAddress 0x46 //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĴӵ<EFBFBD>ַ,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ALT ADDRESS<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>Ų<EFBFBD>ͬ<EFBFBD>޸<EFBFBD>
//ALT ADDRESS<EFBFBD><EFBFBD><EFBFBD>Žӵ<EFBFBD>ʱ<EFBFBD><EFBFBD>ַΪ0x46<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>Դʱ<EFBFBD><EFBFBD>ַΪ0xB8
sbit SCL=P1^0; //IICʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><EFBFBD><EFBFBD>
sbit SDA=P1^1; //IIC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD><EFBFBD><EFBFBD>
typedef unsigned short WORD;
unsigned char BUF[8]; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int dis_data; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void delay_nms(unsigned int k);
void InitLcd();
void Init_BH1750(void);
void WriteDataLCM(uchar dataW);
void WriteCommandLCM(uchar CMD,uchar Attribc);
void DisplayOneChar(uchar X,uchar Y,uchar DData);
void conversion(uint temp_data);
void Single_Write_BH1750(uchar REG_Address); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uchar Single_Read_BH1750(uchar REG_Address); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void Multiple_Read_BH1750(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD>ȡ<EFBFBD>ڲ<EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//------------------------------------
void Delay5us();
void Delay5ms();
void BH1750_Start(); //<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
void BH1750_Stop(); //ֹͣ<EFBFBD>ź<EFBFBD>
void BH1750_SendACK(bit ack); //Ӧ<EFBFBD><EFBFBD>ACK
bit BH1750_RecvACK(); //<EFBFBD><EFBFBD>ack
void BH1750_Sendunsigned char(unsigned char dat); //IIC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>д
unsigned char BH1750_Recvunsigned char(); //IIC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽڶ<EFBFBD>
//-----------------------------------
//*********************************************************
void conversion(uint temp_data) // <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʮ<EFBFBD><EFBFBD><EFBFBD>٣<EFBFBD>ǧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
bai=temp_data/100+0x30;
temp_data=temp_data%100; //ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
shi=temp_data/10+0x30;
temp_data=temp_data%10; //ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ge=temp_data+0x30;
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ**************************
void delay_nms(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{;}}
}
/*******************************/
void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}
/*******************************/
void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
/*******************************/
void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
}
/***********************************/
void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}
/***********************************/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
/**************************************
<EFBFBD><EFBFBD>ʱ5΢<EFBFBD><EFBFBD>(STC90C52RC@12M)
<EFBFBD><EFBFBD>ͬ<EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>ʱ<EFBFBD>ӹ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ҫ<EFBFBD>޸<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1T<EFBFBD><EFBFBD>MCUʱ,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**************************************/
void Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/**************************************
<EFBFBD><EFBFBD>ʱ5<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(STC90C52RC@12M)
<EFBFBD><EFBFBD>ͬ<EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1T<EFBFBD><EFBFBD>MCUʱ,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**************************************/
void Delay5ms()
{
WORD n = 560;
while (n--);
}
/**************************************
<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
**************************************/
void BH1750_Start()
{
SDA = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SCL = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
SDA = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
SCL = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/**************************************
ֹͣ<EFBFBD>ź<EFBFBD>
**************************************/
void BH1750_Stop()
{
SDA = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SCL = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
SDA = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
}
/**************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>:ack (0:ACK 1:NAK)
**************************************/
void BH1750_SendACK(bit ack)
{
SDA = ack; //дӦ<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
SCL = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
SCL = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
}
/**************************************
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
**************************************/
bit BH1750_RecvACK()
{
SCL = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
CY = SDA; //<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
SCL = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
return CY;
}
/**************************************
<EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD><EFBFBD>߷<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**************************************/
void BH1750_Sendunsigned char(unsigned char dat)
{
unsigned char i;
for (i=0; i<8; i++) //8λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
dat <<= 1; //<EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
SDA = CY; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
SCL = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
SCL = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
}
BH1750_RecvACK();
}
/**************************************
<EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**************************************/
unsigned char BH1750_Recvunsigned char()
{
unsigned char i;
unsigned char dat = 0;
SDA = 1; //ʹ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
for (i=0; i<8; i++) //8λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
dat <<= 1;
SCL = 1; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
dat |= SDA; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SCL = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Delay5us(); //<EFBFBD><EFBFBD>ʱ
}
return dat;
}
//*********************************
void Single_Write_BH1750(uchar REG_Address)
{
BH1750_Start(); //<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
BH1750_Sendunsigned char(SlaveAddress); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ź<EFBFBD>
BH1750_Sendunsigned char(REG_Address); //<EFBFBD>ڲ<EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>
// BH1750_Sendunsigned char(REG_data); //<EFBFBD>ڲ<EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>
BH1750_Stop(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹͣ<EFBFBD>ź<EFBFBD>
}
//********<EFBFBD><EFBFBD><EFBFBD>ֽڶ<EFBFBD>ȡ*****************************************
/*
uchar Single_Read_BH1750(uchar REG_Address)
{ uchar REG_data;
BH1750_Start(); //<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
BH1750_Sendunsigned char(SlaveAddress); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ź<EFBFBD>
BH1750_Sendunsigned char(REG_Address); //<EFBFBD><EFBFBD><EFBFBD>ʹ洢<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʼ
BH1750_Start(); //<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
BH1750_Sendunsigned char(SlaveAddress+1); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ+<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
REG_data=BH1750_Recvunsigned char(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
BH1750_SendACK(1);
BH1750_Stop(); //ֹͣ<EFBFBD>ź<EFBFBD>
return REG_data;
}
*/
//*********************************************************
//
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>BH1750<EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//
//*********************************************************
void Multiple_read_BH1750(void)
{ uchar i;
BH1750_Start(); //<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
BH1750_Sendunsigned char(SlaveAddress+1); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ+<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
for (i=0; i<3; i++) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ2<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>BUF
{
BUF[i] = BH1750_Recvunsigned char(); //BUF[0]<EFBFBD>洢0x32<EFBFBD><EFBFBD>ַ<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (i == 3)
{
BH1750_SendACK(1); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>NOACK
}
else
{
BH1750_SendACK(0); //<EFBFBD><EFBFBD>ӦACK
}
}
BH1750_Stop(); //ֹͣ<EFBFBD>ź<EFBFBD>
Delay5ms();
}
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>BH1750<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD>pdf<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>****
void Init_BH1750()
{
Single_Write_BH1750(0x01);
}
#endif