STM 32 하 광 센서 TSL 구동

주로 본인 의 I2C 아 날로 그 라 이브 러 리 파일 을 바탕 으로 이 루어 집 니 다.
#ifndef _TSL_DRV_H_
#define _TSL_DRV_H_
#include "stm32f10x_lib.h"
#include "stm32f10x_nvic.h"

void TSL_i2cInit(void);
u8 TSL_powerUp(void);
void TSL_powerDown(void);
u8 TSL_getLux(float *lux_val);

#endif //_TSL_DRV_H_
#include "HKY_tsl.h"
#include <math.h>

#undef  PIN_SCL
#define PIN_SCL 	GPIO_Pin_10


#undef  PIN_SDA
#define PIN_SDA 	GPIO_Pin_11


#include "HKY_i2c.h"

/*------------------------------TSL 2550-------------------------------------*/
#define TSL_RD_ADDR 	       	0x73 //tsl2550, read address
#define TSL_WR_ADDR 		0x72 //tsl2550, write address
#define TSL_CMD_PD   		0x00 // power down
#define TSL_CMD_PU   		0x03 // power up and read config.
#define TSL_CMD_EXT  		0x1D // set extended mode
#define TSL_CMD_RST  		0x18 // reset to standard mode
#define TSL_CMD_ADC0 		0x43 // read ADC0 value
#define TSL_CMD_ADC1 		0x83 // read ADC1 value
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  TSL_i2cInit 
 */
/* --------------------------------------------------------------------------*/
void TSL_i2cInit(void)
{
    int i;
    TWI_Initialize();
    for (i = 0; i < 10; i++)
    {
	TWI_SCL_0();
	TWI_NOP();
	TWI_SCL_1();
	TWI_NOP();
    }
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  i2c_write_single 
 * 
 * @Param: addr
 * @Param: data
 * 
 * @Returns:   
 */
/* --------------------------------------------------------------------------*/
static u8 i2c_write_single(u8 addr, u8 data)
{
    u8 ret = TWI_ACK;
    ret = TWI_START();
    if (ret == TWI_BUS_ERROR)
	return ret;

    ret = TWI_SendByte(addr);
    if (ret == TWI_NACK)
	return ret;

    ret = TWI_SendByte(data);
    if (ret == TWI_NACK)
	return ret;

    TWI_STOP();

    return ret;
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  i2c_read_single
 * @Desp :  read one single byte
 * 
 * @Param: addr
 * @Param: data
 * 
 * @Returns: TWI_ACK, TWI_NACK 
 */
/* --------------------------------------------------------------------------*/
static u8 i2c_read_single(u8 addr, u8 *data)
{
    u8 ret = TWI_ACK;
    
    ret = TWI_START();
    if (ret == TWI_BUS_ERROR)
        return ret;

    //send address
    ret = TWI_SendByte(addr);
    if (ret == TWI_NACK)
    	return ret;
	
    //read one byte
    *data = TWI_ReceiveByte();

    TWI_SendNACK();
    TWI_STOP();

    return ret;
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  TSL_powerUp 
 * 
 * @Returns:  TWI_NACK, COMMAND
 */
/* --------------------------------------------------------------------------*/
u8 TSL_powerUp(void)
{
    u8 val;
    u8 ret;
    ret = i2c_write_single(TSL_WR_ADDR, TSL_CMD_PU);
    if (ret != 0)
	return ret;
    ret = i2c_read_single(TSL_RD_ADDR, &val);
    if (ret != 0)
	return ret;
    return val;
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  TSL_powerDown 
 */
/* --------------------------------------------------------------------------*/
void TSL_powerDown(void)
{
    i2c_write_single(TSL_WR_ADDR, TSL_CMD_PD);
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  TSL_readADC0 
 * 
 * @Returns:  succ: val
 * 	      fail: -1 
 */
/* --------------------------------------------------------------------------*/
u8 TSL_readADC0(u8 *val)
{
    u8 ret;
    
    ret = i2c_write_single(TSL_WR_ADDR, TSL_CMD_ADC0);
    if (ret != 0)
	return ret;

    ret = i2c_read_single(TSL_RD_ADDR, val);
    if (ret != 0)
	return ret;

    return ret;
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  TSL_readADC1 
 * 
 * @Returns:  succ: val
 * 	      fail: -1 
 */
/* --------------------------------------------------------------------------*/
u8 TSL_readADC1(u8 *val)
{
    u8 ret;
    
    ret = i2c_write_single(TSL_WR_ADDR, TSL_CMD_ADC1);
    if (ret != 0)
	return ret;
    
    ret = i2c_read_single(TSL_RD_ADDR, val);
    if (ret != 0)
	return ret;
    return ret;
}

/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  get_count_val 
 * 
 * @Param: ch
 * 
 * @Returns:   
 */
/* --------------------------------------------------------------------------*/
#define GET_CHORD(ch) ( ((ch) >> 4) & 0x07 )
#define GET_STEP(ch) ((ch) & 0x0f )

static u32 get_count_val(u8 ch)
{
    u32 C, S;
    float CC;
    u32 count_val;

    C = GET_CHORD(ch);
    S = GET_STEP(ch);
    CC = pow(2, C);

    count_val = (u32)( 16.5 * (CC - 1) + (S * CC) );
    return count_val;   
}
/* --------------------------------------------------------------------------*/
/** 
 * @Brief:  TSL_getLux 
 * 
 * @Param: ch0
 * @Param: ch1
 * 
 * @Returns:   
 */
/* --------------------------------------------------------------------------*/
u8 TSL_getLux(float *lux_val)
{
    float r;
    float count_val_0, count_val_1;
    u8 ch0, ch1;
    int ret;

    NVIC_SETPRIMASK();
    ret = TSL_readADC0(&ch0);
    NVIC_RESETPRIMASK();
    if (ret != 0)
	return ret;
    
    NVIC_SETPRIMASK();
    ret = TSL_readADC1(&ch1);
    NVIC_RESETPRIMASK();
    if (ret != 0)
	return ret;

    ch0 &= 0x7f;
    ch1 &= 0x7f;

    count_val_0 = get_count_val(ch0);
    count_val_1 = get_count_val(ch1);

    r = count_val_1 / count_val_0;
   
    *lux_val = (float)(count_val_0 * 0.46 * exp((-1) * 3.13 * r));

    return 0;
}

좋은 웹페이지 즐겨찾기