当前位置:网站首页>Stm32+bh1750 photosensitive sensor obtains light intensity

Stm32+bh1750 photosensitive sensor obtains light intensity

2022-07-05 14:45:00 Hua Weiyun

One 、 Introduction to the environment

MCU: STM32F103ZET6

Photosensitive sensors : BH1750 Digital sensor (IIC Interface )

Development software : Keil5

Code instructions : Use IIC Analog timing drive , Easy to migrate to other platforms , The collected illumination is relatively sensitive . The range of the return value of the synthetic illuminance is 0~255. 0 It means all black 255 It means very bright .

Actually measured : The return value of the status illuminated by the mobile phone flash is 245 about , The return value of the hand covering state is 10 about .

image.png

image.png

Two 、BH1750 Introduce

image.png
image.png
image.png

3、 ... and 、 Core code

BH1750 explain : ADDR Pin ground , The address is 0x46

3.1 iic.c

#include "iic.h"/* The functionality :IIC Interface initialization hardware connection :SDA:PB7SCL:PB6*/void IIC_Init(void){	RCC->APB2ENR|=1<<3;//PB	GPIOB->CRL&=0x00FFFFFF;	GPIOB->CRL|=0x33000000;	GPIOB->ODR|=0x3<<6;}/* The functionality :IIC Bus start signal */void IIC_Start(void){		IIC_SDA_OUTMODE(); // initialization SDA For output mode 		IIC_SDA_OUT=1; 		 // The data cable is pulled high 		IIC_SCL=1;     		 // Pull the clock cable high 		DelayUs(4);        // Level holding time 		IIC_SDA_OUT=0; 		 // Pull down the data cable 		DelayUs(4);        // Level holding time 		IIC_SCL=0;     		 // Pull down the clock line }/* The functionality :IIC Bus stop signal */void IIC_Stop(void){		IIC_SDA_OUTMODE(); // initialization SDA For output mode 		IIC_SDA_OUT=0; 		 // Pull down the data cable 		IIC_SCL=0;     		 // Pull down the clock line 		DelayUs(4);        // Level holding time 		IIC_SCL=1;     		 // Pull the clock cable high 		DelayUs(4);        // Level holding time 		IIC_SDA_OUT=1; 		 // The data cable is pulled high }/* The functionality : Get the reply signal and return   return   value :1 It means failure ,0 It means success */u8 IIC_GetACK(void){		u8 cnt=0;		IIC_SDA_INPUTMODE();// initialization SDA Is the input mode 		IIC_SDA_OUT=1; 		  // Data online pull 	  DelayUs(2);         // Level holding time 		IIC_SCL=0;     		  // Pull down the clock line , Tell the slave , The host needs data 		DelayUs(2);         // Level holding time , Wait for the slave to send data 	  IIC_SCL=1;     		  // Pull the clock cable high , Tell the slave , The host now starts reading data 		while(IIC_SDA_IN)   // Waiting for the slave to respond 		{				cnt++;				if(cnt>250)return 1;		}		IIC_SCL=0;     		  // Pull down the clock line , Tell the slave , The host needs data 		return 0;}/* The functionality : The master sends the response signal function parameter to the slave :0 To answer ,1 It means no response */void IIC_SendACK(u8 stat){		IIC_SDA_OUTMODE(); // initialization SDA For output mode 		IIC_SCL=0;     		 // Pull down the clock line , Tell the slave , The host needs to send data 		if(stat)IIC_SDA_OUT=1; // The data cable is pulled high , Send non reply signal 		else IIC_SDA_OUT=0; 	 // Pull down the data cable , Send reply signal 		DelayUs(2);            // Level holding time , Wait for the clock line to stabilize 		IIC_SCL=1;     		     // Pull the clock cable high , Tell the slave , The host data has been sent 		DelayUs(2);            // Level holding time , Wait for the slave to receive data 		IIC_SCL=0;     		  	 // Pull down the clock line , Tell the slave , The host needs data }/* The functionality :IIC send out 1 Byte data function parameters : Data to be sent */void IIC_WriteOneByteData(u8 data){		u8 i;		IIC_SDA_OUTMODE(); // initialization SDA For output mode 		IIC_SCL=0;     		 // Pull down the clock line , Tell the slave , The host needs to send data 		for(i=0;i<8;i++)		{				if(data&0x80)IIC_SDA_OUT=1; // The data cable is pulled high , send out 1				else IIC_SDA_OUT=0; 	 // Pull down the data cable , send out 0				IIC_SCL=1;     		     // Pull the clock cable high , Tell the slave , The host data has been sent 				DelayUs(2);            // Level holding time , Wait for the slave to receive data 				IIC_SCL=0;     		 		 // Pull down the clock line , Tell the slave , The host needs to send data 				DelayUs(2);            // Level holding time , Wait for the clock line to stabilize 				data<<=1;              // First mover 		}}/* The functionality :IIC receive 1 Bytes of data are returned   return   value : Data received */u8 IIC_ReadOneByteData(void){		u8 i,data;		IIC_SDA_INPUTMODE();// initialization SDA Is the input mode 	  for(i=0;i<8;i++)	  {			 	IIC_SCL=0;     		  // Pull down the clock line , Tell the slave , The host needs data 				DelayUs(2);         // Level holding time , Wait for the slave to send data 				IIC_SCL=1;     		  // Pull the clock cable high , Tell the slave , The host is now reading data 				data<<=1;           				if(IIC_SDA_IN)data|=0x01;				DelayUs(2);         // Level holding time , Wait for the clock line to stabilize 	  }		IIC_SCL=0;     		  		// Pull down the clock line , Tell the slave , The host needs data  ( Must be pulled down , Otherwise, it will be recognized as a stop signal )		return data;}

3.2 iic.h

#ifndef _IIC_H#define _IIC_H#include "stm32f10x.h"#include "sys.h"#include "delay.h"#define IIC_SDA_OUTMODE() {GPIOB->CRL&=0x0FFFFFFF;GPIOB->CRL|=0x30000000;}#define IIC_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)  // 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

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

3.4 BH1750.c

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

3.5 main.c

 If you need a complete project, you can download it here :https://download.csdn.net/download/xiaolong1126626497/18500653#include "stm32f10x.h"#include "led.h"#include "delay.h"#include "key.h"#include "usart.h"#include "at24c02.h"#include "bh1750.h"int main(){	u8 val;	LED_Init();	BEEP_Init();	KeyInit();  USARTx_Init(USART1,72,115200);  IIC_Init();  	while(1)	{		val=KeyScan();		if(val)		{            val=Read_BH1750_Data();           printf(" Light intensity =%d\r\n",val);           printf(" Technical communication QQ Group =%s\r\n","763218897");// BEEP=!BEEP;			 LED0=!LED0;			 LED1=!LED1;		}	}}

3.6 Run the renderings

image.png

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051436339718.html