当前位置:网站首页>Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)
2022-06-25 07:37:00 【Assassin ari】
Here I write STC12C5A60S2 The single chip microcomputer controls the digital illumination intensity module GY-30( Main chip BH1750FVI) Share your program , It's to make the elder correct .
to update :
2014/04/29 14:05
( Add : The following code only needs to be modified .h The document contains “ choice ” Part of the word , The effect of reuse can be achieved )
For the serial port debugging part , Please refer to 《 SCM program assisted debugging method ( One ) Serial debugging 》 Click to enter
about BH1750FVI Yes 6 Working mode , The mode selected in the article is Continuously H-Resolution Mode



The test program :
#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 the hardware selection */
sbit BH1750FVI_sda_bit=P2^1; /* According to the hardware selection */
#define BH1750FVI_ADDRESS_ON_ADD_LOW_LEVEL 0x46
#define BH1750FVI_ADDRESS_ON_ADD_HIGH_LEVEL 0Xb8
/* Be careful : Here ADD Pin connection VCC Time selection BH1750FVI_ADDRESS_ON_ADD_HIGH_LEVEL,
When ADD Pin connection GND Time selection BH1750FVI_ADDRESS_ON_ADD_LOW_LEVEL.
In the lab. , I picked up GND, So I chose BH1750FVI_ADDRESS_ON_ADD_LOW_LEVEL
Select according to the hardware connection */
#define BH1750FVI_ADDRESS BH1750FVI_ADDRESS_ON_ADD_LOW_LEVEL
#define BH1750FVI_READ 0x01
#define BH1750FVI_WRITE (0x01 & (~(0x01<<0)))
// command ( reference datasheet)
#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 functions ******************/
// initialization , Access to electricity , Mode selection
extern void BH1750FVIInit() ;
// Read the results
extern UW16 BH1750FVIReadResult(void) ;
// Write orders
extern void BH1750FVIWriteCommand(UB8 command) ;
/**********************************************/
#endif /*__BH1750FVI_H__*/
/*################BH1750FVI.h end ################*/
/*################BH1750FVI.c start ################*/
/***************************************************************************
Module :BH1750FVI.c
Purpose :Implementation of BH1750FVI module.( Digital light intensity module GY-30)
Version :0.01 2014/02/03 12:00(OK)
Complier:Keil 8051 C complier V9.01
MCU :STC12C5A60S2
Author :yangrui
QQ :279729201
Email :[email protected]
Modification:
=================
2014/04/29 14:35
Reason:
1. The working mode adopted here is Continuously H-Resolution Mode, Continuous high resolution mode .
besides , There are other modes of operation , About 6 The operating modes in are summarized as follows :
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-Resolution Mode ;(at 4LX resolution,Measurement Time is typically 16ms)
One Time H-Resolution Mode ;(at 1LX resolution,Measurement Time is typically 120ms,
It is 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 is 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 is autionmatically set to power dowm mode after measurement,
so if want to read ,you shoule set "power on")
2.BH1750FVI Write data and other data in the internal address IIC The device is slightly different , other IIC Device after transmitting device address , There is also a need to transmit the inside of the device
Address , however BH1750FVI When writing data , It's a command , So no internal device address is required , So the function is directly named
BH1750FVIWriteCommand(...), The code here is from my at24c02.c Evolved , If there are many in the project IIC device ,
Want to put IIC The communication protocol is encapsulated , Be sure to pay attention to this detail .
=================
***************************************************************************/
#include <intrins.h>
#include "BH1750FVI.h"
/* The external interface function is in BH1750FVI.h In a statement */
/***************** Internal 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 :delay5usForBH1750FVI
//Input :N/A
//Output :N/A
//Return :N/A
//Description :N/A
//Note : from STC-ISP V6.69 The software is for the corresponding MCU Generate , if MCU Different
// It is best to modify this function as well .
//******************************************************/
//static void delay5usForBH1750FVI(void) //@11.0592MHz
//{
// unsigned char i;
//
// _nop_();
// _nop_();
// _nop_();
// i = 10;
// while (--i);
//}
/******************************************************
Function :delay180msForBH1750FVI
Input :N/A
Output :N/A
Return :N/A
Description :N/A
Note : from STC-ISP V6.69 The software is for the corresponding MCU Generate , if MCU Different
It is best to modify this function as well .
******************************************************/
static void delay180msForBH1750FVI(void) //@11.0592MHz
{
unsigned char i, j, k;
i = 8;
j = 145;
k = 99;
do
{
do
{
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 BH1750FVIStopSignal(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
Description : Release the bus
Note :N/A
******************************************************/
static void BH1750FVIFreeBus(void)
{
//free bus
BH1750FVI_sclk_bit = HIGH_LEVEL ;
BH1750FVI_sda_bit = HIGH_LEVEL ;
}
/******************************************************
Function :BH1750FVIAcknowledge
Input :N/A
Output :N/A
Return :N/A
Description : The host is waiting BH1750FVI The reply
Note :N/A
******************************************************/
static void BH1750FVIAcknowledge(void)
{
UB8 i=0 ;
BH1750FVI_sclk_bit = LOW_LEVEL ;
//_nop_() ;
BH1750FVI_sclk_bit = HIGH_LEVEL ;
//_nop_() ;
while((BH1750FVI_sda_bit) && (i<250))
{
i++;// temporary , See commissioning for details
// After testing , there 250 Big enough
}
BH1750FVI_sclk_bit = LOW_LEVEL ;
}
/******************************************************
Function :mcuAcknowledge
Input :N/A
Output :N/A
Return :N/A
Description : The host receives... As a receiver BH1750FVI After the data , Send a reply 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 :dat
Output :N/A
Return :N/A
Description :write byte-data to BH1750FVI
Note :N/A
******************************************************/
static void BH1750FVIWriteByte(UB8 dat)
{
UB8 i;
for (i=0; i<8; i++)
{
BH1750FVI_sclk_bit = LOW_LEVEL ;
//_nop_();
// Method 1
BH1750FVI_sda_bit = (bit)(dat & (0x80>>i)) ;
// Method 2
//temp <<= 1 ;
//BH1750FVI_sda_bit = CY ;
//delay5usForBH1750FVI();/* It can be shielded after testing */
//_nop_();
BH1750FVI_sclk_bit = HIGH_LEVEL ;
//_nop_();
}
}
/******************************************************
Function :BH1750FVIReadByte
Input :N/A
Output :N/A
Return :byte-data from BH1750FVI
Description : Read byte data
Note :N/A
******************************************************/
static UB8 BH1750FVIReadByte()
{
UB8 i ;
UB8 dataCode = 0x00 ;
//Ready
/*Data on sda pin may change 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 commands
Note :N/A
******************************************************/
void BH1750FVIWriteCommand(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 the next byte is read , The host replaces the reply signal with the end signal */
BH1750FVIStopSignal();
BH1750FVIFreeBus() ;
temp = ((UW16)(msb)) <<8 | lsb ;
temp = (float)temp/1.2 ;
/* There are implicit type conversions :float -- > unsigned int */
return temp ;
}
/******************************************************
Function :BH1750FVIPowerOn
Input :N/A
Output :N/A
Return :N/A
Description :" Access to electricity " command
Note : about " Access to electricity " command , If you use "One Time...." Pattern ,
After converting the result , The hardware will automatically switch to POWER DOWM Pattern ,
Therefore, it is necessary to reuse the power on command , In the program , I use the
The pattern is "Continuously H-Resolution Mode", therefore
It will not automatically switch to power down mode , You only need to initialize , call
This function . If you use other modes , You may also need to call... Elsewhere .
And other functions should 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
Return :N/A
Description : initialization
Note :N/A
******************************************************/
void BH1750FVIInit()
{
BH1750FVIPowerOn();
/* Mode selection is 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__*/边栏推荐
- TEMPEST HDMI泄漏接收 1
- 高数基础_函数的奇偶性
- Full range of isolator chips with integrated isolated power supply
- TEMPEST HDMI泄漏接收 2
- Harmony美食菜单界面
- How do I create a guid in excel- How to create a GUID in Excel?
- [batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
- MySQL face Scripture eight part essay
- How comfortable it is to use Taijiquan to talk about distributed theory!
- Reading sensor data with GPIO analog SPI interface
猜你喜欢

Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing

Chuantu microelectronics ca-if1051 can-fd transceiver

Classic paper in the field of character recognition: aster

Evolution of Alibaba e-commerce architecture

【批處理DOS-CMD命令-匯總和小結】-外部命令-cmd下載命令、抓包命令(wget)

Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
![[batch dos-cmd command - summary and summary] - external command -cmd download command and packet capture command (WGet)](/img/00/5a5b081b78ad6a6c1c3a3c847dd315.png)
[batch dos-cmd command - summary and summary] - external command -cmd download command and packet capture command (WGet)

Authentique Photoshop 2022 expérience d'achat partage

smartBugs安装小问题总结

Explain distributed raft with dynamic diagram
随机推荐
Loopholes in the missed scanning system of Lvmeng and its repair scheme
函数模板_类模板
shell小技巧(一百三十四)简单的键盘输入记录器
TEMPEST HDMI泄漏接收 2
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
How is the network connected?
Ns32f103c8t6 can perfectly replace stm32f103c8t6
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
[Batch dos - cmd Command - Summary and Summary] - External Command - cmd Download Command, wget Command
高数基础_函数的奇偶性
Alphassl wildcard certificate for one month
Ppt template of small fresh open class education courseware
Escape analysis of 982 golang
关于硬件问题造成的MCU死机,过来人简单的谈一谈
Harmony food menu interface
Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
[Introduction aux uvm== > Episode 9] ~ modèle de registre, intégration du modèle de registre, méthode conventionnelle du modèle de registre, scénario d'application du modèle de registre
从感知机到Transformer,一文概述深度学习简史
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection