STM 32 하 광 센서 TSL 구동
#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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.