CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

BH1750 Digital Light Sensor / Lux Meter Chip Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

BH1750 Digital Light Sensor / Lux Meter Chip Driver
PostPosted: Tue Apr 30, 2013 9:02 pm     Reply with quote

BH1750.h

Code:
#use I2C (Master, SDA = PIN_A0, SCL = PIN_A1)   


#define  BH1750_address_w 0x46                               
#define  BH1750_address_r 0x47
         
#define  power_down 0x00
#define  power_up 0x01
#define  reset 0x07
#define  cont_H_res_mode1 0x10
#define  cont_H_res_mode2 0x11 
#define  cont_L_res_mode 0x13                                         
#define  one_time_H_res_mode1 0x20
#define  one_time_H_res_mode2 0x21
#define  one_time_L_res_mode 0x23                                     
                       

void BH1750_init();
void BH1750_write(unsigned char cmd);                                           
unsigned long BH1750_read_word();
unsigned long get_lux_value(unsigned char mode, unsigned long delay_time); 



BH1750.c

Code:
#include "BH1750.h"
                                     
             
void BH1750_init()
{
   delay_ms(100); 
   BH1750_write(power_down);
}                 
             

void BH1750_write(unsigned char cmd)
{
   I2C_start();
   I2C_write(BH1750_address_w);       
   I2C_write(cmd);               
   I2C_stop();   
}

             
                                   
unsigned long BH1750_read_word()
{                     
   register unsigned long value = 0;
   unsigned char lb = 0;
   unsigned char hb = 0;
   I2C_start();
   I2C_write(BH1750_address_r);     
   hb = I2C_read(1); 
   lb = I2C_read(0);
   I2C_stop();                           
   value = make16(hb, lb);     
   return value;
}


unsigned long get_lux_value(unsigned char mode, unsigned long delay_time)
{
   register unsigned long lux_value = 0; 
   BH1750_write(power_up);
   BH1750_write(mode);
   lux_value = BH1750_read_word();
   delay_ms(delay_time);
   BH1750_write(power_down);
   return lux_value;                                 
}                         

_________________
https://www.facebook.com/MicroArena

SShahryiar
mstbzr



Joined: 02 Jan 2015
Posts: 1
Location: türkiye

View user's profile Send private message

PostPosted: Fri Jan 02, 2015 3:05 am     Reply with quote

Rica etsem örnek bir kod yazmanız mümkün mü?

Is it possible to write a sample code please?
scanan



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

View user's profile Send private message Visit poster's website

PostPosted: Tue May 26, 2015 8:00 am     Reply with quote

mstbzr wrote:
Rica etsem örnek bir kod yazmanız mümkün mü?

Is it possible to write a sample code please?



Here is an application that I did with PIC18F6310 and works quite well

luxmeter.h
Code:

#include <18F6310.h>
#device *=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                       //High speed osc with HW enabled 4X PLL
#FUSES NOBROWNOUT
#FUSES PUT                    //No Power Up Timer
#FUSES NOSTVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOFCMEN                    //Fail-safe clock monitor enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOLPT1OSC
#FUSES NOMCLR   

#use delay(clock=16000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,FORCE_SW)
#use I2C (Master, SDA = PIN_F3, SCL = PIN_F2,FORCE_SW)


struct IOpins {
           BOOLEAN unused00;        //A0
           BOOLEAN unused01;        //A1 
           BOOLEAN unused02;        //A2
           BOOLEAN Aled2;           //A3
           BOOLEAN Aled1;           //A4 
           BOOLEAN unused03;        //A5 
           BOOLEAN unused04;        //A6 
           BOOLEAN unused05;        //A7 
           BOOLEAN unused06;        //B0 
           BOOLEAN unused07;        //B1 
           BOOLEAN unused08;        //B2 
           BOOLEAN unused09;        //B3 
           BOOLEAN unused10;        //B4
           BOOLEAN unused11;        //B5 
           BOOLEAN comLED;          //B6 
           BOOLEAN unused12;        //B7 
           BOOLEAN cpuLED;          //C0 
           BOOLEAN unused13;        //C1 
           BOOLEAN unused14;        //C2 
           BOOLEAN unused15;        //C3 
           BOOLEAN unused16;        //C4 
           BOOLEAN unused17;        //C5 
           BOOLEAN rs232Tx;         //C6 
           BOOLEAN rs232Rx;         //C7 
           BOOLEAN unused18;        //D0 
           BOOLEAN unused19;        //D1 
           BOOLEAN unused20;        //D2 
           BOOLEAN unused21;        //D3 
           BOOLEAN unused22;        //D4 
           BOOLEAN unused23;        //D5 
           BOOLEAN unused24;        //D6 
           BOOLEAN unused25;        //D7 
           BOOLEAN unused26;        //E0 
           BOOLEAN unused27;        //E1 
           BOOLEAN unused28;        //E2 
           BOOLEAN unused29;        //E3 
           BOOLEAN unused30;        //E4 
           BOOLEAN errorLED;        //E5 
           BOOLEAN unused31;        //E6 
           BOOLEAN unused32;        //E7 
           BOOLEAN addr;            //F0 
           BOOLEAN unused33;        //F1 
           BOOLEAN SCL;             //F2
           BOOLEAN SDA;             //F3
           BOOLEAN unused34;        //F4
           BOOLEAN unused35;        //F5
           BOOLEAN unused36;        //F6
           BOOLEAN unused37;        //F7
           BOOLEAN emergencyLED;    //G0
           BOOLEAN unused38;        //G1
           BOOLEAN unused39;        //G2
           BOOLEAN unused40;        //G3                 
           BOOLEAN unused41;        //G4
           //BOOLEAN unused42;      //G5
           //BOOLEAN unused43;      //G6
           //BOOLEAN unused44;      //G7
} IOpin;
#byte IOpin=0xF80


#define  BH1750_address_w 0x46                               
#define  BH1750_address_r 0x47
         
#define  power_down 0x00
#define  power_up 0x01
#define  reset 0x07
#define  cont_H_res_mode1 0x10
#define  cont_H_res_mode2 0x11 
#define  cont_L_res_mode 0x13                                         
#define  one_time_H_res_mode1 0x20
#define  one_time_H_res_mode2 0x21
#define  one_time_L_res_mode 0x23
unsigned int16 lightintensity=0;

void BH1750_init();
void BH1750_write(unsigned char cmd);                                           
unsigned long BH1750_read_word();
unsigned long get_lux_value(unsigned char mode, unsigned long delay_time);


luxmeter.c
Code:

#include "luxmeter.h"
             
void BH1750_init()
{
   delay_ms(100); 
   BH1750_write(power_down);
}                 

void BH1750_write(unsigned char cmd)
{
   I2C_start();
   I2C_write(BH1750_address_w);       
   I2C_write(cmd);               
   I2C_stop();   
}
                                   
unsigned long BH1750_read_word()
{                     
   register unsigned long value = 0;
   unsigned char lb = 0;
   unsigned char hb = 0;
   I2C_start();
   I2C_write(BH1750_address_r);     
   hb = I2C_read(1); 
   lb = I2C_read(0);
   I2C_stop();                           
   value = make16(hb, lb);     
   return value;
}

unsigned long get_lux_value(unsigned char mode, unsigned long delay_time)
{
   register unsigned long lux_value = 0;
   
   BH1750_write(power_up);
   BH1750_write(mode);
   lux_value = BH1750_read_word();
   delay_ms(delay_time);
   BH1750_write(power_down);
   return lux_value;                                 
}   

void main()
{
set_tris_a(0x00);
set_tris_b(0x24);
set_tris_c(0x80);
set_tris_d(0x00);
set_tris_e(0x15);
set_tris_f(0x58);
set_tris_g(0x10);

   setup_wdt(WDT_OFF);
   setup_adc(ADC_OFF);
   setup_adc_ports(NO_ANALOGS);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DISABLED, 0xFF, 0x10);
   setup_timer_3(T3_DISABLED);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(OSC_16MHZ);

   clear_interrupt(int_ext1);
   clear_interrupt(int_ext2);

IOpin.cpuLED=0;
IOpin.errorLED=0;
IOpin.comLED=0;
IOpin.emergencyLED=0;
// set BH1750 address to 0x23 when low
// set BH1750 address to 0x5C when high
IOpin.addr=0;

EXT_INT_EDGE(1,L_TO_H);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
disable_interrupts(INT_LOWVOLT);
disable_interrupts(INT_TIMER0);
disable_interrupts(INT_TIMER1);
disable_interrupts(GLOBAL);
BH1750_init();
IOpin.addr=0;

   for(;;)
   {
      lightintensity=get_lux_value(cont_H_res_mode1,1000);
      printf("lux value:%f \n\r",(float)lightintensity/1.2);
      IOpin.cpuLED=1;
      delay_ms(10);
      IOpin.cpuLED=0;
   }
}



_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Jul 07, 2017 8:37 pm     Reply with quote

Example Code

Code:

#include <16F87.h>

#device *= 16

#fuses INTRC_IO, PROTECT, NOWDT, BROWNOUT, NOWRT
#fuses NODEBUG, NOIESO, PUT, NOMCLR, FCMEN, CPD
                         
#use delay (internal = 4MHz)

                       
#include <lcd420.c>
#include "BH1750.c"
                               
void setup();   

void main()
{
      unsigned long lux = 0;   
      setup();
      while(TRUE)
      {
         lux = get_lux_value(cont_H_res_mode1, 180);        
         lcd_gotoxy(6, 2); 
         printf(lcd_putc, "%05lu    ", lux); 
         delay_ms(100);                         
      };     
}
         

void setup()
{                                         
   disable_interrupts(global);
   setup_oscillator(OSC_4MHz);           
   setup_WDT(WDT_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_SPI(SPI_disabled | SPI_SS_disabled);
   setup_timer_0(0 | 0 | 0);
   setup_timer_1(T1_disabled);
   setup_timer_2(T2_disabled, 255, 1);
   setup_CCP1(CCP_off);
   set_timer0(0);
   set_timer1(0);
   set_timer2(0);
   BH1750_init();
   lcd_init();
   lcd_putc("\f");
   lcd_gotoxy(1, 1); 
   lcd_putc("Ambient Light/lx");
}

_________________
https://www.facebook.com/MicroArena

SShahryiar
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group