模块样子如下:
模块号称使用ZC-CLS381芯片识别颜色,但其实不是(其实是TCS34721/TCS34725)
前期找来了ZC-CLS381芯片的手册
连模块地址都不一样。。。。
经过波折终于有以下程序 ADDR 0x29
#include"i2c.h"
#include "SEGGER_RTT.h"
typedef enum {
TCS34725_INTEGRATIONTIME_2_4MS = 0xFF, ///< 2.4ms - 1 cycle - Max Count: 1024
TCS34725_INTEGRATIONTIME_24MS = 0xF6, ///< 24ms - 10 cycles - Max Count: 10240
TCS34725_INTEGRATIONTIME_50MS = 0xEB, ///< 50ms - 20 cycles - Max Count: 20480
TCS34725_INTEGRATIONTIME_101MS = 0xD5, ///< 101ms - 42 cycles - Max Count: 43008
TCS34725_INTEGRATIONTIME_154MS = 0xC0, ///< 154ms - 64 cycles - Max Count: 65535
TCS34725_INTEGRATIONTIME_700MS = 0x00 ///< 700ms - 256 cycles - Max Count: 65535
}
tcs34725IntegrationTime_t;
typedef enum {
TCS34725_GAIN_1X = 0x00, ///< No gain
TCS34725_GAIN_4X = 0x01, ///< 4x gain
TCS34725_GAIN_16X = 0x02, ///< 16x gain
TCS34725_GAIN_60X = 0x03 ///< 60x gain
}
tcs34725Gain_t;
#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)
uint32_t CS_DATA_IR = 0, CS_DATA_GREEN = 0, CS_DATA_RED = 0, CS_DATA_BLUE = 0;
uint8_t mregister[] = {TCS34725_CDATAL | 0x80, TCS34725_CDATAH | 0x80, TCS34725_RDATAL | 0x80, TCS34725_RDATAH | 0x80, TCS34725_GDATAL | 0x80, TCS34725_GDATAH | 0x80, TCS34725_BDATAL | 0x80, TCS34725_BDATAH | 0x80};
uint8_t recvbuff[64] = {0};
uint8_t mysendbuf[256] = {0};
int main() {
uint32_t j = 0;
SEGGER_RTT_Init();
Si2c_initial();
MY_Si2c_unlockbus(0);
recvbuff[0] = 0x12 | 0x80;
MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &recvbuff[0], 1, &recvbuff[1], 1);//0x44 TCS34721/TCS34725
while(1) {
recvbuff[0] = 0x01 | 0x80; //采样时间
recvbuff[1] = TCS34725_INTEGRATIONTIME_154MS;
MY_Si2c_Multibyte_sending( 0, MY_SI2C_ADDR1, recvbuff, 2);
recvbuff[0] = 0x0F | 0x80; //放大倍数
recvbuff[1] = TCS34725_GAIN_16X;
MY_Si2c_Multibyte_sending( 0, MY_SI2C_ADDR1, recvbuff, 2);
recvbuff[0] = 0x00 | 0x80; //开始采样
recvbuff[1] = 0x01;
MY_Si2c_Multibyte_sending( 0, MY_SI2C_ADDR1, recvbuff, 2);
for(j = 0; j < 20000; j++);
recvbuff[0] = 0x00 | 0x80;
recvbuff[1] = 0x01 | 0x02;
MY_Si2c_Multibyte_sending( 0, MY_SI2C_ADDR1, recvbuff, 2);
MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[0], 1, &recvbuff[0], 2);
//MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[1], 1, &recvbuff[1], 1);
CS_DATA_IR = recvbuff[1] << 8 | recvbuff[0];
MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[2], 1, &recvbuff[0], 2);
//MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[3], 1, &recvbuff[1], 1);
CS_DATA_RED = recvbuff[1] << 8 | recvbuff[0];
MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[4], 1, &recvbuff[0], 2);
//MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[5], 1, &recvbuff[1], 1);
CS_DATA_GREEN = recvbuff[1] << 8 | recvbuff[0];
MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[6], 1, &recvbuff[0], 2);
//MY_Si2c_writeandread( 0, MY_SI2C_ADDR1, &mregister[7], 1, &recvbuff[1], 1);
CS_DATA_BLUE = recvbuff[1] << 8 | recvbuff[0];
for(j = 0; j < 5000000; j++);//不同的采样要有不同的延时
sprintf(mysendbuf, "CS_DATA_IR:%d CS_DATA_RED:%d CS_DATA_GREEN:%d CS_DATA_BLUE:%d\r\n", CS_DATA_IR, CS_DATA_RED, CS_DATA_GREEN, CS_DATA_BLUE);
SEGGER_RTT_printf(0, "%s", mysendbuf);
}
return 0;
}
发出开始转换指令后一定要第一时间读取颜色寄存器值,这里推测读取的是上一次的颜色值,不读取的话新转换的值更新不进去,然后就是读取完之后,一定要有足够的时间等待转换结束,否则数值不更新