[STM32U5]【NUCLEO

您所在的位置:网站首页 i2c传感器 [STM32U5]【NUCLEO

[STM32U5]【NUCLEO

2024-06-17 18:28:24| 来源: 网络整理| 查看: 265

I2C外设使用以光照传感器信息读取 I2C接口是很多传感器使用的通讯接口,在低功耗应用中,传感器的使用也是很普遍的。本文使用开发板的I2C接口来读取ISL29035光照传感器的数据。1.硬件准备 通过查看原理图可知,PB8、PB9为Arduino接口上的I2C引脚。本文使用I2C接口与ISL29035光照传感器通讯,获得环境中的光照强度信息。 本文中使用到的传感器的原理图如图所示。  

与传感器通讯的的I2C接口选择Arduino接口上的PB8和PB9,传感器模块和开发板的实物连接图如下图所示。  

2.程序编写 首先,在STM32 CubeIDE中创建开发板的工程,选择根据开发板创建工程, 在CubeMX界面中会自动初始化开发板上的部分外设,比如串口、调试口、LED等,本文通过I2C1控制传感器,使用的PB8和PB9,选择对应管脚的复用功能,并在I2C的配置界面选择相应的I2C通讯参数,这里使用的参数是默认的配置。 完成上述操作后,保存配置并生成相应的外设初始化代码。 传感器ISL29035的功能框图如下图所示。  

通过I2C向指令寄存器中写值,配置传感器的参数和控制传感器采样;从数据寄存器中读取采样的结果,用于计算光照值。驱动传感器工作需要实现以下几个函数。

复制

uint8_t DigitalLightISL29035_readRegister(uint8_t reg_address); void DigitalLightISL29035_writeRegister( int reg_address, uint8_t val) ; int DigitalLightISL29035_init(void) ; uint32_t DigitalLightISL29035_readIRLux(void);

其中的读取和写入寄存器的函数是驱动传感器工作的基础函数。这两个函数的具体实现如下。

复制

void DigitalLightISL29035_writeRegister( int reg_address, uint8_t val) {     uint8_t ii[2]={0x00,0x00};     ii[0] = reg_address;     ii[1] = val;         if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)ISL29035_I2C_ADDRESS_W, ii, 0x02,10000) != HAL_OK)         {         /* Error_Handler() function is called when error occurs. */         Error_Handler();         }         /*##- Wait for the end of the transfer #################################*/         /*  Before starting a new communication transfer, you need to check the current           state of the peripheral; if it�s busy you need to wait for the end of current           transfer before starting a new one.           For simplicity reasons, this example is just waiting till the end of the           transfer, but application may perform other tasks while transfer operation           is ongoing. */         while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)         {         } }

复制

uint8_t DigitalLightISL29035_readRegister(uint8_t reg_address) {     uint8_t value;         if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)ISL29035_I2C_ADDRESS_W, ®_address, 0x01,10000) != HAL_OK)         {         /* Error_Handler() function is called when error occurs. */         Error_Handler();         }         /*##- Wait for the end of the transfer #################################*/         /*  Before starting a new communication transfer, you need to check the current           state of the peripheral; if it�s busy you need to wait for the end of current           transfer before starting a new one.           For simplicity reasons, this example is just waiting till the end of the           transfer, but application may perform other tasks while transfer operation           is ongoing. */         while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)         {         }     /* Read data back from the I2C slave */         if (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)ISL29035_I2C_ADDRESS_R, (uint8_t *)&value, 0x01,10000) != HAL_OK)         {         /* Error_Handler() function is called when error occurs. */         Error_Handler();         }         /*##- Wait for the end of the transfer #################################*/         /*  Before starting a new communication transfer, you need to check the current           state of the peripheral; if it�s busy you need to wait for the end of current           transfer before starting a new one.           For simplicity reasons, this example is just waiting till the end of the           transfer, but application may perform other tasks while transfer operation           is ongoing. */         while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)         {         }     return value; }

本文采用传感器的单次转换模式来获取光照强度的信息,传感器在该模式下会接收到单次转换指令后转换一次环境中的光照值,MCU通过I2C读取数据寄存器中缓存的光照强度转换值,对其根据一定规则换算,可以得到光照强度值。 通过阅读传感器的数据手册可知传感器的寄存的分布如下图  

通过向COMMAND-I和COMMAND-II指令寄存器中写入控制字,从而实现控制传感器的采样精度、采样时间、工作模式等。从DATA数据寄存器中读取转换完成的数据。  

值得注意的一点是在数据手册中,ID寄存器的第7位需要在开始时确定其处于复位的状态。 配合数据手册中的信息,传感器的初始化函数如下:

复制

int DigitalLightISL29035_init(void) {     uint8_t reg = 0;     reg=   DigitalLightISL29035_readRegister( CHIP_ID);//CHIP_ID->0x0f     //Serial.println(reg, HEX);     uint8_t chip_id = (reg >> 3) & 0x7;     if (chip_id != 0x5) {         return -1;     }     //清除BOUT位     DigitalLightISL29035_writeRegister(CHIP_ID, reg & 0x7f);//CHIP_ID->0x0f     //确保芯片处于停止模式     DigitalLightISL29035_writeRegister( COMMAND_I, 0);//COMMAND_I->0x00     //设置分辨率     DigitalLightISL29035_writeRegister(COMMAND_II, full_scale_lux_range | (integration_time 0x01     //设置为单次模式     DigitalLightISL29035_writeRegister( COMMAND_I, OPMODE_ALS_ONCE);//COMMAND_I->0x00     return 0; }

完成初始化后,编写启动传感器单次转换并读取传感器的数据转换为光照强度数据。函数如下:

复制

uint32_t DigitalLightISL29035_readIRLux(void) {     uint16_t data = 0;     uint8_t l, h;     //设置为单次模式     DigitalLightISL29035_writeRegister( COMMAND_I, OPMODE_ALS_ONCE);     //等待时间     if(integration_time==0)     {             HAL_Delay(105);     }     else if(integration_time==1 || integration_time==2)     {             HAL_Delay(7);     }     else if(integration_time==3)     {             HAL_Delay(1);     }     l=DigitalLightISL29035_readRegister(DATA_L);//DATA_L->0x02     h=DigitalLightISL29035_readRegister(DATA_H);//DATA_H->0x03     data=(h


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭