光感芯片veml7700的使用
1、veml有7个寄存器,每个十六位,见图。
00是config寄存器,
01 02 是中断设置的阈值
03是节能模式的设置
04 是得到的光的亮度值
05是得到的data of whole WHITE
06是中断设置值。
2、我们只测试得到光的亮度值,所以veml寄存器设置如下:
设置gain,integration time ,power save mode ,interrupt ,power on.
3、数据的换算:
可以用代码中公式。
4、代码
#include "gd32f30x.h"
#include "veml7700.h"
#include "i2c.h"
#include <stdio.h>
void veml7700_write(uint8_t reg_addr,uint8_t *p_buf)
{
#if I2C1_MODE == I2C1_MODE_SW
swi2c_device_write_data(VEML_SLAVE_ADDR,®_addr,1,p_buf,2);
#endif
}
void veml7700_read(uint8_t reg_addr,uint8_t *p_buf)
{
#if I2C1_MODE == I2C1_MODE_SW
swi2c_device_read_data(VEML_SLAVE_ADDR,®_addr,1,p_buf,2);
#endif
}
static int get_veml7700_default_value(uint16_t *default_value)
{
uint8_t rx_data[2] = {0};
veml7700_read(0x00,rx_data);
*default_value = ((rx_data[1] << 8) | rx_data[0]);
return 0;
}
static uint16_t veml7700_get_light_sensor_value(uint8_t command,uint16_t mask,uint8_t shift)
{
uint8_t rx_data[2] = {0};
uint16_t r_value = 0;
veml7700_read(command,rx_data);
r_value = ((rx_data[1] << 8) | rx_data[0]);
return (r_value & mask) >> shift;
}
static void veml7700_set_light_sensor_value(uint8_t value,uint8_t command,uint16_t mask,uint8_t shift)
{
uint8_t rx_data[2] = {0};
uint8_t tx_data[2] = {0};
uint16_t r_value = 0;
uint16_t tmp_value = 0;
veml7700_read(command,rx_data);
r_value = ((rx_data[1] << 8) | rx_data[0]);
tmp_value = r_value & ~(mask);
tmp_value |= ((value << shift) & mask);
tx_data[0] = tmp_value & 0xff;
tx_data[1] = ((tmp_value >> 8) & 0xff);
veml7700_write(command,tx_data);
}
static uint16_t get_power_enable()
{
return veml7700_get_light_sensor_value(COMMAND_ALS_SD,ALS_SD_MASK,ALS_SD_SHIFT);
}
static void set_power_enable(int enable)
{
return veml7700_set_light_sensor_value(enable,COMMAND_ALS_SD,ALS_SD_MASK,ALS_SD_SHIFT);
}
static uint16_t get_gain_value()
{
return veml7700_get_light_sensor_value(COMMAND_ALS_GAIN,ALS_GAIN_MASK,ALS_GAIN_SHIFT);
}
static void set_gain_value(uint16_t gain)
{
veml7700_set_light_sensor_value(gain,COMMAND_ALS_GAIN,ALS_GAIN_MASK,ALS_GAIN_SHIFT);
}
static uint16_t get_interrupt_enable()
{
return veml7700_get_light_sensor_value(COMMAND_ALS_INT_EN,ALS_INT_EN_MASK,ALS_INT_EN_SHIFT);
}
static void set_interrupt_enable(uint16_t enable)
{
veml7700_set_light_sensor_value(enable,COMMAND_ALS_INT_EN,ALS_INT_EN_MASK,ALS_INT_EN_SHIFT);
}
static int get_integration_time()
{
return veml7700_get_light_sensor_value(COMMAND_ALS_IT,ALS_IT_MASK,ALS_IT_SHIFT);
}
static void set_integration_time(int time)
{
veml7700_set_light_sensor_value(time,COMMAND_ALS_IT,ALS_IT_MASK,ALS_IT_SHIFT);
}
static int get_power_saving_mode()
{
return veml7700_get_light_sensor_value(COMMAND_PSM_EN,PSM_EN_MASK,PSM_EN_SHIFT);
}
static void set_power_saving_mode(int mode)
{
veml7700_set_light_sensor_value(mode,COMMAND_PSM_EN,PSM_EN_MASK,PSM_EN_SHIFT);
}
static uint16_t get_als_reg_data()
{
uint8_t rx_data[2] = {0};
uint16_t r_value = 0;
veml7700_read(COMMAND_ALS_DATA,rx_data);
r_value = ((rx_data[1] << 8) | rx_data[0]);
return r_value;
}
static int normalize_resolution(int value)
{
switch (get_gain_value())
{
case ALS_GAIN_X2:
value /= 2.0;
break;
case ALS_GAIN_d4:
value *= 4;
break;
case ALS_GAIN_d8:
value *= 8;
break;
case ALS_GAIN_X1:
default:break;
}
switch (get_integration_time()){
case ALS_INTEGRATION_25ms:
value *= 4;
break;
case ALS_INTEGRATION_50ms:
value *= 2;
break;
case ALS_INTEGRATION_200ms:
value /= 2.0;
break;
case ALS_INTEGRATION_400ms:
value /= 4.0;
break;
case ALS_INTEGRATION_800ms:
value /= 8.0;
break;
case ALS_INTEGRATION_100ms:
default:break;
}
return value;
}
int get_lux_data(void)
{
return normalize_resolution(get_als_reg_data() * 0.0576);
}
void veml7700_init(void)
{
uint16_t default_value;
get_veml7700_default_value(&default_value);
if (default_value == 0x0001)
{
printf("Have Fined Veml7700!\r\n");
}
else
{
printf("Veml7700 not exsit %x\r\n",default_value);
//return ;
}
set_power_enable(ALS_POWER_SHUTDOWN);
printf("get_power_enable %d\r\n",get_power_enable());
set_interrupt_enable(ALS_INTERRUPT_DISABLE);
set_gain_value(ALS_GAIN_d8);
printf("get_gain_value == %d\r\n",get_gain_value());
set_integration_time(ALS_INTEGRATION_100ms);
set_power_saving_mode(ALS_POWER_MODE_DISABLE);
set_power_enable(ALS_POWER_ON);
}
.h 文件文章来源:https://www.toymoban.com/news/detail-442780.html
#ifndef __VEML_7700_H__
#define __VEML_7700_H__
#define ENABLE_VEML7700 1
#define VEML_SLAVE_ADDR 0x20
#define COMMAND_ALS_DEFAULT_VALUE 0x01
// Gain Register
#define COMMAND_ALS_GAIN 0x00
#define ALS_GAIN_MASK 0x1800
#define ALS_GAIN_SHIFT 11
// ALS IT Register
#define COMMAND_ALS_IT 0x00
#define ALS_IT_MASK 0x03C0
#define ALS_IT_SHIFT 6
// ALS Persistence Register
#define COMMAND_ALS_PER 0x00
#define ALS_PER_MASK 0x0030
#define ALS_PER_SHIFT 4
// ALS INT EN Register
#define COMMAND_ALS_INT_EN 0x00
#define ALS_INT_EN_MASK 0x0002
#define ALS_INT_EN_SHIFT 1
// ALS SD Register
#define COMMAND_ALS_SD 0x00
#define ALS_SD_MASK 0x0001
#define ALS_SD_SHIFT 0
// ALS Window High
#define COMMAND_ALS_WH 0x01
// ALS Window Low
#define COMMAND_ALS_WL 0x02
// ALS Power Save Mode
#define COMMAND_PSM 0x03
#define PSM_MASK 0x0006
#define PSM_SHIFT 1
// ALS Power Save Mode Enable
#define COMMAND_PSM_EN 0x03
#define PSM_EN_MASK 0x0001
#define PSM_EN_SHIFT 0
// ALS High Resolution Output Data
#define COMMAND_ALS_DATA 0x04
//White Channel Output Data
#define COMMAND_WHITE_DATA 0x05
// Interrupt Status
#define COMMAND_ALS_IF_L 0x06
#define ALS_IF_L_MASK 0x8000
#define ALS_IF_L_SHIFT 15
#define COMMAND_ALS_IF_H 0x06
#define ALS_IF_H_MASK 0x4000
#define ALS_IF_H_SHIFT 14
#define ALS_GAIN_X1 0x0
#define ALS_GAIN_X2 0x1
#define ALS_GAIN_d8 0x2
#define ALS_GAIN_d4 0x3
#define ALS_INTEGRATION_25ms 0xC
#define ALS_INTEGRATION_50ms 0x8
#define ALS_INTEGRATION_100ms 0x0
#define ALS_INTEGRATION_200ms 0x1
#define ALS_INTEGRATION_400ms 0x2
#define ALS_INTEGRATION_800ms 0x3
#define ALS_PERSISTENCE_1 0x0
#define ALS_PERSISTENCE_2 0x1
#define ALS_PERSISTENCE_4 0x2
#define ALS_PERSISTENCE_8 0x3
#define ALS_POWER_MODE_1 0x0
#define ALS_POWER_MODE_2 0x1
#define ALS_POWER_MODE_3 0x2
#define ALS_POWER_MODE_4 0x3
#define ALS_POWER_MODE_ENABLE 0x1
#define ALS_POWER_MODE_DISABLE 0x0
#define ALS_INTERRUPT_ENABLE 0x1
#define ALS_INTERRUPT_DISABLE 0x0
#define ALS_POWER_ON 0x0
#define ALS_POWER_SHUTDOWN 0x1
void veml7700_init(void);
int get_lux_data(void);
#endif
main函数调用,先调用init函数,然后在while循环中点用get_lux_data函数。
5、打印以及现象:
6、代码路径:https://gitee.com/xiaoguo-tec_0/gd32-iap-code.git文章来源地址https://www.toymoban.com/news/detail-442780.html
到了这里,关于单片机GD32F303RCT6 (Macos环境)开发 (二十)—— 光感芯片veml7700的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!