加入星計(jì)劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入

嵌入式外設(shè)-顏色識別與反饋控制(TCS34725)驅(qū)動代碼編寫

06/07 11:49
2317
服務(wù)支持:
技術(shù)交流群

完成交易后在“購買成功”頁面掃碼入群,即可與技術(shù)大咖們分享疑惑和經(jīng)驗(yàn)、收獲成長和認(rèn)同、領(lǐng)取優(yōu)惠和紅包等。

虛擬商品不可退

當(dāng)前內(nèi)容為數(shù)字版權(quán)作品,購買后不支持退換且無法轉(zhuǎn)移使用。

加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論
放大
實(shí)物圖
相關(guān)方案
  • 方案介紹
    • 一、前言
    • 二、資料獲取
    • 三、設(shè)備使用
    • 四、代碼編寫
    • ?五、參考
  • 相關(guān)文件
  • 推薦器件
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

一、前言

是基于AMS 的TCS3472XFN 彩色光數(shù)字轉(zhuǎn)換器為核心的顏色傳感器,傳感器提供紅色,綠色,藍(lán)色(RGB)和清晰光感應(yīng)值的數(shù)字輸出。集成紅外阻擋濾光片可最大限度地減少入射光的紅外光譜成分,并可精確地進(jìn)行顏色測量。具有高靈敏度,寬動態(tài)范圍和紅外阻隔濾波器。最小化IR 和UV 光譜分量效應(yīng),以產(chǎn)生準(zhǔn)確的顏色測量。并且?guī)в协h(huán)境光強(qiáng)檢測和可屏蔽中斷。通過I2C 接口通信。本設(shè)計(jì)基于同一個設(shè)計(jì)原理,提供2 個不同造型設(shè)計(jì)(方形版本/雙孔版本)

產(chǎn)品參數(shù)

工作電壓:3.3V/5V
控制芯片:TCS3472XFN
邏輯電壓:3.3V/5V
通信接口:I2C

二、資料獲取

關(guān)注微信公眾號--星之援工作室 發(fā)送關(guān)鍵字(TCS34725

自己重新寫了一遍控制部分,并添加了反饋效果演示

????

三、設(shè)備使用

接線

接線方式如下,可根據(jù)個人喜好進(jìn)行引腳的切換

圖片演示

視頻演示

懸空會自動反饋光譜最大的值進(jìn)行顯示

顏色識別

四、代碼編寫

main.c

實(shí)現(xiàn)函數(shù)調(diào)用


/*接線 
星之援學(xué)習(xí)資料 顏色識別

VCC------------>供電
DIO------------>PB9
SCLK------------>PB8 
LOAD------------>PB7 
GND------------>接地

RGB
R ---- PC15
B ---- PC14
G ---- PC13

可以根據(jù)識別到的顏色值進(jìn)行亮燈提示

*/
#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#include "tcs3472.h"

int main(void)
{
	u16 time_cnt =0;
	u8 rgb_max;
	delay_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	 //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
	// LED 端口初始化 
	LED_GPIO_Config();
	// 串口初始化
	uart_init(115200);
	// 顏色識別
	TCS34725_Init();
	printf("init okn");
	
	while ( 1 )
	{
		time_cnt++;
		if(time_cnt %50 == 0){
			TCS34725_GetRawData(&rgb);  //讀兩次,實(shí)際測試時發(fā)現(xiàn)讀到的顏色總是上一次的顏色
			RGBtoHSL(&rgb,&hsl);
			// 反饋?zhàn)畲?
			rgb_max = max3v(rgb.r,rgb.g,rgb.b);
			if(rgb_max == rgb.r){
				LED_RED
			}
			else if(rgb_max == rgb.g){
				LED_GREEN
			}
			else if(rgb_max == rgb.b){
				LED_BLUE
			}
			printf("R=%d G=%d B=%d C=%drn",rgb.r,rgb.g,rgb.b,rgb.c);
			printf("H=%d S=%d L=%drn",hsl.h,hsl.s,hsl.l);
		}
		delay_ms(10);
	}
}

tcs3472.h

#ifndef __TCS_H
#define	__TCS_H
#include "stm32f10x.h"

/******************************************************************************/
#define TCS34725_ADDRESS          (0x29)

#define TCS34725_COMMAND_BIT      (0x80)

#define TCS34725_ENABLE           (0x00)
#define TCS34725_ENABLE_AIEN      (0x10)    /* RGBC Interrupt Enable */
#define TCS34725_ENABLE_WEN       (0x08)    /* Wait enable - Writing 1 activates the wait timer */
#define TCS34725_ENABLE_AEN       (0x02)    /* RGBC Enable - Writing 1 actives the ADC, 0 disables it */
#define TCS34725_ENABLE_PON       (0x01)    /* Power on - Writing 1 activates the internal oscillator, 0 disables it */
#define TCS34725_ATIME            (0x01)    /* Integration time */
#define TCS34725_WTIME            (0x03)    /* Wait time (if TCS34725_ENABLE_WEN is asserted) */
#define TCS34725_WTIME_2_4MS      (0xFF)    /* WLONG0 = 2.4ms   WLONG1 = 0.029s */
#define TCS34725_WTIME_204MS      (0xAB)    /* WLONG0 = 204ms   WLONG1 = 2.45s  */
#define TCS34725_WTIME_614MS      (0x00)    /* WLONG0 = 614ms   WLONG1 = 7.4s   */
#define TCS34725_AILTL            (0x04)    /* Clear channel lower interrupt threshold */
#define TCS34725_AILTH            (0x05)
#define TCS34725_AIHTL            (0x06)    /* Clear channel upper interrupt threshold */
#define TCS34725_AIHTH            (0x07)
#define TCS34725_PERS             (0x0C)    /* Persistence register - basic SW filtering mechanism for interrupts */
#define TCS34725_PERS_NONE        (0b0000)  /* Every RGBC cycle generates an interrupt                                */
#define TCS34725_PERS_1_CYCLE     (0b0001)  /* 1 clean channel value outside threshold range generates an interrupt   */
#define TCS34725_PERS_2_CYCLE     (0b0010)  /* 2 clean channel values outside threshold range generates an interrupt  */
#define TCS34725_PERS_3_CYCLE     (0b0011)  /* 3 clean channel values outside threshold range generates an interrupt  */
#define TCS34725_PERS_5_CYCLE     (0b0100)  /* 5 clean channel values outside threshold range generates an interrupt  */
#define TCS34725_PERS_10_CYCLE    (0b0101)  /* 10 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_15_CYCLE    (0b0110)  /* 15 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_20_CYCLE    (0b0111)  /* 20 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_25_CYCLE    (0b1000)  /* 25 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_30_CYCLE    (0b1001)  /* 30 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_35_CYCLE    (0b1010)  /* 35 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_40_CYCLE    (0b1011)  /* 40 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_45_CYCLE    (0b1100)  /* 45 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_50_CYCLE    (0b1101)  /* 50 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_55_CYCLE    (0b1110)  /* 55 clean channel values outside threshold range generates an interrupt */
#define TCS34725_PERS_60_CYCLE    (0b1111)  /* 60 clean channel values outside threshold range generates an interrupt */
#define TCS34725_CONFIG           (0x0D)
#define TCS34725_CONFIG_WLONG     (0x02)    /* Choose between short and long (12x) wait times via TCS34725_WTIME */
#define TCS34725_CONTROL          (0x0F)    /* Set the gain level for the sensor */
#define TCS34725_ID               (0x12)    /* 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727 */
#define TCS34725_STATUS           (0x13)
#define TCS34725_STATUS_AINT      (0x10)    /* RGBC Clean channel interrupt */
#define TCS34725_STATUS_AVALID    (0x01)    /* Indicates that the RGBC channels have completed an integration cycle */
#define TCS34725_CDATAL           (0x14)    /* Clear channel data */
#define TCS34725_CDATAH           (0x15)
#define TCS34725_RDATAL           (0x16)    /* Red channel data */
#define TCS34725_RDATAH           (0x17)
#define TCS34725_GDATAL           (0x18)    /* Green channel data */
#define TCS34725_GDATAH           (0x19)
#define TCS34725_BDATAL           (0x1A)    /* Blue channel data */
#define TCS34725_BDATAH           (0x1B)

#define TCS34725_INTEGRATIONTIME_2_4MS   0xFF   /**<  2.4ms - 1 cycle    - Max Count: 1024  */
#define TCS34725_INTEGRATIONTIME_24MS    0xF6   /**<  24ms  - 10 cycles  - Max Count: 10240 */
#define TCS34725_INTEGRATIONTIME_50MS    0xEB   /**<  50ms  - 20 cycles  - Max Count: 20480 */
#define TCS34725_INTEGRATIONTIME_101MS   0xD5   /**<  101ms - 42 cycles  - Max Count: 43008 */
#define TCS34725_INTEGRATIONTIME_154MS   0xC0   /**<  154ms - 64 cycles  - Max Count: 65535 */
#define TCS34725_INTEGRATIONTIME_240MS   0x9C   /**<  240ms - 100 cycles - Max Count: 65535 */
#define TCS34725_INTEGRATIONTIME_700MS   0x00   /**<  700ms - 256 cycles - Max Count: 65535 */

#define TCS34725_GAIN_1X                 0x00   /**<  No gain  */
#define TCS34725_GAIN_4X                 0x01   /**<  4x gain  */
#define TCS34725_GAIN_16X                0x02   /**<  16x gain */
#define TCS34725_GAIN_60X                0x03   /**<  60x gain */

/* 定義IIC連接的GPIO端口, 用戶只需要修改下面的代碼即可改變控制的LED引腳 */
#define TCS_DIO_GPIO_PORT    	GPIOB			              /* GPIO端口 */
#define TCS_DIO_GPIO_CLK 	    RCC_APB2Periph_GPIOB		/* GPIO端口時鐘 */
#define TCS_DIO_GPIO_PIN		    GPIO_Pin_9

#define TCS_CLK_GPIO_PORT    	GPIOB		                /* GPIO端口 */
#define TCS_CLK_GPIO_CLK 	    RCC_APB2Periph_GPIOB		/* GPIO端口時鐘 */
#define TCS_CLK_GPIO_PIN			  GPIO_Pin_8

#define TCS_SCL_H     GPIO_SetBits(TCS_CLK_GPIO_PORT,TCS_CLK_GPIO_PIN)
#define TCS_SCL_L     GPIO_ResetBits(TCS_CLK_GPIO_PORT,TCS_CLK_GPIO_PIN)
#define TCS_SDA_H     GPIO_SetBits(TCS_DIO_GPIO_PORT,TCS_DIO_GPIO_PIN)
#define TCS_SDA_L     GPIO_ResetBits(TCS_DIO_GPIO_PORT,TCS_DIO_GPIO_PIN)

#define TCS_SDA_READ   PBin(9)
/******************************************************************************/
#define max3v(v1, v2, v3)   ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))
#define min3v(v1, v2, v3)   ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))

typedef struct{
	unsigned short  c;      //[0-65536]
	unsigned short  r;
	unsigned short  g;
	unsigned short  b;
}COLOR_RGBC;//RGBC

typedef struct{
	unsigned short h;       //[0,360]
	unsigned char  s;       //[0,100]
	unsigned char  l;       //[0,100]
}COLOR_HSL;//HSL

extern COLOR_RGBC rgb;
extern COLOR_HSL  hsl;
// 端口初始化
u8 TCS34725_Init(void);
//RGB轉(zhuǎn)HSL
void RGBtoHSL(COLOR_RGBC *Rgb, COLOR_HSL *Hsl);
/*******************************************************************************
 * @brief TCS34725獲取各個通道數(shù)據(jù)
*******************************************************************************/
u8 TCS34725_GetRawData(COLOR_RGBC *rgbc);

#endif /* TCS */

tcs3472.c

#include "tcs3472.h"
#include "delay.h"
 
 
COLOR_RGBC rgb;
COLOR_HSL  hsl;


//端口初始化
void TCS34725_I2C_Init( void )
{
 
	GPIO_InitTypeDef  GPIO_InitStructure;
	RCC_APB2PeriphClockCmd( TCS_DIO_GPIO_CLK, ENABLE );
	GPIO_InitStructure.GPIO_Pin = TCS_DIO_GPIO_PIN | TCS_CLK_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init( TCS_DIO_GPIO_PORT, &GPIO_InitStructure );
	
	GPIO_SetBits(TCS_DIO_GPIO_PORT,TCS_DIO_GPIO_PIN | TCS_CLK_GPIO_PIN);   //高電平
}
// 配置雙向I/O端口為輸出態(tài)
static void TCS_SDA_OUT()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(TCS_DIO_GPIO_CLK, ENABLE);

	GPIO_InitStructure.GPIO_Pin = TCS_DIO_GPIO_PIN; // PC.10  DATA
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(TCS_DIO_GPIO_PORT, &GPIO_InitStructure); // 初始化GPIOC.10
}
// 配置雙向I/O端口為輸入態(tài)
static void TCS_SDA_IN()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(TCS_DIO_GPIO_CLK, ENABLE);
	GPIO_InitStructure.GPIO_Pin = TCS_DIO_GPIO_PIN; // PC.10 DATA
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(TCS_DIO_GPIO_PORT, &GPIO_InitStructure); // 初始化GPIOC.10
}

/*********************************************/
void TCS34725_I2C_Start()
{
	TCS_SDA_OUT();
	TCS_SDA_H;
	TCS_SCL_H;
	delay_us(4);//delay_us(4);
	TCS_SDA_L;
	delay_us(4);//delay_us(4);
	TCS_SCL_L;
}
/*********************************************/
void TCS34725_I2C_Stop()
{
	TCS_SDA_OUT();
	TCS_SCL_L;
	TCS_SDA_L;
	delay_us(4);//delay_us(4);
	TCS_SCL_H;
	TCS_SDA_H;
	delay_us(4);//delay_us(4);							   	
}
/*********************************************/
//返回值:1,接收應(yīng)答失敗
//        0,接收應(yīng)答成功
u8 TCS34725_I2C_Wait_ACK()
{
	u32 t=0;
	
	TCS_SDA_IN();//SDA設(shè)置為輸入  
	TCS_SDA_H; 
	delay_us(1);//delay_us(1);
	TCS_SCL_H; 
	delay_us(1);//delay_us(1);
	while(TCS_SDA_READ)
	{
		t++;
		if(t > 250)
		{
			TCS34725_I2C_Stop();
			return 1;
		}
	}
	TCS_SCL_L;
	return 0;	
}
/*********************************************/
//產(chǎn)生ACK應(yīng)答
void TCS34725_I2C_ACK()
{
	TCS_SCL_L;
	TCS_SDA_OUT();//sda線輸出
	TCS_SDA_L;
	delay_us(2);//delay_us(2);
	TCS_SCL_H;
	delay_us(2);//delay_us(2);
	TCS_SCL_L;
}
/*********************************************/
//不產(chǎn)生ACK應(yīng)答		    
void TCS34725_I2C_NACK()
{
	TCS_SCL_L;
	TCS_SDA_OUT();//sda線輸出
	TCS_SDA_H;
	delay_us(2);//delay_us(2);
	TCS_SCL_H;
	delay_us(2);//delay_us(2);
	TCS_SCL_L;
}
/*********************************************/
//I2C發(fā)送一個字節(jié)		  
void TCS34725_I2C_Send_Byte(u8 byte)
{
	u8 i;
	
	TCS_SDA_OUT();//sda線輸出
	TCS_SCL_L;//拉低時鐘開始數(shù)據(jù)傳輸
	for(i = 0; i < 8; i++)
	{
		if(((byte&0x80)>>7)==1)TCS_SDA_H;
		else
			TCS_SDA_L;
		byte <<= 1;
		
		delay_us(2);//delay_us(2);
		TCS_SCL_H;
		delay_us(2);//delay_us(2);
		TCS_SCL_L;
		delay_us(2);//delay_us(2);
	} 
}
/*********************************************/
//讀1個字節(jié),ack=1時,發(fā)送ACK,ack=0,發(fā)送nACK   
u8 TCS34725_I2C_Read_Byte(u8 ack)
{
	u8 i,receive = 0;
	
	TCS_SDA_IN();
	for(i = 0; i < 8; i++)
	{
		TCS_SCL_L;
		delay_us(2);//delay_us(2);
		TCS_SCL_H;
		receive <<= 1;
		if(TCS_SDA_READ) receive++;
		delay_us(1);//delay_us(1);
	}
	if (!ack) TCS34725_I2C_NACK();//發(fā)送nACK
	else TCS34725_I2C_ACK(); //發(fā)送ACK 
	
	return receive;
}
/*******************************************************************************
 * @brief Writes data to a slave device.
 *
 * @param slaveAddress - Adress of the slave device.
 * @param dataBuffer - Pointer to a buffer storing the transmission data.
 * @param bytesNumber - Number of bytes to write.
 * @param stopBit - Stop condition control.
 *                  Example: 0 - A stop condition will not be sent;
 *                           1 - A stop condition will be sent.
*******************************************************************************/
void TCS34725_I2C_Write(u8 slaveAddress, u8* dataBuffer,u8 bytesNumber, u8 stopBit)
{
	u8 i = 0;
	
	TCS34725_I2C_Start();
	TCS34725_I2C_Send_Byte((slaveAddress << 1) | 0x00);	   //發(fā)送從機(jī)地址寫命令
	TCS34725_I2C_Wait_ACK();
	for(i = 0; i < bytesNumber; i++)
	{
		TCS34725_I2C_Send_Byte(*(dataBuffer + i));
		TCS34725_I2C_Wait_ACK();
	}
	if(stopBit == 1) TCS34725_I2C_Stop();
}
/*******************************************************************************
 * @brief Reads data from a slave device.
 *
 * @param slaveAddress - Adress of the slave device.
 * @param dataBuffer - Pointer to a buffer that will store the received data.
 * @param bytesNumber - Number of bytes to read.
 * @param stopBit - Stop condition control.
 *                  Example: 0 - A stop condition will not be sent;
 *                           1 - A stop condition will be sent.
*******************************************************************************/
void TCS34725_I2C_Read(u8 slaveAddress, u8* dataBuffer, u8 bytesNumber, u8 stopBit)
{
	u8 i = 0;
	
	TCS34725_I2C_Start();
	TCS34725_I2C_Send_Byte((slaveAddress << 1) | 0x01);	   //發(fā)送從機(jī)地址讀命令
	TCS34725_I2C_Wait_ACK();
	for(i = 0; i < bytesNumber; i++)
	{
		if(i == bytesNumber - 1)
		{
			*(dataBuffer + i) = TCS34725_I2C_Read_Byte(0);//讀取的最后一個字節(jié)發(fā)送NACK
		}
		else
		{
			*(dataBuffer + i) = TCS34725_I2C_Read_Byte(1);
		}
	}
	if(stopBit == 1) TCS34725_I2C_Stop();
}
/*******************************************************************************
 * @brief Writes data into TCS34725 registers, starting from the selected
 *        register address pointer.
 *
 * @param subAddr - The selected register address pointer.
 * @param dataBuffer - Pointer to a buffer storing the transmission data.
 * @param bytesNumber - Number of bytes that will be sent.
 *
 * @return None.
*******************************************************************************/
void TCS34725_Write(u8 subAddr, u8* dataBuffer, u8 bytesNumber)
{
    u8 sendBuffer[10] = {0, };
    u8 byte = 0;
    
    sendBuffer[0] = subAddr | TCS34725_COMMAND_BIT;
    for(byte = 1; byte <= bytesNumber; byte++)
    {
        sendBuffer[byte] = dataBuffer[byte - 1];
    }
	TCS34725_I2C_Write(TCS34725_ADDRESS, sendBuffer, bytesNumber + 1, 1);
}
/*******************************************************************************
 * @brief Reads data from TCS34725 registers, starting from the selected
 *        register address pointer.
 *
 * @param subAddr - The selected register address pointer.
 * @param dataBuffer - Pointer to a buffer that will store the received data.
 * @param bytesNumber - Number of bytes that will be read.
 *
 * @return None.
*******************************************************************************/
void TCS34725_Read(u8 subAddr, u8* dataBuffer, u8 bytesNumber)
{
	subAddr |= TCS34725_COMMAND_BIT;
	
	TCS34725_I2C_Write(TCS34725_ADDRESS, (u8*)&subAddr, 1, 0);
	TCS34725_I2C_Read(TCS34725_ADDRESS, dataBuffer, bytesNumber, 1);
}
/*******************************************************************************
 * @brief TCS34725設(shè)置積分時間
 *
 * @return None
*******************************************************************************/
void TCS34725_SetIntegrationTime(u8 time)
{
	TCS34725_Write(TCS34725_ATIME, &time, 1);
}
/*******************************************************************************
 * @brief TCS34725設(shè)置增益
 *
 * @return None
*******************************************************************************/
void TCS34725_SetGain(u8 gain)
{
	TCS34725_Write(TCS34725_CONTROL, &gain, 1);
}
/*******************************************************************************
 * @brief TCS34725使能
 *
 * @return None
*******************************************************************************/
void TCS34725_Enable(void)
{
	u8 cmd = TCS34725_ENABLE_PON;
	
	TCS34725_Write(TCS34725_ENABLE, &cmd, 1);
	cmd = TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN;
	TCS34725_Write(TCS34725_ENABLE, &cmd, 1);
	//delay_s(600000);//delay_ms(3);//延時應(yīng)該放在設(shè)置AEN之后
}
/*******************************************************************************
 * @brief TCS34725失能
 *
 * @return None
*******************************************************************************/
void TCS34725_Disable(void)
{
	u8 cmd = 0;
	
	TCS34725_Read(TCS34725_ENABLE, &cmd, 1);
	cmd = cmd & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
	TCS34725_Write(TCS34725_ENABLE, &cmd, 1);
}
/*******************************************************************************
 * @brief TCS34725初始化
 *
 * @return ID - ID寄存器中的值
*******************************************************************************/
u8 TCS34725_Init(void)
{
	u8 id=0;
	
	TCS34725_I2C_Init(); 
	TCS34725_Read(TCS34725_ID, &id, 1);  //TCS34725 的 ID 是 0x44 可以根據(jù)這個來判斷是否成功連接,0x4D是TCS34727;
	if(id==0x4D | id==0x44)
		{
			TCS34725_SetIntegrationTime(TCS34725_INTEGRATIONTIME_50MS);
			TCS34725_SetGain(TCS34725_GAIN_1X);
			TCS34725_Enable();
			return 1;
		}
	return 0;
}
/*******************************************************************************
 * @brief TCS34725獲取單個通道數(shù)據(jù)
 *
 * @return data - 該通道的轉(zhuǎn)換值
*******************************************************************************/
u16 TCS34725_GetChannelData(u8 reg)
{
	u8 tmp[2] = {0,0};
	u16 data;
	
	TCS34725_Read(reg, tmp, 2);
	data = (tmp[1] << 8) | tmp[0];
	
	return data;
}
/*******************************************************************************
 * @brief TCS34725獲取各個通道數(shù)據(jù)
 *
 * @return 1 - 轉(zhuǎn)換完成,數(shù)據(jù)可用
 *   	   0 - 轉(zhuǎn)換未完成,數(shù)據(jù)不可用
*******************************************************************************/
u8 TCS34725_GetRawData(COLOR_RGBC *rgbc)
{
	u8 status = TCS34725_STATUS_AVALID;
	
	TCS34725_Read(TCS34725_STATUS, &status, 1);
	
	if(status & TCS34725_STATUS_AVALID)
	{
		rgbc->c = TCS34725_GetChannelData(TCS34725_CDATAL);	
		rgbc->r = TCS34725_GetChannelData(TCS34725_RDATAL);	
		rgbc->g = TCS34725_GetChannelData(TCS34725_GDATAL);	
		rgbc->b = TCS34725_GetChannelData(TCS34725_BDATAL);
		return 1;
	}
	return 0;
}
/******************************************************************************/
//RGB轉(zhuǎn)HSL
void RGBtoHSL(COLOR_RGBC *Rgb, COLOR_HSL *Hsl)
{
	u8 maxVal,minVal,difVal;
	u8 r = Rgb->r*100/Rgb->c;   //[0-100]
	u8 g = Rgb->g*100/Rgb->c;
	u8 b = Rgb->b*100/Rgb->c;
	
	maxVal = max3v(r,g,b);
	minVal = min3v(r,g,b);
	difVal = maxVal-minVal;
	
	//計(jì)算亮度
	Hsl->l = (maxVal+minVal)/2;   //[0-100]
	
	if(maxVal == minVal)//若r=g=b,灰度
	{
		Hsl->h = 0; 
		Hsl->s = 0;
	}
	else
	{
		//計(jì)算色調(diào)
		if(maxVal==r)
		{
			if(g>=b)
				Hsl->h = 60*(g-b)/difVal;
			else
				Hsl->h = 60*(g-b)/difVal+360;
		}
		else
			{
				if(maxVal==g)Hsl->h = 60*(b-r)/difVal+120;
				else
					if(maxVal==b)Hsl->h = 60*(r-g)/difVal+240;
			}
		
		//計(jì)算飽和度
		if(Hsl->l<=50)Hsl->s=difVal*100/(maxVal+minVal);  //[0-100]
		else
			Hsl->s=difVal*100/(200-(maxVal+minVal));
	}
}
/******************************************************************************/



?五、參考

TCS34725顏色感應(yīng)識別模塊icon-default.png?t=N7T8https://blog.csdn.net/qq_42250136/article/details/125705450?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171695366516800227437485%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=171695366516800227437485&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-2-125705450-null-null.142%5Ev100%5Epc_search_result_base4&utm_term=TCS34725&spm=1018.2226.3001.4187

聯(lián)系方式 微信號:13648103287

  • 聯(lián)系方式.docx

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風(fēng)險等級 參考價格 更多信息
A1260LLHLT-T 1 Allegro MicroSystems LLC Hall Effect Sensor, -5mT Min, 5mT Max, 30-60mA, BICMOS, Plastic/epoxy, Rectangular, 3 Pin, Surface Mount, SOT-23W, 3 PIN

ECAD模型

下載ECAD模型
$4.42 查看
TMP112AIDRLT 1 Texas Instruments &plusmn;0.5&deg;C 1.4V to 3.6V digital temperature sensor with I2C/SMBus in 2.56mm2 package 6-SOT-5X3 -40 to 125

ECAD模型

下載ECAD模型
$3.68 查看
LM335Z 1 STMicroelectronics Precision Temperature Sensor

ECAD模型

下載ECAD模型
$0.3 查看

相關(guān)推薦

電子產(chǎn)業(yè)圖譜

方案定制,程序設(shè)計(jì)方案、單片機(jī)程序設(shè)計(jì)與講解、APP定制開發(fā)。本公眾號致力于向讀者傳遞關(guān)于程序設(shè)計(jì)和開發(fā)的相關(guān)知識,并分享一些關(guān)于軟件開發(fā)的最佳實(shí)踐。如果您有什么問題或建議,請隨時聯(lián)系我們。我們將竭誠為您服務(wù)