#include<reg52.h>
#include<math.h>
#include<stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^7;
sbit lcdrs=P2^6;
sbit lcdwr=P2^5;
sbit sda=P2^0;
sbit scl=P2^1;
uchar ge,shi,bai,qian,wan; //????
uchar buffer[8]; //???????
int s; //??
void chaizi(uint temp) //????
{
wan=temp/10000+0x30 ;
temp=temp%10000; //????
qian=temp/1000+0x30 ;
temp=temp%1000; //????
bai=temp/100+0x30;
temp=temp%100; //????
shi=temp/10+0x30;
temp=temp%10; //????
ge=temp+0x30;
}
void delay_us() //??5us
{;;}
void delay_ms(uint t) //??t??
{
uchar i,j;
for(i=0;i<t;i++)
{
for(j=0;j<121;j++)
{;}};
}
/************I2C????************/
void BH1750_start() //????
{
sda=1;
scl=1;
delay_us();
sda=0;
delay_us();
scl=0;
}
void BH1750_stop() //????
{
sda=0;
scl=1;
delay_us();
sda=1;
delay_us();
}
void send_respons(bit ack) //?????
{
sda=ack; //?????
scl=1; //?????
delay_us();
scl=0;
delay_us();
}
bit get_respons() //??????
{
scl=1;
delay_us();
CY=sda;
scl=0;
delay_us();
return CY;
}
void send_byte(uchar date) //?IIC??????????
{
uchar i;
for(i=0;i<8;i++)
{
date<<=1;
sda=CY; //CY?PWS????
scl=1;
delay_us();
scl=0;
delay_us();
}
get_respons();
}
uchar get_byte() //?IIC??????????
{
uchar j;
uchar k=0;
sda=1; //??????sda???,????????
for(j=0;j<8;j++)
{
k<<=1;
scl=1;
delay_us();
k|=sda; //k????,?sda??;??sda?0??1;???????????
scl=0; //?????????,scl??
delay_us();
}
return k;
}
void write_add(uchar date)
{
BH1750_start();
send_byte(0x46); //ADD??,?????0x46
send_byte(date);
BH1750_stop();
}
void read_BH1750(void) //????BH1750????
{
uchar i;
BH1750_start();
send_byte(0x46+1);
for(i=0;i<3;i++)
{
buffer[i]=get_byte();
if(i == 3)
{
send_respons(1);
}
else
{
send_respons(0);
}
}
BH1750_stop();
delay_ms(5);
}
void BH1750_init()
{
write_add(0x01);
}
/************LCD??************/
void wait_enable(void)
{
P0=0xff;
lcdrs=0;
lcdwr=1;
delay_us();
lcden=1;
delay_us();
while(P0&0x80);
lcden=0;
}
void lcd_command(uchar command,uchar shuxin)//????
{
if(shuxin)
wait_enable();
lcdrs=0;
lcdwr=0;
delay_us();
P0=command;//????lcd??????P0?
delay_us();
lcden=1;
delay_us();
lcden=0;
}
void lcd_writebyte(uchar byte)//????
{
wait_enable();
lcdrs=1;
lcdwr=0;
delay_us();
P0=byte;
delay_us();
lcden=1;
delay_us();
lcden=0;
}
/*void lcd_set_xy(uchar x,uchar y)//??????
{
uchar address;
if(y==0) //?????????
address=0x80+x;
else
address=0xc0+x;
lcd_command(address);
}*/
void lcd_display(uchar x,uchar y,uchar h) //lcd????
{
y&=1;
x&=15;
if(y)x|=0x40;
x|=0x80;
lcd_command(x,0);
lcd_writebyte(h);
}
/*void lcd_clean() //lcd??
{
lcd_command(0x01);
delay_us();
}*/
void lcd1602_init()
{
lcd_command(0x38,1);
lcd_command(0x08,1);
lcd_command(0x01,1);
lcd_command(0x06,1);
lcd_command(0x0c,1);
}
void main()
{
float result;
delay_ms(100);
BH1750_init();
lcd1602_init();
while(1)
{
write_add(0x01);
write_add(0x10);
delay_ms(180); //??180ms
read_BH1750(); //??????,???BUF?
s=buffer[0];
s=(s<<8)+buffer[1];//????,?????
result=(float)s/1.2;
chaizi(result); //???????
lcd_display(0,0,'L');
lcd_display(1,0,'i');
lcd_display(2,0,'g');
lcd_display(3,0,'h');
lcd_display(4,0,'t');
lcd_display(5,0,':');
lcd_display(7,0,wan);
lcd_display(8,0,qian);
lcd_display(9,0,bai);
lcd_display(10,0,shi);
lcd_display(11,0,ge);
lcd_display(13,0,'l'); ////?????
lcd_display(14,0,'x');
lcd_display(0,1,'s');
lcd_display(1,1,'u');
lcd_display(2,1,'n');
lcd_display(3,1,'>');
lcd_display(4,1,'2');
lcd_display(5,1,'0');
lcd_display(9,1,'m');
lcd_display(10,1,'o');
lcd_display(11,1,'o');
lcd_display(12,1,'n');
lcd_display(13,1,'<');
lcd_display(14,1,'2');
lcd_display(15,1,'0');
}
}
|