在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

BH1750.c

#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "BH1750.h"

#include  <math.h>    //Keil library  
#include  <stdlib.h>  //Keil library  
#include  <stdio.h>   //Keil library	
#include  <INTRINS.H> //Keil library 


#define   uchar unsigned char
#define   uint unsigned int	
#define	  SlaveAddress1   0x46  //定义器件在IIC总线中的从地址


uchar BUF1[8];//接收数据缓存区      	
int  dis_data1;
float BH1750_temp;


void Delay51us() //5us延时(不怎么准)
{
 nop; nop; nop; nop; nop; nop; nop; nop; nop;nop;nop; nop; nop; nop; nop; nop; nop; nop;nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop; nop;
}

					

/**************************************
起始信号
**************************************/
void BH1750_Start()
{
    SDA= 1;                    //拉高数据线
    SCL = 1;                    //拉高时钟线
    Delay51us();                 //延时
    SDA = 0;                    //产生下降沿
    Delay51us();                 //延时
    SCL = 0;                    //拉低时钟线
}

/**************************************
停止信号
**************************************/
void BH1750_Stop()
{
    SDA = 0;                    //拉低数据线
    SCL = 1;                    //拉高时钟线
    Delay51us();                 //延时
    SDA = 1;                    //产生上升沿
    Delay51us();                 //延时
}

/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void BH1750_SendACK(bit ack)
{
    SDA = ack;                  //写应答信号
    SCL = 1;                    //拉高时钟线
    Delay51us();                 //延时
    SCL = 0;                    //拉低时钟线
    Delay51us();                 //延时
}

/**************************************
接收应答信号
**************************************/
bit BH1750_RecvACK()
{
    SCL = 1;                    //拉高时钟线
    Delay51us();                 //延时
    CY = SDA;                   //读应答信号
    SCL = 0;                    //拉低时钟线
    Delay51us();                 //延时

    return CY;
}

/**************************************
向IIC总线发送一个字节数据
**************************************/
void BH1750_SendByte(uchar dat)
{
    uchar i;

    for (i=0; i<8; i++)         //8位计数器
    {
        dat <<= 1;              //移出数据的最高位
        SDA = CY;               //送数据口
        SCL = 1;                //拉高时钟线
        Delay51us();             //延时
        SCL = 0;                //拉低时钟线
        Delay51us();             //延时
    }
    BH1750_RecvACK();
}

/**************************************
从IIC总线接收一个字节数据
**************************************/
uchar BH1750_RecvByte()
{
    uchar i;
    uchar dat = 0;

    SDA = 1;                    //使能内部上拉,准备读取数据,
    for (i=0; i<8; i++)         //8位计数器
    {
        dat <<= 1;
        SCL = 1;                //拉高时钟线
        Delay51us();             //延时
        dat |= SDA;             //读数据               
        SCL = 0;                //拉低时钟线
        Delay51us();             //延时
    }
    return dat;
}

//***************************************************

void Single_Write_BH1750(uchar REG_Address)
{
    BH1750_Start();                  //起始信号
    BH1750_SendByte(SlaveAddress1);   //发送设备地址+写信号
    BH1750_SendByte(REG_Address);    //内部寄存器地址,
  //  BH1750_SendByte(REG_data);       //内部寄存器数据,
    BH1750_Stop();                   //发送停止信号
}


//******************************************************
//
//连续读出bh1750内部数据
//
//******************************************************
void Multiple_read_BH1750()
{   uchar i;
    BH1750_Start();                          //起始信号
    BH1750_SendByte(SlaveAddress1+1);           //发送设备地址+写信号
	for (i=0; i<3; i++)                      //连续读取6个地址数据,存储中BUF
    {
        BUF1[i] = BH1750_RecvByte();          //BUF[0]存储数据
        if (i == 3)
        {
           BH1750_SendACK(1);                //最后一个数据需要回NOACK
        }
        else
        {
          BH1750_SendACK(0);                //回应ACK
       }
   }
    BH1750_Stop();                          //停止信号
	Timer0_Delay1ms(5);
}


void Init_BH1750()
{
   Single_Write_BH1750(0X01);  
}


void EC_BH1750()//Electronic compass
{ 
	Single_Write_BH1750(0x01);   // power on
	Single_Write_BH1750(0x10);   // H- resolution mode
	Timer0_Delay1ms(180);
	Multiple_read_BH1750();       //连续读出数据,存储在BUF中
	dis_data1=BUF1[0];
	dis_data1=(dis_data1<<8)+BUF1[1];//合成数据,即光照数据 
	BH1750_temp=(float)dis_data1/1.2;           
} 

BH1750.h

#ifndef _BH1750_h_
#define _BH1750_h_
extern  float BH1750_temp;
void Init_BH1750();//初始化程序,只运行一次
void EC_BH1750();//读取光照强度
#endif

更多推荐