I. Introduction to the environment

MCU:  ****STM32F103ZET6

Photosensitive sensor: BH1750 digital sensor (IIC interface)

Development software: Keil5

The use of IIC simulation timing drive, easy to transplant to other platforms, the collection of light is more sensitive. The resultant illuminance return value ranges from 0 to 255. 0 means all black and 255 means very bright.

Test: the return value of the state illuminated by the mobile phone flash is about 245, and the return value of the state covered by the hand is about 10.

Complete engineering code download: download.csdn.net/download/xi…

Ii. Introduction to BH1750

Core code

BH1750 The ADDR pin is grounded and the address is 0x46

3.1 iic. C

#include "iic.h"

/* IIC interface initialization hardware connection: SDA: PB7 SCL: PB6 */
void IIC_Init(void)
{
	RCC->APB2ENR|=1<<3;//PB
	GPIOB->CRL&=0x00FFFFFF;
	GPIOB->CRL|=0x33000000;
	GPIOB->ODR|=0x3<<6;
}

IIC bus start signal */
void IIC_Start(void)
{
		IIC_SDA_OUTMODE(a);// Initialize SDA to output mode
		IIC_SDA_OUT=1; 		 // Data cable is pulled up
		IIC_SCL=1;     		 // The clock line is pulled up
		DelayUs(4);        // Level hold time
		IIC_SDA_OUT=0; 		 // Data line pulled down
		DelayUs(4);        // Level hold time
		IIC_SCL=0;     		 // The clock line is pulled down
}


/* IIC bus stop signal */
void IIC_Stop(void)
{
		IIC_SDA_OUTMODE(a);// Initialize SDA to output mode
		IIC_SDA_OUT=0; 		 // Data line pulled down
		IIC_SCL=0;     		 // The clock line is pulled down
		DelayUs(4);        // Level hold time
		IIC_SCL=1;     		 // The clock line is pulled up
		DelayUs(4);        // Level hold time
		IIC_SDA_OUT=1; 		 // Data cable is pulled up
}

Return value: 1 indicates failure, 0 indicates success */
u8 IIC_GetACK(void)
{
		u8 cnt=0;
		IIC_SDA_INPUTMODE(a);// Initialize SDA to input mode
		IIC_SDA_OUT=1; 		  // Data cable pull up
	  DelayUs(2);         // Level hold time
		IIC_SCL=0;     		  // The clock line is pulled down to tell the slave that the master needs data
		DelayUs(2);         // Level hold time, waiting for slave machine to send data
	  IIC_SCL=1;     		  // The clock line is pulled up to tell the slave that the master is now reading data
		while(IIC_SDA_IN)   // Wait for an answer signal from the slave machine
		{
				cnt++;
				if(cnt>250)return 1;
		}
		IIC_SCL=0;     		  // The clock line is pulled down to tell the slave that the master needs data
		return 0;
}

/* Function Function: the host sends an answer signal to the slave. Parameter: 0 indicates an answer, and 1 indicates a non-answer */
void IIC_SendACK(u8 stat)
{
		IIC_SDA_OUTMODE(a);// Initialize SDA to output mode
		IIC_SCL=0;     		 // The clock line is pulled down to tell the slave that the master needs to send data
		if(stat)IIC_SDA_OUT=1; // The data line is pulled up to send a non-reply signal
		else IIC_SDA_OUT=0; 	 // The data line is pulled down to send a reply signal
		DelayUs(2);            // Level hold time, waiting for the clock line to stabilize
		IIC_SCL=1;     		     // The clock line is pulled up to tell the slave machine that the host data has been sent
		DelayUs(2);            // Level hold time, waiting for slave machine to receive data
		IIC_SCL=0;     		  	 // The clock line is pulled down to tell the slave that the master needs data
}


/* IIC sends 1 byte of data. Parameter: data to be sent */
void IIC_WriteOneByteData(u8 data)
{
		u8 i;
		IIC_SDA_OUTMODE(a);// Initialize SDA to output mode
		IIC_SCL=0;     		 // The clock line is pulled down to tell the slave that the master needs to send data
		for(i=0; i<8; i++) {if(data&0x80)IIC_SDA_OUT=1; // Data cable is pulled up, send 1
				else IIC_SDA_OUT=0; 	 // Data line is pulled down to send 0
				IIC_SCL=1;     		     // The clock line is pulled up to tell the slave machine that the host data has been sent
				DelayUs(2);            // Level hold time, waiting for slave machine to receive data
				IIC_SCL=0;     		 		 // The clock line is pulled down to tell the slave that the master needs to send data
				DelayUs(2);            // Level hold time, waiting for the clock line to stabilize
				data<<=1;              // Start high}}/* The IIC function receives 1 byte of data
u8 IIC_ReadOneByteData(void)
{
		u8 i,data;
		IIC_SDA_INPUTMODE(a);// Initialize SDA to input mode
	  for(i=0; i<8; i++) { IIC_SCL=0;     		  // The clock line is pulled down to tell the slave that the master needs data
				DelayUs(2);         // Level hold time, waiting for slave machine to send data
				IIC_SCL=1;     		  // The clock line is pulled up to tell the slave that the master is now reading data
				data<<=1;           
				if(IIC_SDA_IN)data|=0x01;
				DelayUs(2);         // Level hold time, waiting for the clock line to stabilize
	  }
		IIC_SCL=0;     		  		// The clock line is pulled down to tell the slave that the master needs data (it must be pulled down, otherwise it will be recognized as a stop signal)
		return data;
}
Copy the code

3.2 iic. H

#ifndef _IIC_H
#define _IIC_H
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#defineIIC_SDA_OUTMODE() {GPIOB->CRL&=0x0FFFFFFF; GPIOB->CRL|=0x30000000; }
#defineIIC_SDA_INPUTMODE() {GPIOB->CRL&=0x0FFFFFFF; GPIOB->CRL|=0x80000000; }
#define IIC_SDA_OUT PBout(7)  // Data line output
#define IIC_SDA_IN PBin(7)  // Data line input
#define IIC_SCL PBout(6)  / / the clock line

void IIC_Init(void);
void IIC_Start(void);
void IIC_Stop(void);
u8 IIC_GetACK(void);
void IIC_SendACK(u8 stat);
void IIC_WriteOneByteData(u8 data);
u8 IIC_ReadOneByteData(void);
#endif
Copy the code

3.3 BH1750. H

#ifndef _BH1750_H
#define _BH1750_H
#include "delay.h"
#include "iic.h"
#include "usart.h"
u8 Read_BH1750_Data(void);
#endif
Copy the code

3.4 BH1750. C

#include "bh1750.h"
u8 Read_BH1750_Data(a)
{
    unsigned char t0;
    unsigned char t1;
    unsigned char t;
    u8 r_s=0;
    IIC_Start(a);// Send the start signal
    IIC_WriteOneByteData(0x46);
    r_s=IIC_GetACK(a);// Get the reply
    if(r_s)printf("error:1\r\n");
    IIC_WriteOneByteData(0x01);
    r_s=IIC_GetACK(a);// Get the reply
     if(r_s)printf("error:2\r\n");
    IIC_Stop(a);// Stop signal
    
    IIC_Start(a);// Send the start signal
    IIC_WriteOneByteData(0x46);
    r_s=IIC_GetACK(a);// Get the reply
    if(r_s)printf("error:3\r\n");
    IIC_WriteOneByteData(0x01);
    r_s=IIC_GetACK(a);// Get the reply
    if(r_s)printf("error:4\r\n");
    IIC_Stop(a);// Stop signal
    
    IIC_Start(a);// Send the start signal
    IIC_WriteOneByteData(0x46);
    r_s=IIC_GetACK(a);// Get the reply
    if(r_s)printf("error:5\r\n");
    IIC_WriteOneByteData(0x10);
    r_s=IIC_GetACK(a);// Get the reply
    if(r_s)printf("error:6\r\n");
    IIC_Stop(a);// Stop signal
    
    DelayMs(300); / / wait for
    
    IIC_Start(a);// Send the start signal
    IIC_WriteOneByteData(0x47);
    r_s=IIC_GetACK(a);// Get the reply
    if(r_s)printf("error:7\r\n");
    
    t0=IIC_ReadOneByteData(a);// Receive data
    IIC_SendACK(0); // Send a reply signal
    t1=IIC_ReadOneByteData(a);// Receive data
    IIC_SendACK(1); // Send a non-reply signal
    IIC_Stop(a);// Stop signal
    
     t=(((t0<<8)|t1)/1.2);
     return t;  
}
Copy the code

3.5 the main. C

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include "at24c02.h"
#include "bh1750.h"

int main(a)
{
	u8 val;
	LED_Init(a);BEEP_Init(a);KeyInit(a);USARTx_Init(USART1,72.115200);
  IIC_Init(a);while(1)
	{
		val=KeyScan(a);if(val)
		{
            val=Read_BH1750_Data(a);printf("Light intensity =%d\r\n",val);
// BEEP=! BEEP;
			 LED0=!LED0;
			 LED1=!LED1;
		}
	}
}
Copy the code

3.6 Operation effect drawing