STM32F103C8开发紫外线传感器SI1133的使用(七)

SI1133传感器是基于I2C通信的,关于I2C部分请参考:


关于传感器初始化参数的说明:


PARAM_CHAN_LIST:配置通道,从bit0到bit5,分别代表通道0到5,共六个通道,最高两位无效;


PARAM_ADCCONFIGx:半导体功能配置,0x78表示配置成UV紫外线检测,积分时间基数短24.4us,即CFG_RATE_SHORT|CFG_UV,其余见datasheet;
PARAM_ADCSENSx:检测信号范围,最高位置1表示高,可防止结果溢出,低4位表示积分时间;
PARAM_ADCPOSTx:测量结果设置,0x00表示结果为16位模式,0x40表示24位模式;
PARAM_MEASCONFIGx:计数器选择,0x00表示不设置,0x40设置计数器0,0x80设置计数器1,0xc0设置计数器2


PARAM_MEASRATE_H:采样频率设置高位
PARAM_MEASRATE_L:采样频率设置低位


REG_IRQ_ENABLE:开启通道中断


按照SI1133_init()中的设置,积分时间基数为24.4us,积分时间t = 24.4us * 2^0x09 = 12.5ms,采样频率f = 1250Hz/0x09c4 = 0.5Hz;




关于结果:
根据以上配置,传感器检测结果保存在REG_HOSTOUT0-REG_HOSTOUT11中,每个通道占用两个字节,高位在前;
CH0H,CH0L,CH1H,CH1L..............
如设置了24位模式,则结果如下:

CH0H,CH0L,CH1H,CH1M,CH1L........

头文件:

#ifndef __SI1133_H__
#define __SI1133_H__

#include "tim.h"
#include "i2c.h"

#define SI1133_PART_ID				  0xAA

//*******************************************************//
//*                   ADCCONFIGx                        *//
//*******************************************************//

//=========================================================
//  measurement rate,Offsets for Parameter Table ADCCONFIGx
//=========================================================
#define CFG_RATE_SHORT  		  0X60 //24.4us
#define CFG_RATE_NORMAL 		  0X00 //48.8us
#define CFG_RATE_LONG   		  0X20 //97.6us
#define CFG_RATE_VLONG  		  0X40 //195us
//==========================================================
// Photodiodos select,Offsets for Parameter Table ADCCONFIGx
//==========================================================
#define CFG_SMALL_IR 			  0X00 
#define CFG_MEDIUM_IR			  0X01
#define CFG_LARGE_IR			  0X02
#define CFG_WHITE   			  0X0B
#define CFG_LARGE_WHITE 		  0X0C
#define CFG_UV  				  0X18
#define CFG_UV_DEEP 			  0X19

//------------------------END------------------------------

//*******************************************************//
//*                   MEASCONFIGx                       *//
//*******************************************************//

//===========================
// MEASCOUNTER select,Value for Parameter Table MEASCONFIGx
//===========================
#define MEAS_COUNT_NONE 		  0X00
#define MEAS_COUNT_0 			  0X40
#define MEAS_COUNT_1 			  0X80
#define MEAS_COUNT_2 			  0XC0

//------------------------END------------------------------

//*******************************************************//
//*                      ADCPOSTx                       *//
//*******************************************************//
//===========================
// OUTPUT setting,Offsets for Parameter Table ADCPOSTx
//===========================
#define POST_BITS_16			  0X00
#define POST_BITS_24			  0X40
//===========================
// THRESHOLD setting,Offsets for Parameter Table ADCPOSTx
//===========================
#define POST_THRESHOLD_NONE		  0X00
#define POST_THRESHOLD_0		  0X01
#define POST_THRESHOLD_1		  0X02
#define POST_THRESHOLD_2		  0X03
//------------------------END------------------------------

//===========================
// I2C Registers Address
//===========================
#define REG_PART_ID               0x00
#define REG_REV_ID                0x01
#define REG_MFR_ID                0x02
#define REG_INFO0             	  0x03
#define REG_INFO1                 0x04

#define REG_HOSTIN3               0x07
#define REG_HOSTIN2               0x08
#define REG_HOSTIN1               0x09
#define REG_HOSTIN0               0x0A
#define REG_COMMAND               0x0B

#define REG_IRQ_ENABLE            0x0F
#define REG_RESET                 0x0F
#define REG_RESPONSE1             0x10
#define REG_RESPONSE0             0x11
#define REG_IRQ_STATUS            0x12

#define REG_HOSTOUT0              0x13
#define REG_HOSTOUT1              0x14
#define REG_HOSTOUT2              0x15
#define REG_HOSTOUT3              0x16
#define REG_HOSTOUT4              0x17
#define REG_HOSTOUT5              0x18
#define REG_HOSTOUT6              0x19
#define REG_HOSTOUT7              0x1A
#define REG_HOSTOUT8              0x1B
#define REG_HOSTOUT9              0x1C
#define REG_HOSTOUT10             0x1D
#define REG_HOSTOUT11             0x1E
#define REG_HOSTOUT12             0x1F
#define REG_HOSTOUT13             0x20
#define REG_HOSTOUT14             0x21
#define REG_HOSTOUT15             0x22
#define REG_HOSTOUT16             0x23
#define REG_HOSTOUT17             0x24
#define REG_HOSTOUT18             0x25
#define REG_HOSTOUT19             0x26
#define REG_HOSTOUT20             0x27
#define REG_HOSTOUT21             0x28
#define REG_HOSTOUT22             0x29
#define REG_HOSTOUT23             0x2A
#define REG_HOSTOUT24             0x2B
#define REG_HOSTOUT25             0x2C
//============================================================
// Parameter Table Offsets
//============================================================
#define PARAM_I2C_ADDR            0x00
#define PARAM_CHAN_LIST           0x01

#define PARAM_ADCCONFIG0          0x02
#define PARAM_ADCSENS0            0x03
#define PARAM_ADCPOST0            0x04
#define PARAM_MEASCONFIG0         0x05

#define PARAM_ADCCONFIG1          0x06
#define PARAM_ADCSENS1            0x07
#define PARAM_ADCPOST1            0x08
#define PARAM_MEASCONFIG1         0x09

#define PARAM_ADCCONFIG2          0x0A
#define PARAM_ADCSENS2            0x0B
#define PARAM_ADCPOST2            0x0C
#define PARAM_MEASCONFIG2         0x0D

#define PARAM_ADCCONFIG3          0x0E
#define PARAM_ADCSENS3            0x0F
#define PARAM_ADCPOST3            0x10
#define PARAM_MEASCONFIG3         0x11

#define PARAM_ADCCONFIG4          0x12
#define PARAM_ADCSENS4            0x13
#define PARAM_ADCPOST4            0x14
#define PARAM_MEASCONFIG4         0x15

#define PARAM_ADCCONFIG5          0x16
#define PARAM_ADCSENS5            0x17
#define PARAM_ADCPOST5            0x18
#define PARAM_MEASCONFIG5         0x19

#define PARAM_MEASRATE_H          0x1A
#define PARAM_MEASRATE_L          0x1B
#define PARAM_MEASCOUNT0          0x1C
#define PARAM_MEASCOUNT1          0x1D
#define PARAM_MEASCOUNT2          0x1E

#define PARAM_LED1_A        	  0x1F
#define PARAM_LED1_B        	  0x20
#define PARAM_LED2_A        	  0x21
#define PARAM_LED2_B        	  0x22
#define PARAM_LED3_A        	  0x23
#define PARAM_LED3_B        	  0x24

#define PARAM_THRESHOLD0_H  	  0x25
#define PARAM_THRESHOLD0_L  	  0x26
#define PARAM_THRESHOLD1_H  	  0x27
#define PARAM_THRESHOLD1_L  	  0x28
#define PARAM_THRESHOLD2_H  	  0x29
#define PARAM_THRESHOLD2_L  	  0x2A
#define PARAM_BURST         	  0x2B

//==================================================================
//COMMAND Register Value
//==================================================================
#define CMD_RESET_CTR			  0x00
#define CMD_RESET_SW			  0x01
#define CMD_FORCE				  0x11
#define CMD_PAUSE				  0x12
#define CMD_START				  0x13
#define CMD_WRITE				  0x80
#define CMD_READ				  0x40

/*******************************************************************************
 *******    Si115x Register and Parameter Bit Definitions  *********************
 ******************************************************************************/
#define RSP0_CHIPSTAT_MASK      0xe0
#define RSP0_COUNTER_MASK       0x1f
#define RSP0_SLEEP              0x20

extern int16_t SI1133_ReadRegister(uint8_t addr);
extern int16_t SI1133_WriteRegister(uint8_t addr, uint8_t data);
extern int16_t SI1133_ReadParameter(uint8_t addr);
extern int16_t SI1133_WriteParameter(uint8_t addr, uint8_t data);
extern int16_t SI1133_Init(void);
extern int16_t SI1133_NOP(void);
extern int16_t SI1133_Force(void);
extern int16_t SI1133_Start (void);
extern int16_t SI1133_Pause(void);

#endif

驱动:


#include "si1133.h"

int16_t SI1133_ReadRegister(uint8_t addr)
{
	uint8_t x;
	I2C_ReadByte(SI1133_PART_ID, addr, &x);
	return x;
}

int16_t SI1133_WriteRegister(uint8_t addr, uint8_t data)
{
	I2C_WriteByte(SI1133_PART_ID, addr, &data);
	return 0;
}

static int16_t SI1133_WriteBlock(uint8_t addr, uint8_t* p_data, uint16_t len)
{
	I2C_WriteBlock(SI1133_PART_ID, addr, p_data, len);
	return 0;
}

static int16_t SI1133_ReadBlock(uint8_t addr, uint8_t* p_data, uint16_t len)
{
	I2C_ReadBlock(SI1133_PART_ID, addr, p_data, len);
	return 0;
}

static int16_t WaitUntilSleep(void)
{
	int16_t retval = -1;
	uint8_t count = 0;
	while (count < 5)
	{
		retval = SI1133_ReadRegister(REG_RESPONSE0);
		if ((retval & RSP0_CHIPSTAT_MASK) == RSP0_SLEEP)   break;
		if (retval <  0)   return retval;
		count++;
	}
	return 0;
}

static int16_t SI1133_SendCMD(uint8_t command)
{
	int16_t  response, retval;
	uint8_t  count = 0;
	response = SI1133_ReadRegister(REG_RESPONSE0);
	if (response < 0) return response;
	response = response & RSP0_COUNTER_MASK;
	for (count = 0 ; count < 5; count++)
	{
		if ((retval = WaitUntilSleep()) != 0) return retval;
		if (command == 0) break;
		retval = SI1133_ReadRegister(REG_RESPONSE0);
		if ((retval & RSP0_COUNTER_MASK) == response) break;
		else if (retval < 0) return retval;
		else
			response = retval & RSP0_COUNTER_MASK;
	}
	if ((retval = (SI1133_WriteRegister(REG_COMMAND, command)) != 0))
	{
		return retval;
	}
	for (count = 0 ; count < 5; count++)
	{
		if (command == 0)  break;
		retval = SI1133_ReadRegister(REG_RESPONSE0);
		if ((retval & RSP0_COUNTER_MASK) != response) break;
		else if (retval < 0) return retval;
	}
	return 0;
}

int16_t SI1133_ReadParameter(uint8_t addr)
{
	int16_t retval;
	retval = SI1133_SendCMD(CMD_READ | (addr & 0x3F));
	if ( retval != 0 )    return retval;
	retval = SI1133_ReadRegister(REG_RESPONSE1);
	return retval;
}

int16_t SI1133_WriteParameter(uint8_t addr, uint8_t data)
{
	uint8_t buffer[2];
	int16_t retval, response_stored, response;
	buffer[0] = data; buffer[1] = CMD_WRITE | (addr & 0x3F);
	if ((retval = WaitUntilSleep()) != 0)    return retval;
	response_stored = RSP0_COUNTER_MASK & SI1133_ReadRegister(REG_RESPONSE0);
	retval = SI1133_WriteBlock(REG_HOSTIN0, (uint8_t *)buffer, 2);
	if (retval != 0)  return retval;
	response = SI1133_ReadRegister(REG_RESPONSE0);
	while ((response & RSP0_COUNTER_MASK) == response_stored)
	{
		response = SI1133_ReadRegister(REG_RESPONSE0);
	}
	if (retval < 0)   return retval;
	else   return 0;
}

static int16_t SI1133_Reset(void)
{
	int16_t retval = 0;
	Delay_ms(10);
	Delay_ms(10);
	Delay_ms(10);
	retval += SI1133_WriteRegister(REG_COMMAND, CMD_RESET_SW);
	Delay_ms(10);
	return retval;
}

int16_t SI1133_NOP(void)
{
	return SI1133_SendCMD(CMD_RESET_CTR);
}

int16_t SI1133_Force(void)
{
	return SI1133_SendCMD(CMD_FORCE);
}

int16_t SI1133_Start (void)
{
	return SI1133_SendCMD(CMD_START);
}

int16_t SI1133_Pause(void)
{
	uint8_t countA, countB;
	int8_t  retval = 0;
	while ((RSP0_CHIPSTAT_MASK & retval) != RSP0_SLEEP)
	{
		retval = SI1133_ReadRegister(REG_RESPONSE0);
	}
	countA = 0;
	while (countA < 5)
	{
		countB = 0;
		while (countB < 5)
		{
			retval = SI1133_ReadRegister(REG_RESPONSE0);
			if ((retval & RSP0_COUNTER_MASK) == 0)     break;
			else
			{
				SI1133_WriteRegister(REG_COMMAND, 0x00);
			}
			countB++;
		}
		SI1133_SendCMD(CMD_PAUSE);
		countB = 0;
		while (countB < 5)
		{
			retval = SI1133_ReadRegister(REG_RESPONSE0);
			if ((retval & RSP0_COUNTER_MASK) != 0)        break;
			countB++;
		}
		retval = SI1133_ReadRegister(REG_RESPONSE0);
		if ((retval & RSP0_COUNTER_MASK) == 1 )      break;
		countA++;
	}
	return 0;
}
int16_t SI1133_Init(void)
{
	int16_t    retval;
	retval  = SI1133_Reset();
	Delay_ms(10);
	retval += SI1133_WriteParameter( PARAM_CHAN_LIST, 	0x3f);
	//UV
	retval += SI1133_WriteParameter( PARAM_ADCCONFIG0, 	0x78);
	retval += SI1133_WriteParameter( PARAM_ADCSENS0, 	0x89);
	retval += SI1133_WriteParameter( PARAM_ADCPOST0, 	0x00);
	retval += SI1133_WriteParameter( PARAM_MEASCONFIG0, 0x40);
	//
	retval += SI1133_WriteParameter( PARAM_ADCCONFIG1, 	0x6d);
	retval += SI1133_WriteParameter( PARAM_ADCSENS1, 	0x89);
	retval += SI1133_WriteParameter( PARAM_ADCPOST1, 	0x00);
	retval += SI1133_WriteParameter( PARAM_MEASCONFIG1, 0x40);

	retval += SI1133_WriteParameter( PARAM_ADCCONFIG2, 	0x6b);
	retval += SI1133_WriteParameter( PARAM_ADCSENS2, 	0x89);
	retval += SI1133_WriteParameter( PARAM_ADCPOST2, 	0x00);
	retval += SI1133_WriteParameter( PARAM_MEASCONFIG2, 0x40);

	retval += SI1133_WriteParameter( PARAM_ADCCONFIG3, 	0x61);
	retval += SI1133_WriteParameter( PARAM_ADCSENS3, 	0x89);
	retval += SI1133_WriteParameter( PARAM_ADCPOST3, 	0x00);
	retval += SI1133_WriteParameter( PARAM_MEASCONFIG3, 0x40);

	retval += SI1133_WriteParameter( PARAM_ADCCONFIG4, 	0x60);
	retval += SI1133_WriteParameter( PARAM_ADCSENS4, 	0x89);
	retval += SI1133_WriteParameter( PARAM_ADCPOST4, 	0x00);
	retval += SI1133_WriteParameter( PARAM_MEASCONFIG4, 0x40);

	retval += SI1133_WriteParameter( PARAM_ADCCONFIG5, 	0x79);
	retval += SI1133_WriteParameter( PARAM_ADCSENS5, 	0x89);
	retval += SI1133_WriteParameter( PARAM_ADCPOST5, 	0x00);
	retval += SI1133_WriteParameter( PARAM_MEASCONFIG5, 0x40);

	retval += SI1133_WriteParameter( PARAM_MEASRATE_H, 	0x09);
	retval += SI1133_WriteParameter( PARAM_MEASRATE_L, 	0xc4);
	retval += SI1133_WriteRegister(  REG_IRQ_ENABLE, 	0x3f);
	retval += SI1133_Start();
	return retval;
}


你可能感兴趣的:(STM32单片机学习,STM32,UV,SI1133,传感器)