当前位置:网站首页>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 .
Two 、BH1750 Introduce
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
边栏推荐
- Share 20 strange JS expressions and see how many correct answers you can get
- 【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
- Two Bi development, more than 3000 reports? How to do it?
- Principle and performance analysis of lepton lossless compression
- Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
- Strong connection component
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
- Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- Loop invariant
猜你喜欢
CPU design related notes
Install and configure Jenkins
Under the crisis of enterprise development, is digital transformation the future savior of enterprises
基于TI DRV10970驱动直流无刷电机
Niuke: intercepting missiles
Sharing the 12 most commonly used regular expressions can solve most of your problems
计算中间件 Apache Linkis参数解读
FR练习题目---简单题
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
随机推荐
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
【招聘岗位】基础设施软件开发人员
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
Sharing the 12 most commonly used regular expressions can solve most of your problems
Implement a blog system -- using template engine technology
CPU设计相关笔记
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
CPU design related notes
【C 题集】of Ⅷ
Handwriting promise and async await
Structure - C language
The function of qualifier in C language
[C question set] of Ⅷ
Time to calculate cron expression based on cronsequencegenerator
想进阿里必须啃透的12道MySQL面试题
Is it OK to open the securities account on the excavation finance? Is it safe?
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
Install and configure Jenkins