![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
AndresRam
Joined: 27 May 2017 Posts: 16 Location: Colombia
|
Help with migration code from Arduino |
Posted: Sun May 28, 2017 8:06 pm |
|
|
I have been a user of Arduino and Pic microcontroller with CCS compiller. On this occasion I would like a help trying to migrate the following syntax to CCS language (The original syntax is from Arduino):
I've been trying to achieve the equivalent for a long time but I have not been able.
Thanks
Code: | typedef enum
{
PIEN_NOT_ASSERT = TMD37821_REG_COLORPROX_ENABLE_PIEN_NOT_ASSERT,
PIEN_ASSERT = TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT
} tmdProximityInterrupt_t;
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 28, 2017 9:21 pm |
|
|
Show us the code where PIEN_NOT_ASSERT and the other 3 items are declared.
I couldn't find PIEN_NOT_ASSERT with Google, so if you got this code
off the net, then post a link to it. Or if you wrote it, then tell us that.
Also post your PIC and your CCS compiler version. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19385
|
|
Posted: Mon May 29, 2017 12:37 am |
|
|
An enum, is very similar to a #define. It generates named symbolic 'values' that can be assigned to a variable. Each value is an integer constant. It's differences to a #define are that it can be syntactically checked, and are handled at run time, while a #define is done at compile time. The resulting variable is also therefore something that can be viewed/located in a debugger, where a #define cannot.
So the code is generating an enum 'type', with two values 'PIEN_NOT_ASSERT', and 'PIEN_ASSERT', whose numeric values, are TMD37821_REG_COLORPROX_ENABLE_PIEN_NOT_ASSERT, and TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT respectively. Now given that the PEN bit is bit 2 of the enable register on the chip, I'd expect the values to be perhaps 0x0, and 0x02 respectively. However this is going to depend on how the value is used in the rest of the code.
What is posted, should be directly accepted by CCS C, provided the TMD values are already #defined. The only 'caveat' would be if the values are small enough to fit in an int8. CCS by default uses a type large enough to hold the largest value given for an enum. So if you declare an enum to contain two values 0, and 0x2, an int8 will be used. However the smallest type that will be used on the Arduino, will be an int16. CCS will now create int16 (and even int32) enum's, but only if the values stored need these sizes. This would only have an effect if the values are used later, and are assumed to be int16, where at the point of use, the value would need to be cast to an int16, if this does matter. |
|
![](templates/subSilver/images/spacer.gif) |
AndresRam
Joined: 27 May 2017 Posts: 16 Location: Colombia
|
|
Posted: Mon May 29, 2017 1:27 am |
|
|
Thank you for your quick answers.
To respond to PCM programmer:
I'm doing the driver for the sensor TMD37821 (with 18f47j53):
Code: | /**************************************************************************
I2C ADDRESS/BITS
**************************************************************************/
#define TMD37821_DEFAULT_ADDRESS (0x39)
#define TMD37821_COLORPROX_DEV_ID (0x30)
/**************************************************************************
CONVERSION DELAY (in mS)
**************************************************************************/
#define TMD37821_CONVERSIONDELAY (100)
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE REGISTERS
**************************************************************************/
#define TMD37821_REG_COLORPROX_COMMAND // Specifies Register Address
#define TMD37821_REG_COLORPROX_ENABLE (0x00) // Enables States and Interrupts Register
#define TMD37821_REG_COLORPROX_ATIME (0x01) // RGBC Integration Time Register
#define TMD37821_REG_COLORPROX_WTIME (0x03) // Wait Time Register
#define TMD37821_REG_COLORPROX_AILTL (0x04) // Clear Interrupt Low Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_AILTH (0x05) // Clear Interrupt Low Threshold High Byte Register
#define TMD37821_REG_COLORPROX_AIHTL (0x06) // Clear Interrupt High Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_AIHTH (0x07) // Clear Interrupt High Threshold High Byte Register
#define TMD37821_REG_COLORPROX_PILTL (0x08) // Proximity Interrupt Low Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_PILTH (0x09) // Proximity Interrupt Low Threshold High Byte Register
#define TMD37821_REG_COLORPROX_PIHTL (0x0A) // Proximity Interrupt High Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_PIHTH (0x0B) // Proximity Interrupt High Threshold High Byte Register
#define TMD37821_REG_COLORPROX_PERS (0x0C) // Interrupt Persistence Filters Register
#define TMD37821_REG_COLORPROX_CONFIG (0x0D) // Configuration Register
#define TMD37821_REG_COLORPROX_PPULSE (0x0E) // Proximity Pulse Count Register
#define TMD37821_REG_COLORPROX_CONTROL (0x0F) // Control Register
#define TMD37821_REG_COLORPROX_REVISION (0x11) // Die Revision Number Register
#define TMD37821_REG_COLORPROX_ID (0x12) // Device ID Register
#define TMD37821_REG_COLORPROX_STATUS (0x13) // Device Status Register
#define TMD37821_REG_COLORPROX_CDATAL (0x14) // Clear ADC Low Data Register
#define TMD37821_REG_COLORPROX_CDATAH (0x15) // Clear ADC High Data Register
#define TMD37821_REG_COLORPROX_RDATAL (0x16) // Red ADC Low Data Register
#define TMD37821_REG_COLORPROX_RDATAH (0x17) // Red ADC High Data Register
#define TMD37821_REG_COLORPROX_GDATAL (0x18) // Green ADC Low Data Register
#define TMD37821_REG_COLORPROX_GDATAH (0x19) // Green ADC High Data Register
#define TMD37821_REG_COLORPROX_BDATAL (0x1A) // Blue ADC Low Data Register
#define TMD37821_REG_COLORPROX_BDATAH (0x1B) // Blue ADC High Data Register
#define TMD37821_REG_COLORPROX_PDATAL (0x1C) // Proximity ADC Low Data Register
#define TMD37821_REG_COLORPROX_PDATAH (0x1D) // Proximity ADC High Data Register
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE COMMAND REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_CMD_MASK (0x80) // Select Command Register
#define TMD37821_REG_COLORPROX_CMD_SELECT (0x80) // Addresses COMMAND Register
#define TMD37821_REG_COLORPROX_CMD_TYPE_MASK (0x60) // Selects Type of Transaction to Follow in Subsequent Data Transfers
#define TMD37821_REG_COLORPROX_CMD_TYPE_REP_BYTE (0x00) // Repeated Byte Protocol Transaction
#define TMD37821_REG_COLORPROX_CMD_TYPE_AUTO_INCR (0x20) // Auto-Increment Protocol Transaction
#define TMD37821_REG_COLORPROX_CMD_TYPE_SPECIAL (0x60) // Special Function
#define TMD37821_REG_COLORPROX_CMD_ADD_MASK (0x1F) // Specifies a Special Function Command
#define TMD37821_REG_COLORPROX_CMD_ADD_NORMAL (0x00) // Normal — No Action
#define TMD37821_REG_COLORPROX_CMD_ADD_PROX_INTR (0x05) // Proximity Interrupt Clear
#define TMD37821_REG_COLORPROX_CMD_ADD_CLEAR_INTR (0x06) // Clear Interrupt Clear
#define TMD37821_REG_COLORPROX_CMD_ADD_PROX_CLEAR_INTR (0x07) // Proximity and Clear Interrupt Clear
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE ENABLE REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_MASK (0x20) // Proximity Interrupt Mask
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_NOT_ASSERT (0x00) // Not Asserted, Did Not Permits Proximity Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT (0x20) // Asserted, Permits Proximity Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_MASK (0x10) // Ambient Light Sensing (ALS) Interrupt Mask
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_NOT_ASSERT (0x00) // Not Asserted, Did Not Permits Ambient Light Sensing (ALS) Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_ASSERT (0x10) // Asserted, Permits Ambient Light Sensing (ALS) Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_WEN_MASK (0x08) // Wait Enable
#define TMD37821_REG_COLORPROX_ENABLE_WEN_DISABLE (0x00) // Disables the Wait Timer
#define TMD37821_REG_COLORPROX_ENABLE_WEN_ENABLE (0x08) // Enables the Wait Timer
#define TMD37821_REG_COLORPROX_ENABLE_PEN_MASK (0x04) // Proximity Enable
#define TMD37821_REG_COLORPROX_ENABLE_PEN_DISABLE (0x00) // Deactivates the Proximity
#define TMD37821_REG_COLORPROX_ENABLE_PEN_ENABLE (0x04) // Activates the Proximity
#define TMD37821_REG_COLORPROX_ENABLE_AEN_MASK (0x02) // 4-Channel RGBC ADC Enable
#define TMD37821_REG_COLORPROX_ENABLE_AEN_DISABLE (0x00) // Disables the RGBC ADC
#define TMD37821_REG_COLORPROX_ENABLE_AEN_ENABLE (0x02) // Activates the RGBC ADC
#define TMD37821_REG_COLORPROX_ENABLE_PON_MASK (0x01) // Power ON
#define TMD37821_REG_COLORPROX_ENABLE_PON_DISABLE (0x00) // Disables the Internal Oscillator to Permit the Timers and ADC Channels to Operate
#define TMD37821_REG_COLORPROX_ENABLE_PON_ENABLE (0x01) // Activates the Internal Oscillator to Permit the Timers and ADC Channels to Operate
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE ATIME REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_ATIME_MASK (0xFF) // Controls the Internal Integration Time of the RGBC Channel ADC's in 2.38 ms Increments
#define TMD37821_REG_COLORPROX_ATIME_2_38MS (0xFF) // Cycles: 1, Time: 2.38 ms Max Count: 1024
#define TMD37821_REG_COLORPROX_ATIME_24MS (0xF6) // Cycles: 10, Time: 24 ms Max Count: 10240
#define TMD37821_REG_COLORPROX_ATIME_100MS (0xDB) // Cycles: 37, Time: 100 ms Max Count: 43008
#define TMD37821_REG_COLORPROX_ATIME_152MS (0xC0) // Cycles: 64, Time: 152 ms Max Count: 65535
#define TMD37821_REG_COLORPROX_ATIME_609MS (0x00) // Cycles: 256, Time: 609 ms Max Count: 65535
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE WTIME REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_WTIME_MASK (0xFF) // Wait Time is Set in 2.38 ms Increments Unless the WLONG Bit is Asserted in Which Case the wait Times are 12x Longer
#define TMD37821_REG_COLORPROX_WTIME_1 (0xFF) // Wait Time: 1, Time (WLONG = 0): 2.38 ms Time (WLONG = 1): 0.03 sec
#define TMD37821_REG_COLORPROX_WTIME_85 (0xB6) // Wait Time: 85, Time (WLONG = 0): 202 ms Time (WLONG = 1): 2.43 sec
#define TMD37821_REG_COLORPROX_WTIME_256 (0x00) // Wait Time: 256, Time (WLONG = 0): 609 ms Time (WLONG = 1): 7.31 sec
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE CONFIGURATION REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_CONFIG_WLONG_MASK (0x02) // Wait Long
#define TMD37821_REG_COLORPROX_CONFIG_WLONG_NOT_ASSERT (0x00) // Not Asserted, the Wait Cycles are Not Increased by a Factor 12X from that Programmed in the WTIME Register
#define TMD37821_REG_COLORPROX_CONFIG_WLONG_ASSERT (0x02) // Asserted, the Wait Cycles are Increased by a Factor 12X from that Programmed in the WTIME Register
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE CONTROL REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_MASK (0xC0) // Proximity LED Drive Strength
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_100 (0x00) // LED Strength – PDL=0: 100%
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_50 (0x40) // LED Strength – PDL=0: 50%
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_25 (0x80) // LED Strength – PDL=0: 25%
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_12_5 (0xC0) // LED Strength – PDL=0: 12.5%
#define TMD37821_REG_COLORPROX_CONTROL_PSAT_MASK (0x10) // PDATA Output
#define TMD37821_REG_COLORPROX_CONTROL_PSAT_0 (0x00) // PDATA Output Regardless of Ambient Light Level
#define TMD37821_REG_COLORPROX_CONTROL_PSAT_1 (0x10) // PDATA Output Equal to Dark Current Value if Saturated
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_MASK (0x03) // RGBC Gain Control
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_1X (0x00) // 1X Gain
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_4X (0x01) // 4X Gain
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_16X (0x02) // 16X Gain
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_60X (0x03) // 60X Gain
typedef enum
{
COMMAND_SELECT = TMD37821_REG_COLORPROX_CMD_SELECT,
COMMAND_REP_BYTE = TMD37821_REG_COLORPROX_CMD_TYPE_REP_BYTE,
COMMAND_REP_AUTO_INCR = TMD37821_REG_COLORPROX_CMD_TYPE_AUTO_INCR,
COMMAND_REP_SPECIAL = TMD37821_REG_COLORPROX_CMD_TYPE_SPECIAL,
COMMAND_ADD_NORMAL = TMD37821_REG_COLORPROX_CMD_ADD_NORMAL,
COMMAND_ADD_PROX_INTR = TMD37821_REG_COLORPROX_CMD_ADD_PROX_INTR,
COMMAND_ADD_CLEAR_INTR = TMD37821_REG_COLORPROX_CMD_ADD_CLEAR_INTR,
COMMAND_ADD_PROX_CLEAR_INTR = TMD37821_REG_COLORPROX_CMD_ADD_PROX_CLEAR_INTR
} tmdCommand_t;
typedef enum
{
PIEN_NOT_ASSERT = TMD37821_REG_COLORPROX_ENABLE_PIEN_NOT_ASSERT,
PIEN_ASSERT = TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT
} tmdProximityInterrupt_t;
typedef enum
{
AIEN_NOT_ASSERT = TMD37821_REG_COLORPROX_ENABLE_AIEN_NOT_ASSERT,
AIEN_ASSERT = TMD37821_REG_COLORPROX_ENABLE_AIEN_ASSERT
} tmdColorInterrupt_t;
typedef enum
{
WEN_DISABLE = TMD37821_REG_COLORPROX_ENABLE_WEN_DISABLE,
WEN_ENABLE = TMD37821_REG_COLORPROX_ENABLE_WEN_ENABLE
} tmdWaitEnable_t;
typedef enum
{
PEN_DISABLE = TMD37821_REG_COLORPROX_ENABLE_PEN_DISABLE,
PEN_ENABLE = TMD37821_REG_COLORPROX_ENABLE_PEN_ENABLE
} tmdProximityEnable_t;
typedef enum
{
AEN_DISABLE = TMD37821_REG_COLORPROX_ENABLE_AEN_DISABLE,
AEN_ENABLE = TMD37821_REG_COLORPROX_ENABLE_AEN_ENABLE
} tmdColorEnable_t;
typedef enum
{
PON_DISABLE = TMD37821_REG_COLORPROX_ENABLE_PON_DISABLE,
PON_ENABLE = TMD37821_REG_COLORPROX_ENABLE_PON_ENABLE
} tmdPowerEnable_t;
typedef enum
{
ATIME_2_38MS = TMD37821_REG_COLORPROX_ATIME_2_38MS,
ATIME_24MS = TMD37821_REG_COLORPROX_ATIME_24MS,
ATIME_100MS = TMD37821_REG_COLORPROX_ATIME_100MS,
ATIME_152MS = TMD37821_REG_COLORPROX_ATIME_152MS,
ATIME_609MS = TMD37821_REG_COLORPROX_ATIME_609MS
} tmdATime_t;
typedef enum
{
WTIME_1 = TMD37821_REG_COLORPROX_WTIME_1,
WTIME_85 = TMD37821_REG_COLORPROX_WTIME_85,
WTIME_256 = TMD37821_REG_COLORPROX_WTIME_256
} tmdWTime_t;
typedef enum
{
WLONG_NOT_ASSERT = TMD37821_REG_COLORPROX_CONFIG_WLONG_NOT_ASSERT,
WLONG_ASSERT = TMD37821_REG_COLORPROX_CONFIG_WLONG_ASSERT
} tmdWaitLong_t;
typedef enum
{
PDRIVE_100 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_100,
PDRIVE_50 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_50,
PDRIVE_25 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_25,
PDRIVE_12_5 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_12_5
} tmdProximityDrive_t;
typedef enum
{
PSAT_0 = TMD37821_REG_COLORPROX_CONTROL_PSAT_0,
PSAT_1 = TMD37821_REG_COLORPROX_CONTROL_PSAT_1
} tmdProximityOutput_t;
typedef enum
{
AGAIN_1X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_1X,
AGAIN_4X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_4X,
AGAIN_16X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_16X,
AGAIN_60X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_60X
} tmdColorGain_t;
typedef struct
{
uint16_t C;
uint16_t R;
uint16_t G;
uint16_t B;
uint16_t P;
} tmdSensorData_t;
// Instance-specific properties
uint8_t tmd_conversionDelay;
tmdCommand_t tmd_command;
tmdProximityInterrupt_t tmd_proximityinterrupt;
tmdColorInterrupt_t tmd_colorinterrupt;
tmdWaitEnable_t tmd_waitenable;
tmdProximityEnable_t tmd_proximityenable;
tmdColorEnable_t tmd_colorenable;
tmdPowerEnable_t tmd_powerenable;
tmdATime_t tmd_atime;
tmdWTime_t tmd_wtime;
tmdWaitLong_t tmd_waitlong;
tmdProximityDrive_t tmd_proximitydrive;
tmdProximityOutput_t tmd_proximityoutput;
tmdColorGain_t tmd_colorgain;
uint8_t tmd_i2cAddress;
tmdSensorData_t tmd_colorproxData;
void getAddr_TMD37821(uint8_t i2cAddress);
bool begin(void);
void Measure_Sensor(void);
void setUpColorProximity(void);
void Measure_Color(void);
void Measure_Proximity(void);
void setCommand(tmdCommand_t command);
tmdCommand_t getCommand(void);
void setProximityInterrupt(tmdProximityInterrupt_t proximityinterrupt);
tmdProximityInterrupt_t getProximityInterrupt(void);
void setColorInterrupt(tmdColorInterrupt_t colorinterrupt);
tmdColorInterrupt_t getColorInterrupt(void);
void setWaitEnable(tmdWaitEnable_t waitenable);
tmdWaitEnable_t getWaitEnable(void);
void setProximityEnable(tmdProximityEnable_t proximityenable);
tmdProximityEnable_t getProximityEnable(void);
void setColorEnable(tmdColorEnable_t colorenable);
tmdColorEnable_t getColorEnable(void);
void setPowerEnable(tmdPowerEnable_t powerenable);
tmdPowerEnable_t getPowerEnable(void);
void setATime(tmdATime_t atime);
tmdATime_t getATime(void);
void setWTime(tmdWTime_t wtime);
tmdWTime_t getWTime(void);
void setWaitLong(tmdWaitLong_t waitlong);
tmdWaitLong_t getWaitLong(void);
void setProximityDrive(tmdProximityDrive_t proximitydrive);
tmdProximityDrive_t getProximityDrive(void);
void setProximityOutput(tmdProximityOutput_t proximityoutput);
tmdProximityOutput_t getProximityOutput(void);
void setColorGain(tmdColorGain_t colorgain);
tmdColorGain_t getColorGain(void); |
Now for the answer given by Ttelmah:
You will notice that indeed, at the top the TMD values are already #defined.
But how to solve the "size" (int16)? Could there be a way to define from the beginning that they are of size int16 as in arduino? |
|
![](templates/subSilver/images/spacer.gif) |
AndresRam
Joined: 27 May 2017 Posts: 16 Location: Colombia
|
|
Posted: Mon May 29, 2017 1:28 am |
|
|
Thank you for your quick answers.
To respond to PCM programmer:
I'm doing the driver for the sensor TMD37821 (with 18f47j53):
Code: | /**************************************************************************
I2C ADDRESS/BITS
**************************************************************************/
#define TMD37821_DEFAULT_ADDRESS (0x39)
#define TMD37821_COLORPROX_DEV_ID (0x30)
/**************************************************************************
CONVERSION DELAY (in mS)
**************************************************************************/
#define TMD37821_CONVERSIONDELAY (100)
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE REGISTERS
**************************************************************************/
#define TMD37821_REG_COLORPROX_COMMAND // Specifies Register Address
#define TMD37821_REG_COLORPROX_ENABLE (0x00) // Enables States and Interrupts Register
#define TMD37821_REG_COLORPROX_ATIME (0x01) // RGBC Integration Time Register
#define TMD37821_REG_COLORPROX_WTIME (0x03) // Wait Time Register
#define TMD37821_REG_COLORPROX_AILTL (0x04) // Clear Interrupt Low Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_AILTH (0x05) // Clear Interrupt Low Threshold High Byte Register
#define TMD37821_REG_COLORPROX_AIHTL (0x06) // Clear Interrupt High Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_AIHTH (0x07) // Clear Interrupt High Threshold High Byte Register
#define TMD37821_REG_COLORPROX_PILTL (0x08) // Proximity Interrupt Low Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_PILTH (0x09) // Proximity Interrupt Low Threshold High Byte Register
#define TMD37821_REG_COLORPROX_PIHTL (0x0A) // Proximity Interrupt High Threshold Low Byte Register
#define TMD37821_REG_COLORPROX_PIHTH (0x0B) // Proximity Interrupt High Threshold High Byte Register
#define TMD37821_REG_COLORPROX_PERS (0x0C) // Interrupt Persistence Filters Register
#define TMD37821_REG_COLORPROX_CONFIG (0x0D) // Configuration Register
#define TMD37821_REG_COLORPROX_PPULSE (0x0E) // Proximity Pulse Count Register
#define TMD37821_REG_COLORPROX_CONTROL (0x0F) // Control Register
#define TMD37821_REG_COLORPROX_REVISION (0x11) // Die Revision Number Register
#define TMD37821_REG_COLORPROX_ID (0x12) // Device ID Register
#define TMD37821_REG_COLORPROX_STATUS (0x13) // Device Status Register
#define TMD37821_REG_COLORPROX_CDATAL (0x14) // Clear ADC Low Data Register
#define TMD37821_REG_COLORPROX_CDATAH (0x15) // Clear ADC High Data Register
#define TMD37821_REG_COLORPROX_RDATAL (0x16) // Red ADC Low Data Register
#define TMD37821_REG_COLORPROX_RDATAH (0x17) // Red ADC High Data Register
#define TMD37821_REG_COLORPROX_GDATAL (0x18) // Green ADC Low Data Register
#define TMD37821_REG_COLORPROX_GDATAH (0x19) // Green ADC High Data Register
#define TMD37821_REG_COLORPROX_BDATAL (0x1A) // Blue ADC Low Data Register
#define TMD37821_REG_COLORPROX_BDATAH (0x1B) // Blue ADC High Data Register
#define TMD37821_REG_COLORPROX_PDATAL (0x1C) // Proximity ADC Low Data Register
#define TMD37821_REG_COLORPROX_PDATAH (0x1D) // Proximity ADC High Data Register
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE COMMAND REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_CMD_MASK (0x80) // Select Command Register
#define TMD37821_REG_COLORPROX_CMD_SELECT (0x80) // Addresses COMMAND Register
#define TMD37821_REG_COLORPROX_CMD_TYPE_MASK (0x60) // Selects Type of Transaction to Follow in Subsequent Data Transfers
#define TMD37821_REG_COLORPROX_CMD_TYPE_REP_BYTE (0x00) // Repeated Byte Protocol Transaction
#define TMD37821_REG_COLORPROX_CMD_TYPE_AUTO_INCR (0x20) // Auto-Increment Protocol Transaction
#define TMD37821_REG_COLORPROX_CMD_TYPE_SPECIAL (0x60) // Special Function
#define TMD37821_REG_COLORPROX_CMD_ADD_MASK (0x1F) // Specifies a Special Function Command
#define TMD37821_REG_COLORPROX_CMD_ADD_NORMAL (0x00) // Normal — No Action
#define TMD37821_REG_COLORPROX_CMD_ADD_PROX_INTR (0x05) // Proximity Interrupt Clear
#define TMD37821_REG_COLORPROX_CMD_ADD_CLEAR_INTR (0x06) // Clear Interrupt Clear
#define TMD37821_REG_COLORPROX_CMD_ADD_PROX_CLEAR_INTR (0x07) // Proximity and Clear Interrupt Clear
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE ENABLE REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_MASK (0x20) // Proximity Interrupt Mask
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_NOT_ASSERT (0x00) // Not Asserted, Did Not Permits Proximity Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT (0x20) // Asserted, Permits Proximity Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_MASK (0x10) // Ambient Light Sensing (ALS) Interrupt Mask
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_NOT_ASSERT (0x00) // Not Asserted, Did Not Permits Ambient Light Sensing (ALS) Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_ASSERT (0x10) // Asserted, Permits Ambient Light Sensing (ALS) Interrupts to be Generated
#define TMD37821_REG_COLORPROX_ENABLE_WEN_MASK (0x08) // Wait Enable
#define TMD37821_REG_COLORPROX_ENABLE_WEN_DISABLE (0x00) // Disables the Wait Timer
#define TMD37821_REG_COLORPROX_ENABLE_WEN_ENABLE (0x08) // Enables the Wait Timer
#define TMD37821_REG_COLORPROX_ENABLE_PEN_MASK (0x04) // Proximity Enable
#define TMD37821_REG_COLORPROX_ENABLE_PEN_DISABLE (0x00) // Deactivates the Proximity
#define TMD37821_REG_COLORPROX_ENABLE_PEN_ENABLE (0x04) // Activates the Proximity
#define TMD37821_REG_COLORPROX_ENABLE_AEN_MASK (0x02) // 4-Channel RGBC ADC Enable
#define TMD37821_REG_COLORPROX_ENABLE_AEN_DISABLE (0x00) // Disables the RGBC ADC
#define TMD37821_REG_COLORPROX_ENABLE_AEN_ENABLE (0x02) // Activates the RGBC ADC
#define TMD37821_REG_COLORPROX_ENABLE_PON_MASK (0x01) // Power ON
#define TMD37821_REG_COLORPROX_ENABLE_PON_DISABLE (0x00) // Disables the Internal Oscillator to Permit the Timers and ADC Channels to Operate
#define TMD37821_REG_COLORPROX_ENABLE_PON_ENABLE (0x01) // Activates the Internal Oscillator to Permit the Timers and ADC Channels to Operate
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE ATIME REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_ATIME_MASK (0xFF) // Controls the Internal Integration Time of the RGBC Channel ADC's in 2.38 ms Increments
#define TMD37821_REG_COLORPROX_ATIME_2_38MS (0xFF) // Cycles: 1, Time: 2.38 ms Max Count: 1024
#define TMD37821_REG_COLORPROX_ATIME_24MS (0xF6) // Cycles: 10, Time: 24 ms Max Count: 10240
#define TMD37821_REG_COLORPROX_ATIME_100MS (0xDB) // Cycles: 37, Time: 100 ms Max Count: 43008
#define TMD37821_REG_COLORPROX_ATIME_152MS (0xC0) // Cycles: 64, Time: 152 ms Max Count: 65535
#define TMD37821_REG_COLORPROX_ATIME_609MS (0x00) // Cycles: 256, Time: 609 ms Max Count: 65535
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE WTIME REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_WTIME_MASK (0xFF) // Wait Time is Set in 2.38 ms Increments Unless the WLONG Bit is Asserted in Which Case the wait Times are 12x Longer
#define TMD37821_REG_COLORPROX_WTIME_1 (0xFF) // Wait Time: 1, Time (WLONG = 0): 2.38 ms Time (WLONG = 1): 0.03 sec
#define TMD37821_REG_COLORPROX_WTIME_85 (0xB6) // Wait Time: 85, Time (WLONG = 0): 202 ms Time (WLONG = 1): 2.43 sec
#define TMD37821_REG_COLORPROX_WTIME_256 (0x00) // Wait Time: 256, Time (WLONG = 0): 609 ms Time (WLONG = 1): 7.31 sec
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE CONFIGURATION REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_CONFIG_WLONG_MASK (0x02) // Wait Long
#define TMD37821_REG_COLORPROX_CONFIG_WLONG_NOT_ASSERT (0x00) // Not Asserted, the Wait Cycles are Not Increased by a Factor 12X from that Programmed in the WTIME Register
#define TMD37821_REG_COLORPROX_CONFIG_WLONG_ASSERT (0x02) // Asserted, the Wait Cycles are Increased by a Factor 12X from that Programmed in the WTIME Register
/**************************************************************************
DIGITAL COLOR, PROXIMITY MODULE CONTROL REGISTER DESCRIPTION
**************************************************************************/
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_MASK (0xC0) // Proximity LED Drive Strength
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_100 (0x00) // LED Strength – PDL=0: 100%
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_50 (0x40) // LED Strength – PDL=0: 50%
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_25 (0x80) // LED Strength – PDL=0: 25%
#define TMD37821_REG_COLORPROX_CONTROL_PDRIVE_12_5 (0xC0) // LED Strength – PDL=0: 12.5%
#define TMD37821_REG_COLORPROX_CONTROL_PSAT_MASK (0x10) // PDATA Output
#define TMD37821_REG_COLORPROX_CONTROL_PSAT_0 (0x00) // PDATA Output Regardless of Ambient Light Level
#define TMD37821_REG_COLORPROX_CONTROL_PSAT_1 (0x10) // PDATA Output Equal to Dark Current Value if Saturated
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_MASK (0x03) // RGBC Gain Control
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_1X (0x00) // 1X Gain
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_4X (0x01) // 4X Gain
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_16X (0x02) // 16X Gain
#define TMD37821_REG_COLORPROX_CONTROL_AGAIN_60X (0x03) // 60X Gain
typedef enum
{
COMMAND_SELECT = TMD37821_REG_COLORPROX_CMD_SELECT,
COMMAND_REP_BYTE = TMD37821_REG_COLORPROX_CMD_TYPE_REP_BYTE,
COMMAND_REP_AUTO_INCR = TMD37821_REG_COLORPROX_CMD_TYPE_AUTO_INCR,
COMMAND_REP_SPECIAL = TMD37821_REG_COLORPROX_CMD_TYPE_SPECIAL,
COMMAND_ADD_NORMAL = TMD37821_REG_COLORPROX_CMD_ADD_NORMAL,
COMMAND_ADD_PROX_INTR = TMD37821_REG_COLORPROX_CMD_ADD_PROX_INTR,
COMMAND_ADD_CLEAR_INTR = TMD37821_REG_COLORPROX_CMD_ADD_CLEAR_INTR,
COMMAND_ADD_PROX_CLEAR_INTR = TMD37821_REG_COLORPROX_CMD_ADD_PROX_CLEAR_INTR
} tmdCommand_t;
typedef enum
{
PIEN_NOT_ASSERT = TMD37821_REG_COLORPROX_ENABLE_PIEN_NOT_ASSERT,
PIEN_ASSERT = TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT
} tmdProximityInterrupt_t;
typedef enum
{
AIEN_NOT_ASSERT = TMD37821_REG_COLORPROX_ENABLE_AIEN_NOT_ASSERT,
AIEN_ASSERT = TMD37821_REG_COLORPROX_ENABLE_AIEN_ASSERT
} tmdColorInterrupt_t;
typedef enum
{
WEN_DISABLE = TMD37821_REG_COLORPROX_ENABLE_WEN_DISABLE,
WEN_ENABLE = TMD37821_REG_COLORPROX_ENABLE_WEN_ENABLE
} tmdWaitEnable_t;
typedef enum
{
PEN_DISABLE = TMD37821_REG_COLORPROX_ENABLE_PEN_DISABLE,
PEN_ENABLE = TMD37821_REG_COLORPROX_ENABLE_PEN_ENABLE
} tmdProximityEnable_t;
typedef enum
{
AEN_DISABLE = TMD37821_REG_COLORPROX_ENABLE_AEN_DISABLE,
AEN_ENABLE = TMD37821_REG_COLORPROX_ENABLE_AEN_ENABLE
} tmdColorEnable_t;
typedef enum
{
PON_DISABLE = TMD37821_REG_COLORPROX_ENABLE_PON_DISABLE,
PON_ENABLE = TMD37821_REG_COLORPROX_ENABLE_PON_ENABLE
} tmdPowerEnable_t;
typedef enum
{
ATIME_2_38MS = TMD37821_REG_COLORPROX_ATIME_2_38MS,
ATIME_24MS = TMD37821_REG_COLORPROX_ATIME_24MS,
ATIME_100MS = TMD37821_REG_COLORPROX_ATIME_100MS,
ATIME_152MS = TMD37821_REG_COLORPROX_ATIME_152MS,
ATIME_609MS = TMD37821_REG_COLORPROX_ATIME_609MS
} tmdATime_t;
typedef enum
{
WTIME_1 = TMD37821_REG_COLORPROX_WTIME_1,
WTIME_85 = TMD37821_REG_COLORPROX_WTIME_85,
WTIME_256 = TMD37821_REG_COLORPROX_WTIME_256
} tmdWTime_t;
typedef enum
{
WLONG_NOT_ASSERT = TMD37821_REG_COLORPROX_CONFIG_WLONG_NOT_ASSERT,
WLONG_ASSERT = TMD37821_REG_COLORPROX_CONFIG_WLONG_ASSERT
} tmdWaitLong_t;
typedef enum
{
PDRIVE_100 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_100,
PDRIVE_50 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_50,
PDRIVE_25 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_25,
PDRIVE_12_5 = TMD37821_REG_COLORPROX_CONTROL_PDRIVE_12_5
} tmdProximityDrive_t;
typedef enum
{
PSAT_0 = TMD37821_REG_COLORPROX_CONTROL_PSAT_0,
PSAT_1 = TMD37821_REG_COLORPROX_CONTROL_PSAT_1
} tmdProximityOutput_t;
typedef enum
{
AGAIN_1X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_1X,
AGAIN_4X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_4X,
AGAIN_16X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_16X,
AGAIN_60X = TMD37821_REG_COLORPROX_CONTROL_AGAIN_60X
} tmdColorGain_t;
typedef struct
{
uint16_t C;
uint16_t R;
uint16_t G;
uint16_t B;
uint16_t P;
} tmdSensorData_t;
// Instance-specific properties
uint8_t tmd_conversionDelay;
tmdCommand_t tmd_command;
tmdProximityInterrupt_t tmd_proximityinterrupt;
tmdColorInterrupt_t tmd_colorinterrupt;
tmdWaitEnable_t tmd_waitenable;
tmdProximityEnable_t tmd_proximityenable;
tmdColorEnable_t tmd_colorenable;
tmdPowerEnable_t tmd_powerenable;
tmdATime_t tmd_atime;
tmdWTime_t tmd_wtime;
tmdWaitLong_t tmd_waitlong;
tmdProximityDrive_t tmd_proximitydrive;
tmdProximityOutput_t tmd_proximityoutput;
tmdColorGain_t tmd_colorgain;
uint8_t tmd_i2cAddress;
tmdSensorData_t tmd_colorproxData;
void getAddr_TMD37821(uint8_t i2cAddress);
bool begin(void);
void Measure_Sensor(void);
void setUpColorProximity(void);
void Measure_Color(void);
void Measure_Proximity(void);
void setCommand(tmdCommand_t command);
tmdCommand_t getCommand(void);
void setProximityInterrupt(tmdProximityInterrupt_t proximityinterrupt);
tmdProximityInterrupt_t getProximityInterrupt(void);
void setColorInterrupt(tmdColorInterrupt_t colorinterrupt);
tmdColorInterrupt_t getColorInterrupt(void);
void setWaitEnable(tmdWaitEnable_t waitenable);
tmdWaitEnable_t getWaitEnable(void);
void setProximityEnable(tmdProximityEnable_t proximityenable);
tmdProximityEnable_t getProximityEnable(void);
void setColorEnable(tmdColorEnable_t colorenable);
tmdColorEnable_t getColorEnable(void);
void setPowerEnable(tmdPowerEnable_t powerenable);
tmdPowerEnable_t getPowerEnable(void);
void setATime(tmdATime_t atime);
tmdATime_t getATime(void);
void setWTime(tmdWTime_t wtime);
tmdWTime_t getWTime(void);
void setWaitLong(tmdWaitLong_t waitlong);
tmdWaitLong_t getWaitLong(void);
void setProximityDrive(tmdProximityDrive_t proximitydrive);
tmdProximityDrive_t getProximityDrive(void);
void setProximityOutput(tmdProximityOutput_t proximityoutput);
tmdProximityOutput_t getProximityOutput(void);
void setColorGain(tmdColorGain_t colorgain);
tmdColorGain_t getColorGain(void); |
Now for the answer given by Ttelmah:
You will notice that indeed, at the top the TMD values are already #defined.
But how to solve the "size" (int16)? Could there be a way to define from the beginning that they are of size int16 as in arduino? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19385
|
|
Posted: Mon May 29, 2017 8:04 am |
|
|
I'd suspect that if you changed the defines, so they included an explicit 'long', this might well trigger the enum to be 16bit. So:
#define TMD37821_REG_COLORPROX_ENABLE_PIEN_ASSERT (0x20L)
etc.. |
|
![](templates/subSilver/images/spacer.gif) |
AndresRam
Joined: 27 May 2017 Posts: 16 Location: Colombia
|
|
Posted: Mon May 29, 2017 3:11 pm |
|
|
Thanks very much Ttelmah,
I have thought the following, if in the #define (in the original program of Arduino) it has size 0x00 (for example), I do not think that later it can be used or required to be 0x000. Therefore, being declared:
#define TMD37821_REG_COLORPROX_ENABLE_AIEN_NOT_ASSERT (0x00)
Is because it will always be (or will be needed) that size, right? |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|