当前位置:网站首页>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
边栏推荐
- Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
- CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
- Principle and performance analysis of lepton lossless compression
- PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
- Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- 安装配置Jenkins
- Thymeleaf 使用后台自定义工具类处理文本
- LeetCode_ 3 (longest substring without repeated characters)
- 【華為機試真題詳解】歡樂的周末
猜你喜欢
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
How to protect user privacy without password authentication?
Thymeleaf th:with use of local variables
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
微帧科技荣获全球云计算大会“云鼎奖”!
leetcode:881. lifeboat
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
CPU design related notes
随机推荐
[learning notes] connectivity and circuit of graph
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
安装配置Jenkins
How can non-technical departments participate in Devops?
PostgreSQL 13 installation
【华为机试真题详解】欢乐的周末
一键更改多个文件名字
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
用 Go 跑的更快:使用 Golang 为机器学习服务
STM32+BH1750光敏传感器获取光照强度
计算中间件 Apache Linkis参数解读
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
【学习笔记】图的连通性与回路
面试突击62:group by 有哪些注意事项?
Strong connection component
GPS原始坐标转百度地图坐标(纯C代码)
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
Thymeleaf 常用函数