当前位置:网站首页>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
边栏推荐
- Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
- 申请代码签名证书时如何选择合适的证书品牌?
- 想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
- How to make a second clip of our media video without infringement
- How to protect user privacy without password authentication?
- [learning notes] stage test 1
- How to solve the problem of garbled code when installing dependency through NPM or yarn
- Two policemen were shot dead in a "safety accident" in Philadelphia, USA
- Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
猜你喜欢
【NVMe2.0b 14-9】NVMe SR-IOV
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
FR练习题目---综合题
Principle and performance analysis of lepton lossless compression
APR protocol and defense
黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
Drive brushless DC motor based on Ti drv10970
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
两个BI开发,3000多张报表?如何做的到?
【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !
随机推荐
maxcompute有没有能查询 表当前存储容量的大小(kb) 的sql?
CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
STM32+BH1750光敏传感器获取光照强度
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
Countermeasures of enterprise supply chain management system in UCA Era
做自媒體視頻二次剪輯,怎樣剪輯不算侵權
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
外盘入金都不是对公转吗,那怎么保障安全?
LeetCode_ 3 (longest substring without repeated characters)
危机重重下的企业发展,数字化转型到底是不是企业未来救星
Mongdb learning notes
Sharing the 12 most commonly used regular expressions can solve most of your problems
Handwriting promise and async await
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
be careful! Software supply chain security challenges continue to escalate
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
两个BI开发,3000多张报表?如何做的到?
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Long list optimized virtual scrolling
Isn't it right to put money into the external market? How can we ensure safety?