当前位置:网站首页>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
边栏推荐
- 做自媒體視頻二次剪輯,怎樣剪輯不算侵權
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- Thymeleaf common functions
- ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
- STM32+BH1750光敏传感器获取光照强度
- anaconda使用中科大源
- Implement a blog system -- using template engine technology
- Using tensorboard to visualize the training process in pytoch
- Change multiple file names with one click
- 家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
猜你喜欢
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
【学习笔记】阶段测试1
Interview shock 62: what are the precautions for group by?
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
FR练习题目---简单题
Change multiple file names with one click
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
两个BI开发,3000多张报表?如何做的到?
安装配置Jenkins
随机推荐
anaconda使用中科大源
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
【招聘岗位】基础设施软件开发人员
Structure - C language
开挖财上的证券账户可以吗?安全吗?
mysql8.0JSON_ Instructions for using contains
easyOCR 字符识别
想问下大家伙,有无是从腾讯云MYSQL同步到其他地方的呀?腾讯云MySQL存到COS上的binlog
Longest common subsequence dynamic programming
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
be careful! Software supply chain security challenges continue to escalate
【招聘岗位】软件工程师(全栈)- 公共安全方向
Time to calculate cron expression based on cronsequencegenerator
Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!