SCM Control Digital Illumination Intensity Sensor Module GY-30 (main chip BH1750FVI) modular programming (continuous update) __ programming

Source: Internet
Author: User

Here will I write stc12c5a60s2 single-chip microcomputer control Digital Light intensity module GY-30 (main chip BH1750FVI) program sharing, is to let seniors give treatise.

Update:

2014/04/29 14:05

(Supplemental: The following code only needs to modify the section of the. h file that contains the word "select" to achieve the effect of multiplexing)

For the serial debugging part, please refer to "SCM Program Assistance Debugging method (a) Serial debugging" Click to enter

For BH1750FVI There are 6 modes of operation, the model chosen in this article is continuously h-resolution mode





Test procedure:

#include <reg52.h>
#include "BH1750FVI.h"
#include "uart.h"

void Main ()
{  
	uartinit ();
	Bh1750fviinit ();      
	while (1)              
	{ 
		uartsendvalue ("Ambient light Value  (LX):", Bh1750fvireadresult ());         
	}
} 




/*############### #BH1750FVI. h Start ################*/

#ifndef __bh1750fvi_h__ #define __BH1750FVI_H__ #include <reg52.h> #include "common.h" Sbit bh1750fvi_sclk_bit=p2      ^0;       /* According to Hardware selection */Sbit bh1750fvi_sda_bit=p2^1; /* Depending on hardware selection */#define BH1750FVI_ADDRESS_ON_ADD_LOW_LEVEL 0x46 #define BH1750FVI_ADDRESS_ON_ADD_HIGH_LEVEL 0xb8/* Note:
           Here select Bh1750fvi_address_on_add_high_level when the add pin is connected to VCC and select Bh1750fvi_address_on_add_low_level when the add pin is connected to GND. In the lab, I picked up gnd, so I chose bh1750fvi_address_on_add_low_level according to the Hardware connection choice */#define BH1750FVI_ADDRESS Bh1750fvi_addre Ss_on_add_low_level #define Bh1750fvi_read 0x01 #define Bh1750fvi_write (0x01 & (~ (0x01<<0)))//Command (refer to Datas Heet) #define BH1750FVI_POWER_DOWM 0x00 #define BH1750FVI_POWER_ON 0x01 #define BH1750FVI_RESET 0x07 #define BH1750FVI _continuously_h_resolution_mode 0x10 #define BH1750FVI_CONTINUOUSLY_H_RESOLUTION_MODE2 0x11 #define Bh1750fvi_ Continuously_l_resolution_mode 0x13 #define Bh1750fvi_one_time_h_resolution_mode 0x20 #define BH1750fvi_one_time_h_resolution_mode2 0X21 #define BH1750FVI_ONE_TIME_L_RESOLUTION_MODE 0x23/***************** External interface function * *

///Initialize, power up, mode select extern void Bh1750fviinit ();

Read result extern UW16 bh1750fvireadresult (void);
Write command for extern void Bh1750fviwritecommand (UB8 command);
 /**********************************************/#endif/*__bh1750fvi_h__*/


/*################bh1750fvi. h End ################*/


/*################bh1750fvi. C Start ################*/

/*************************************************************************** MODULE:BH1750FVI.C purpose:i Mplementation of BH1750FVI module.

(Digital light intensity module GY-30) version:0.01 2014/02/03 (OK) complier:keil 8051 C complier V9.01 mcu:stc12c5a60s2 Author:yangrui qq:2 79729201 email:yangrui90s@163.com Modification: ================= 2014/04/29 14:35 reason:1. The way to work here is Continuous
		Ly h-resolution mode, which is a continuous high-resolution model.
		
		In addition, there are other modes of work, as summarized in the 6 working modes: continuously h-resolution mode;(at 1LX resolution,measurement time is typically 120ms) Continuously h-resolution Mode2;(at 0.5LX resolution,measurement time is typically 120ms) continuously L-resolut Ion Mode;(at 4LX resolution,measurement time was typically 16ms) one time h-resolution Mode;(at 1LX resolutio
										  N,measurement time is typically 120ms, It's autionmatically set to power dowm mode after measurement, So if want to read, you shoule set "power on") One time H-resolution Mode;(at 0.5LX resolution,measurement time is typically 120ms, It's autionmatically set to power      DOWM mode after measurement, so if want to read, you shoule set "power on") One time l-resolution mode  ;(at 4LX resolution,measurement time is typically 16ms, It's autionmatically set to power DOWM mode after Measurement, so if want to read, you shoule set "power on") 2.bh1750fvi in the internal address write data and other IIC devices slightly different, other IIC devices in the transmission device ground The internal address of the device is also required, but when the BH1750FVI writes the data, the command is written, so the internal device address is not required, so the function is named directly as Bh1750fviwritecommand (...),

The code here by my at24c02.c evolved, if there are many IIC devices in the project, want to encapsulate the IIC communication protocol, we must pay attention to this detail. ================= ***************************************************************************/#include < intrins.h> #include "BH1750FVI.h"/* External interface function declared in BH1750FVI.h *//***************** intrinsic function ******************///static
void Delay5usforbh1750fvi (void);
static void Delay180msforbh1750fvi (void);
static void bh1750fvistartsignal (void);static void bh1750fvistopsignal (void);
static void Bh1750fvifreebus (void);
static void Bh1750fviacknowledge (void);
static void Mcuacknowledge (void);
static void Bh1750fviwritebyte (UB8 dat);
Static UB8 Bh1750fvireadbyte ();

static void Bh1750fvipoweron (void); 	/**********************************************////******************************************************//Function :d elay5usforbh1750fvi//input:n/a//output:n/a//return:n/a//description:n/a//note: Generated by the Stc-isp V6.69 software for the corresponding MCU, if
MCU is different//best to modify this function. ///static void Delay5usforbh1750fvi (void)//@11.0592mhz// {//unsigned char i;////_NOP_ ();//_nop_ ();//_nop_ ();//I = ten;//while (I.);//}/**************************** Function:d elay180msforbh1750fvi input:n/a output:n/a return:n/a description:n/a Note:
Generated by the Stc-isp V6.69 software for the corresponding MCU, it is best to modify this function if the MCU is different. /Static VOID delay180msforbh1750fvi (void)//@11.0592mhz {unsigned char I, j, K;
	i = 8;
	j = 145;
	k = 99;
		Do {does {while (--k);
	} while (--J);
} while (i.); 		}/****************************************************** function:bh1750fvistartsignal input:n/a output:n/a Return : N/A description:bh1750fvi start signal note:n/a ******************************************************/static void
	Bh1750fvistartsignal (void) {bh1750fvi_sda_bit = High_level;
	_nop_ ();
	Bh1750fvi_sclk_bit = High_level;
	_nop_ ();  
	Bh1750fvi_sda_bit = Low_level; 
_nop_ ();		}/****************************************************** function:bh1750fvistopsignal input:n/a output:n/a Return : N/a DESCRIPTION:BH1750FVI stop signal note:n/a ******************************************************/static void BH
	1750FVIStopSignal (void) {bh1750fvi_sda_bit = Low_level;
	_nop_ ();
	Bh1750fvi_sclk_bit = High_level;
	_nop_ ();
	Bh1750fvi_sda_bit = High_level; 
_nop_ (); }

/****Function:bh1750fvifreebus input:n/a output:n/a return:n/a DESCRI Ption: Release bus note:n/a ******************************************************/static void Bh1750fvifreebus (void) {//FR
	EE bus bh1750fvi_sclk_bit = high_level;
Bh1750fvi_sda_bit = High_level; 		}/****************************************************** Function:bh1750fviacknowledge input:n/a output:n/a Return : N/A Description: Host waits for BH1750FVI answer note:n/a ******************************************************/static void BH1750

	Fviacknowledge (void) {UB8 i=0;
	Bh1750fvi_sclk_bit = Low_level;

	_nop_ ();
	Bh1750fvi_sclk_bit = High_level;

	
	_nop_ (); while ((Bh1750fvi_sda_bit) && (i<250)) {i++;//temporarily, see Debug//Tested, here's 250 big enough} bh1750fvi_sclk_bit = Low_leve
L
}/****************************************************** Function:mcuacknowledge input:n/a output:n/a return:n/a Description: When the host receives BH1750FVI data as a receiver, it sends a response signal note:n/a * * */static void Mcuacknowledge (void) {bh1750fvi_sclk_bit = Low_
	level;
	_nop_ ();
	Bh1750fvi_sda_bit = Low_level;
	_nop_ ();
Bh1750fvi_sclk_bit = High_level; }/****************************************************** function:bh1750fviwritebyte Input:d at output:n/a Return: N/a description:write byte-data to Bh1750fvi note:n/a ******************************************************/Static V

    OID bh1750fviwritebyte (UB8 dat) {UB8 i;
		for (i=0; i<8; i++) {bh1750fvi_sclk_bit = Low_level;

		_nop_ ();

		Method One bh1750fvi_sda_bit = (bit) (Dat & (0x80>>i));
		Method two//temp <<= 1;

		Bh1750fvi_sda_bit = CY;
		DELAY5USFORBH1750FVI ();/* After testing can be shielded *///_nop_ ();
		Bh1750fvi_sclk_bit = High_level;
        
    _nop_ ();		}}/****************************************************** function:bh1750fvireadbyte input:n/a output:n/a Return : Byte-data from Bh1750fvi Description: Reading WordSection data note:n/a ******************************************************/static UB8 bh1750fvireadbyte () {UB8 i;

	UB8 Datacode = 0x00;
	Ready/*data on SDA pin during SCL low timer period*/bh1750fvi_sclk_bit = low_level;
	_nop_ ();
	Bh1750fvi_sda_bit = High_level;

	_nop_ ();
		for (i=0; i<8; i++) {bh1750fvi_sclk_bit = High_level;
		_nop_ ();
		Datacode<<= 1;
		Datacode |= bh1750fvi_sda_bit;
		_nop_ ();
		Bh1750fvi_sclk_bit = Low_level;
	_nop_ ();
} return Datacode; }/****************************************************** Function:bh1750fviwritecommand Input:command Code Output: N/A return:n/a Description: Write Bh1750fvi built-in command note:n/a ******************************************************/void BH17  

    50FVIWriteCommand (UB8 command) {bh1750fvistartsignal (); Bh1750fviwritebyte (bh1750fvi_address |   
	Bh1750fvi_write);
    
    Bh1750fviacknowledge (); 
    Bh1750fviwritebyte (command);

    Bh1750fviacknowledge (); Bh1750fvIfreebus ();		}/****************************************************** function:bh1750fvireadresult input:n/a output:n/a Return : Ambient Light value Description: Read brightness value note:n/a ******************************************************/UW16
	Bh1750fvireadresult (void) {UW16 temp;

    UB8 msb,lsb;  
    
    Bh1750fvistartsignal (); Bh1750fviwritebyte (bh1750fvi_address |
    Bh1750fvi_read);

    Bh1750fviacknowledge ();
    msb= Bh1750fvireadbyte ();                
    
    Mcuacknowledge ();
    lsb= Bh1750fvireadbyte ();                         

    /* After a byte is read, the host replaces the answer signal with the end signal */bh1750fvistopsignal ();

    Bh1750fvifreebus (); temp = ((UW16) (MSB)) <<8 |

    LSB;
    temp = (float) temp/1.2;
/* There is an implicit type conversion: float--> unsigned int */return temp; }/****************************************************** Function:bh1750fvipoweron input:n/a output:n/a return:n/ A Description: "Power on" command Note: for the Power on command, if you use "one time ..." mode, the hardware will automatically switch to power DOW after the result is convertedM mode, so also need to re-use power-on command, in the program, I used the mode is "continuously h-resolution mode", so does not automatically switch to power-down mode, only need to call this function when initializing.
			 If you use a different pattern, you may need to call it elsewhere.
Other features need to be encapsulated according to the actual situation. /static void Bh1750fvipoweron (void) {Bh1750fviwritecommand ( 	BH1750FVI_POWER_ON); Power on}/****************************************************** function:init_bh1750 input:n/a output:n/a Retur N:N/A Description: Initialize note:n/a ******************************************************/void Bh1750fviinit () {BH17
   50FVIPowerOn (); 
   
   /* Mode selected for continuously h-resolution mode*/Bh1750fviwritecommand (Bh1750fvi_continuously_h_resolution_mode);
Delay180msforbh1750fvi ();
 }


/*################bh1750fvi. C end################*/



Add: Common.h

#ifndef __common_h__
#define __COMMON_H__

typedef unsigned char UB8;
typedef unsigned short int UW16;
typedef unsigned long UL32;

typedef char SB8;
typedef short int SW16;
typedef long SL32;
	
#define HIGH_LEVEL 1	
#define Low_level  0


#endif	/*__common_h__*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.