How I can to communicate the PIC through I2C with the color sensor ADJD-S371-QR999.
HOw I can to translate to mikroC code this datasheet, in this documente explain how works but I can´t do it.
These are the datasheets:
http://www.avagotech.com/cs/Satellite?l ... 99&x=0&y=0
Please, I need the code.
Thanks.
PIC I2C to color sensor ADJD-S371-QR999
Re: PIC I2C to color sensor ADJD-S371-QR999
I want to share MikroC code, based on the manifacturer's PIC sample code for ADJD-S371-Q999. I added RED, GREEN and BLUE LEDs and a simple PWM(not enough good, but works) and the color read by the sensor is indicated on the LEDs. The code is not complete - calibration is needed for best and stable work.
Code: Select all
/*
* Project name:
ADJD-S371-Q999 read
* Description:
A simple example of reading a ADJD-S371 sensor for mikroC PRO for PIC.
RED, GREEN and BLUE leds indicates the read color, using a simple PWM.
* Test configuration:
MCU: PIC18F4520
Oscillator: HS, 20.0000 MHz
Dev.Board: EasyPIC5 & Sparkfun's ADJD-S371-Q999 Color Light Sensor Evaluation Board
http://www.sparkfun.com/commerce/product_info.php?products_id=8663
SW: mikroC PRO for PIC
* NOTES:
- A 5V to 3,3V level converter is needed for the I2C communication
- Green LED is connected to PORTE,0
- Red LED is connected to PORTE,1
- Blue LED is connected to PORTE,2
- Pull-up resistors(10k) for the SDA, SCL are needed.
- Sensor calibration is not included in the code
*/
char i;
#define ACK 1
#define NO_ACK 0
#define DEVICE_WRITE 0xE8
#define DEVICE_READ 0xE9
#define CAP_RED 0x06
#define CAP_GREEN 0x07
#define CAP_BLUE 0x08
#define CAP_CLEAR 0x09
#define INT_RED_LO 0x0A
#define INT_RED_HI 0x0B
#define INT_GREEN_LO 0x0C
#define INT_GREEN_HI 0x0D
#define INT_BLUE_LO 0x0E
#define INT_BLUE_HI 0x0F
#define INT_CLEAR_LO 0x10
#define INT_CLEAR_HI 0x11
#define DATA_RED_LO 0x40
#define DATA_RED_HI 0x41
#define DATA_GREEN_LO 0x42
#define DATA_GREEN_HI 0x43
#define DATA_BLUE_LO 0x44
#define DATA_BLUE_HI 0x45
#define DATA_CLEAR_LO 0x46
#define DATA_CLEAR_HI 0x47
#define OFFSET_RED 0x48
#define OFFSET_GREEN 0x49
#define OFFSET_BLUE 0x4A
#define OFFSET_CLEAR 0x4B
// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections
void Init_Main(){
ADCON1 = 0x0F; // AD converter off
PORTB=0;
TRISB=0;
TRISD=0;
PORTE = 0;
TRISE = 0; // out
}
void write_register(unsigned short register_name, unsigned short register_value){
Soft_I2C_START();
Soft_I2C_Write(DEVICE_WRITE);
Soft_I2C_Write(register_name); //Write register address
Soft_I2C_Write(register_value); //Write data
Soft_I2C_STOP();
}
//=============================================
unsigned short read_register(unsigned short register_name){
unsigned short in_byte;
Soft_I2C_START();
Soft_I2C_Write(DEVICE_WRITE);
Soft_I2C_Write(register_name); //Write register address
Soft_I2C_START(); //Repeat start (SR)
Soft_I2C_Write(DEVICE_READ); //Now ask the IC to report on the last command
in_byte = Soft_I2C_Read(register_name);
Soft_I2C_STOP();
return(in_byte);
}
//=============================================
//--------------------- Reads colors information from device
void adjd_s371_read() {
unsigned short response;
unsigned int red, green, blue, clear;
char red_low8, red_high8, green_low8, green_high8, blue_low8, blue_high8, clear_low8=1, clear_high8=1;
char R4, G4, B4, C4;
unsigned short i;
char Rr=10, Rb=1, Rg=1, Rc=1;
int j;
write_register(0x00, 0x01); //Get sensor reading
while(1) {
response = read_register(0x00);
if (response == 0) break;
}
//Red
red_low8 = read_register(DATA_RED_LO);
red_high8 = read_register(DATA_RED_HI);
red = (red_high8<<8) + red_low8; //red=F0FF
R4=red/4;
//Green
green_low8 = read_register(DATA_GREEN_LO);
green_high8 = read_register(DATA_GREEN_HI);
green = (green_high8<<8) + green_low8; //red=F0FF
G4=green/4;
//Blue
blue_low8 = read_register(DATA_BLUE_LO);
blue_high8 = read_register(DATA_BLUE_HI);
blue = (blue_high8<<8) + blue_low8; //red=F0FF
B4=blue/4;
//Clear
clear_low8 = read_register(DATA_CLEAR_LO);
clear_high8 = read_register(DATA_CLEAR_HI);
clear = (clear_high8<<8) + clear_low8; //red=F0FF
C4=clear/4;
PORTB=clear_low8;
PORTD=clear_high8;
//Display R-G-B Color
j=0;
PORTE=0;
if(G4>7){
PORTE.F0=1;
}
if(R4>7){
PORTE.F1=1;
}
if(B4>7){
PORTE.F2=1;
}
REFRESH:
while(j<1000){
if(G4>7){
PORTE.F0=1;
}
if(R4>7){
PORTE.F1=1;
}
if(B4>7){
PORTE.F2=1;
}
for(i=0;i<255; i++){ //PAUSE
if (G4<i){
PORTE.F0=0;
}
if (R4<i){
PORTE.F1=0;
}
if (B4<i){
PORTE.F2=0;
}
}
j++;
goto REFRESH;
}
//######################################
}
//=============================================
void adjd_init()
{
write_register(CAP_RED, 0x05);
write_register(CAP_GREEN, 0x05);
write_register(CAP_BLUE, 0x05);
write_register(CAP_CLEAR, 0x05);
write_register(INT_RED_LO, 0xC4);
write_register(INT_RED_HI, 0x09);
write_register(INT_GREEN_LO, 0xC4);
write_register(INT_GREEN_HI, 0x09);
write_register(INT_BLUE_LO, 0xC4);
write_register(INT_BLUE_HI, 0x09);
write_register(INT_CLEAR_LO, 0xC4);
write_register(INT_CLEAR_HI, 0x09);
}
//=============================================
void main(){
Init_Main();
Delay_ms(500);
adjd_init();
while(1) {
adjd_init();
adjd_s371_read();
}
while(1);
}
//------------------------------------