CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Driver for TCS34725 Color Sensor

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
starfire151



Joined: 01 Apr 2007
Posts: 215

View user's profile Send private message

Driver for TCS34725 Color Sensor
PostPosted: Tue Nov 17, 2026 10:02 am     Reply with quote

I found a driver for the Taos TCS34725 Color Sensor from a Google search. ...I know...

Using Compiler 5.071 on a Windows 11 Pro platform

In looking through the code, I couldn't see anything wrong with it but when I attempt to compile it, I see the following errors:

*** Error 81 "D:\projects\picc\Drivers\tcs34725.h" Line 59(6,22): Expecting a basic type
*** Error 81 "D:\projects\picc\Drivers\tcs34725.h" Line 59(26,27): Expecting a basic type
*** Error 81 "..\..\Drivers\tcs34725.c" Line 24(6,22): Expecting a basic type
*** Error 81 "..\..\Drivers\tcs34725.c" Line 24(26,27): Expecting a basic type
*** Error 28 "..\..\Drivers\tcs34725.c" Line 25(21,37): Expecting an identifier
*** Error 43 "..\..\Drivers\tcs34725.c" Line 25(38,58): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 25(57,58): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 25(58,59): Expecting a declaration
*** Error 28 "..\..\Drivers\tcs34725.c" Line 26(14,15): Expecting an identifier
*** Error 43 "..\..\Drivers\tcs34725.c" Line 26(16,17): Expecting a declaration
*** Error 28 "..\..\Drivers\tcs34725.c" Line 27(21,37): Expecting an identifier
*** Error 43 "..\..\Drivers\tcs34725.c" Line 27(38,58): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 27(58,59): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 27(60,80): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 27(79,80): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 27(80,81): Expecting a declaration
*** Error 43 "..\..\Drivers\tcs34725.c" Line 28(1,2): Expecting a declaration
*** Error 58 "..\..\Drivers\tcs34725.c" Line 32(21,37): Expecting a close paren
*** Error 58 "..\..\Drivers\tcs34725.c" Line 36(21,36): Expecting a close paren
*** Error 58 "..\..\Drivers\tcs34725.c" Line 40(21,38): Expecting a close paren
*** Error 76 "..\..\Drivers\tcs34725.c" Line 53(20,21): Expect ;
21 Errors, 0 Warnings.
Build Failed.

The TCS34725.h file
Code:

#ifndef TCS34725_H
#define TCS34725_H

// I2C Addresses (7-bit address 0x29 shifted left by 1)
#define TCS34725_ADDR_WRITE    0x52
#define TCS34725_ADDR_READ     0x53

// Command Register Bits
#define TCS34725_COMMAND_BIT   0x80
#define TCS34725_AUTO_INC      0xA0  // Auto-increment protocol transaction

// TCS34725 Register Map
#define TCS34725_ENABLE        0x00  // Enables states and interrupts
#define TCS34725_ATIME         0x01  // RGBC time
#define TCS34725_WTIME         0x03  // Wait time
#define TCS34725_CONFIG        0x0D  // Configuration
#define TCS34725_CONTROL       0x0F  // Set gain level for sensor
#define TCS34725_ID            0x12  // 0x44 = TCS34721/TCS34725, 0x4D = TCS34723/TCS34727
#define TCS34725_STATUS        0x13  // Device status
#define TCS34725_CDATAL        0x14  // Clear channel data low byte
#define TCS34725_CDATAH        0x15  // Clear channel data high byte
#define TCS34725_RDATAL        0x16  // Red channel data low byte
#define TCS34725_RDATAH        0x17  // Red channel data high byte
#define TCS34725_GDATAL        0x18  // Green channel data low byte
#define TCS34725_GDATAH        0x19  // Green channel data high byte
#define TCS34725_BDATAL        0x1A  // Blue channel data low byte
#define TCS34725_BDATAH        0x1B  // Blue channel data high byte

// ENABLE Register Bits
#define TCS34725_ENABLE_PON    0x01  // Power ON
#define TCS34725_ENABLE_AEN    0x02  // RGBC Enable

// Integration Time Settings
#define TCS34725_IT_2_4MS      0xFF  // 2.4ms - 1 cycle    - Max Count: 1024
#define TCS34725_IT_24MS       0xF6  // 24ms  - 10 cycles  - Max Count: 10240
#define TCS34725_IT_50MS       0xEB  // 50ms  - 20 cycles  - Max Count: 20480
#define TCS34725_IT_101MS      0xD5  // 101ms - 42 cycles  - Max Count: 43008
#define TCS34725_IT_154MS      0xC0  // 154ms - 64 cycles  - Max Count: 65535
#define TCS34725_IT_700MS      0x00  // 700ms - 256 cycles - Max Count: 65535

// Gain Settings
#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

// Data Structure for Color Readings
typedef struct {
    unsigned int16 c;
    unsigned int16 r;
    unsigned int16 g;
    unsigned int16 b;
} tcs34725_rgbc_t;

// Function Prototypes
void tcs34725_write8(unsigned int8 reg, unsigned int8 value);
unsigned int8 tcs34725_read8(unsigned int8 reg);
int1 tcs34725_init(unsigned int8 integration_time, unsigned int8 gain);
void tcs34725_enable(void);
void tcs34725_disable(void);
void tcs34725_set_integration_time(unsigned int8 it);
void tcs34725_set_gain(unsigned int8 gain);
int1 tcs34725_data_ready(void);
void tcs34725_get_raw_data(tcs34725_rgbc_t *data);
unsigned int16 tcs34725_calculate_color_temp(tcs34725_rgbc_t *data);
unsigned int16 tcs34725_calculate_lux(tcs34725_rgbc_t *data);

#endif


The TCS34725.c file:
Code:

#include "tcs34725.h"
#include <math.h>

void tcs34725_write8(unsigned int8 reg, unsigned int8 value) {
    i2c_start();
    i2c_write(TCS34725_ADDR_WRITE);
    i2c_write(TCS34725_COMMAND_BIT | reg);
    i2c_write(value);
    i2c_stop();
}

unsigned int8 tcs34725_read8(unsigned int8 reg) {
    unsigned int8 val;
    i2c_start();
    i2c_write(TCS34725_ADDR_WRITE);
    i2c_write(TCS34725_COMMAND_BIT | reg);
    i2c_start(); // Repeated start
    i2c_write(TCS34725_ADDR_READ);
    val = i2c_read(0); // NACK after single byte
    i2c_stop();
    return val;
}

void tcs34725_enable(void) {
    tcs34725_write8(TCS34725_ENABLE, TCS34725_ENABLE_PON);
    delay_ms(3); // Datasheet requires >= 2.4ms before enabling RGBC
    tcs34725_write8(TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
}

void tcs34725_disable(void) {
    unsigned int8 reg = tcs34725_read8(TCS34725_ENABLE);
    tcs34725_write8(TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
}

void tcs34725_set_integration_time(unsigned int8 it) {
    tcs34725_write8(TCS34725_ATIME, it);
}

void tcs34725_set_gain(unsigned int8 gain) {
    tcs34725_write8(TCS34725_CONTROL, gain);
}

int1 tcs34725_init(unsigned int8 integration_time, unsigned int8 gain) {
    unsigned int8 id = tcs34725_read8(TCS34725_ID);
   
    // Verify chip ID (0x44 = TCS34725, 0x4D = TCS34727)
    if ((id != 0x44) && (id != 0x4D)) {
        return FALSE;
    }

    tcs34725_set_integration_time(integration_time);
    tcs34725_set_gain(gain);
    tcs34725_enable();

    return TRUE;
}

int1 tcs34725_data_ready(void) {
    // Bit 0 (AVALID) of STATUS register indicates RGBC cycle completed
    return (tcs34725_read8(TCS34725_STATUS) & 0x01);
}

void tcs34725_get_raw_data(tcs34725_rgbc_t *data) {
    unsigned int8 c_l, c_h, r_l, r_h, g_l, g_h, b_l, b_h;

    i2c_start();
    i2c_write(TCS34725_ADDR_WRITE);
    i2c_write(TCS34725_AUTO_INC | TCS34725_CDATAL);
    i2c_start(); // Repeated start
    i2c_write(TCS34725_ADDR_READ);

    c_l = i2c_read(1);
    c_h = i2c_read(1);
    r_l = i2c_read(1);
    r_h = i2c_read(1);
    g_l = i2c_read(1);
    g_h = i2c_read(1);
    b_l = i2c_read(1);
    b_h = i2c_read(0); // NACK on final byte
    i2c_stop();

    data->c = make16(c_h, c_l);
    data->r = make16(r_h, r_l);
    data->g = make16(g_h, g_l);
    data->b = make16(b_h, b_l);
}

// Optional: McCamy's formula for Correlated Color Temperature (Kelvin)
unsigned int16 tcs34725_calculate_color_temp(tcs34725_rgbc_t *data) {
    float X, Y, Z, xc, yc, n, cct;

    if (data->c == 0) return 0;

    // Map RGB to CIE XYZ tristimulus values
    X = (-0.14282 * data->r) + (1.54924 * data->g) + (-0.95641 * data->b);
    Y = (-0.32466 * data->r) + (1.57837 * data->g) + (-0.73191 * data->b);
    Z = (-0.68202 * data->r) + (0.77073 * data->g) + (0.56332 * data->b);

    if ((X + Y + Z) == 0.0) return 0;

    xc = X / (X + Y + Z);
    yc = Y / (X + Y + Z);

    n = (xc - 0.3320) / (0.1858 - yc);
    cct = (449.0 * pow(n, 3)) + (3525.0 * pow(n, 2)) + (6823.3 * n) + 5520.33;

    return (unsigned int16)cct;
}

// Optional: Illuminance calculation (Lux)
unsigned int16 tcs34725_calculate_lux(tcs34725_rgbc_t *data) {
    float illuminance;
    illuminance = (-0.32466 * data->r) + (1.57837 * data->g) + (-0.73191 * data->b);
    if (illuminance < 0.0) return 0;
    return (unsigned int16)illuminance;
}


The example file for using the device:
Code:

#include <16LF18326.h>
#fuses NOWDT, NOBROWNOUT, NOLVP, PUT
#use delay(internal=16MHz)

// --- Peripheral Pin Select (PPS) Setup ---
// Hardware I2C1 on RC0 (SCL) and RC1 (SDA)
#pin_select SCL1OUT = PIN_C0
#pin_select SDA1OUT = PIN_C1
#use i2c(Master, Fast=100000, sda=PIN_C1, scl=PIN_C0, force_hw)

// Optional: Hardware UART1 on RC4 (TX) and RC5 (RX) for debugging
#pin_select U1TX = PIN_C4
#pin_select U1RX = PIN_C5
#use rs232(baud=9600, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8)

#include "tcs34725.c"

void main() {
    tcs34725_rgbc_t color;
    unsigned int16 lux, temp;

    // Disable analog functions on all pins so digital I2C/UART work properly
    setup_adc_ports(NO_ANALOGS);
    delay_ms(50);

    printf("\r\nInitializing TCS34725...\r\n");

    if (tcs34725_init(TCS34725_IT_50MS, TCS34725_GAIN_4X)) {
        printf("TCS34725 Detected!\r\n");
        delay_ms(60); // Wait for first integration cycle (50ms) to complete

        while (TRUE) {
            if (tcs34725_data_ready()) {
                tcs34725_get_raw_data(&color);
                lux = tcs34725_calculate_lux(&color);
                temp = tcs34725_calculate_color_temp(&color);

                printf("C:%Lu R:%Lu G:%Lu B:%Lu | Lux:%Lu | Temp:%LuK\r\n",
                       color.c, color.r, color.g, color.b, lux, temp);
            }
            delay_ms(250);
        }
    } else {
        printf("Error: TCS34725 not found. Check wiring and pull-ups.\r\n");
        while (TRUE);
    }
}


It doesn't recognize the void as a basic type? I looked at all the .h and .c file lines and didn't see any semicolon omissions or other formatting errors. I could have overlooked something, I guess.

Any ideas?
Ttelmah



Joined: 11 Mar 2010
Posts: 20156

View user's profile Send private message

PostPosted: Wed Nov 18, 2026 7:40 am     Reply with quote

First thing, change but your #use statements. Look at the sticky at the
top of the forum.
You set the pins with the #pin_select, and then just use the port
name/number in the #use. Don't use the pins again or the force_hw.
Modern compilers will probably 'get away' with setting up the #use like
this, but your compiler is old enough that it will have problems with the way
you are setting these up.

Then unfortunately, CCS has an awful behaviour sometimes on an error.
It will plough on, and not flag the error, instead flagging something many
lines later as being the fault. So it is probable that there is some tiny
syntax problem before the point where it errors that is actually the issue.
It may be something very silly. I'd open each of the files in a text editor,
and deliberately put the cursor at the very top of the file, and do a delete.
and then do the same at the very bottom of the file. Try to advance right
with the cursor keys and then use the backspace to back up to the last
character in the test, and re-save. I have seen this type of error, where
there was a hidden character accidentally included in a downloaded file
at the start or end.
starfire151



Joined: 01 Apr 2007
Posts: 215

View user's profile Send private message

found the problem
PostPosted: Wed Nov 18, 2026 9:21 am     Reply with quote

Thanks for responding.

I managed to find the problem. ...as odd as it was...

I renamed the function tcs34725_enable(void) to tcs34725_en(void) in the .c and the .h files (functions and prototypes and the call in the tcs34725_init() routine. It compiled without errors

I changed the tcs34725_disable(void) function to tcs34725_dis(void), also, just to be consistent, although the compiler didn't complain about that.

Maybe "tcs34725_enable" is a reserved word?

Thanks, also, for the insight into the #pin_select usage. I will correct that.
jeremiah



Joined: 20 Jul 2010
Posts: 1425

View user's profile Send private message

Re: found the problem
PostPosted: Thu Nov 19, 2026 8:42 am     Reply with quote

starfire151 wrote:
Thanks for responding.

I managed to find the problem. ...as odd as it was...

I renamed the function tcs34725_enable(void) to tcs34725_en(void) in the .c and the .h files (functions and prototypes and the call in the tcs34725_init() routine. It compiled without errors

I changed the tcs34725_disable(void) function to tcs34725_dis(void), also, just to be consistent, although the compiler didn't complain about that.

Maybe "tcs34725_enable" is a reserved word?

Thanks, also, for the insight into the #pin_select usage. I will correct that.


It's because you have a #define of the same name. This is causing a conflict as the function names are now being replaced with 0x00 so you see:

void tcs34725_enable(void);

but the compiler sees

void 0x00(void);

which is invalid C syntax

You use different casing, but the CCS compiler by default is case insensitive. You have to add the #case directive at the beginning to make it act case sensitive.
starfire151



Joined: 01 Apr 2007
Posts: 215

View user's profile Send private message

PostPosted: Thu Nov 19, 2026 9:43 am     Reply with quote

Thanks for that catch!

That explains why changing the function name was all it took. I guess I was under the impression that C was (by default) case sensitive.

I'll make sure to include the #case line to avoid this kind of issue in the future.

Thanks, again.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group