Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Color Sensor (ADJD-S311-CR999) and PIC 18F452

Status
Not open for further replies.

ali_cmi

Junior Member level 2
Junior Member level 2
Joined
Apr 19, 2012
Messages
21
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,512
Hello,

I am trying to interface a Color Sensor (ADJD-S311-CR999 Miniature Surface Mount RGB Digital Color Sensor) with PIC 18F452. This sensor communicates with a peripheral using I2C protocol and is quite handy (as people say it).
The problem I am facing is that when I try to read sensor gains, the PIC somehow resets itself although it is being locked in a while loop. WDT is turned off so I guess that reset rules out. I was suspicious of Stack Overflow Reset (don't know anything about it---came to know when I ran into project settings in MikcroC Pro for PIC).
And another amazing thing, unlike the WDT reset which is periodic (2.3s reset), the controller resets randomly as sometimes the whole program executes and sometimes it breaks due to reset.

The worst part: I use Proteus for general simulations (actually I do for all simulations) and it does not have a model for this or any color sensor. :( So, I have to debug the code line by line and it is painful. :\

Any help would be appreciated on how to stop controller reset. I have provided the code here.



Also, Please refer to me a better simulation software than Proteus. I know PSpice and Multisim but Proteus is handy for most of the jobs.
 

You have not provided your code,

Kindly provide it...

and post your schematic diagram.. as well as code...

First of all Check your voltage levels...

The reset you are referring, had occurred with me also, but it occurs only when i had not written my code properly and causes my controller to stop at a some point..
Post your code and Schematic Diagram

---------- Post added at 23:15 ---------- Previous post was at 23:13 ----------

You have not provided your code,

Kindly provide it...

and post your schematic diagram.. as well as code...

First of all Check your voltage levels...

The reset you are referring, had occurred with me also, but it occurs only when i had not written my code properly and causes my controller to stop at a some point..
Post your code and Schematic Diagram
 

Here is the code and schematic diagram of the color sensor. In addition to it, I am attaching the pin configuration of the sensor.
Capture.PNG
ADJD-S311_breakout_fritzing_part_screenshot.jpg

I have attached an LCD (not shown in schematic) to check the sensor gains for different colors. Also, as I cannot simulate the sensor, I have added code lines to display messages on screen as the code proceeds.

Code:
sbit LCD_RS at RB2_bit;sbit LCD_EN at RB3_bit;sbit LCD_D4 at RB4_bit;sbit LCD_D5 at RB5_bit;sbit LCD_D6 at RB6_bit;sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;sbit LCD_EN_Direction at TRISB3_bit;sbit LCD_D4_Direction at TRISB4_bit;sbit LCD_D5_Direction at TRISB5_bit;sbit LCD_D6_Direction at TRISB6_bit;sbit LCD_D7_Direction at TRISB7_bit;
Lcd_Init();

#define CTRL 0x00
#define DEVICE_WRITE    0xE8 //Default ADJD-S371 I2C address - write
#define DEVICE_READ     0xE9 //Default ADJD-S371 I2C address - read

#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

unsigned short aa=0, rl8=0, gl8=0, bl8=0, cl8=0, rh4, gh4, bh4, ch4;
char redstr[9], greenstr[9], bluestr[9], clearstr[9], read[7];
unsigned long red, green, blue, clear;
void main(){
  adcon0.adon=0;
  adcon1=0b10000111;
  trisa=0;
  porta=0xff;
  PORTB = 0;
  TRISB = 0;                 // Configure PORTB as output
  TRISD.f0=0;
  TRISD=0;
  PORTD=0xff;
  TRISC.f7=0;
  trisc.f6=0;
  portc.f6=portc.f7=0;
 Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);Lcd_Cmd(_LCD_CURSOR_OFF);Lcd_Cmd(_LCD_RETURN_HOME);
  I2C1_Init(100000);         // initialize

//***********CAP_RED VALUES***********

        I2C1_Start();              // start
        I2C1_Wr(DEVICE_WRITE);             // device address
        I2C1_Wr(CAP_RED);             // address
        I2C1_Wr(0x05);             // data
        I2C1_Stop();               // stop

        delay_us(100);

//***********CAP_GREEN VALUES***********

        I2C1_Start();              // start
        I2C1_Wr(DEVICE_WRITE);             // device address
        I2C1_Wr(CAP_GREEN );             // address
        I2C1_Wr(0x05);             // data
        I2C1_Stop();               // stop

        delay_us(100);

//***********CAP_BLUE VALUES***********

        I2C1_Start();              // start
        I2C1_Wr(DEVICE_WRITE);             // device address
        I2C1_Wr(CAP_BLUE);             // address
        I2C1_Wr(0x05);             // data
        I2C1_Stop();               // stop

        delay_us(100);

//***********CAP_CLEAR VALUES***********

        I2C1_Start();              // start
        I2C1_Wr(DEVICE_WRITE);             // device address
        I2C1_Wr(CAP_CLEAR);             // address
        I2C1_Wr(0x05);             // data
        I2C1_Stop();               // stop

        delay_us(100);

//***********INT_RED***********

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_RED_LO);             // address
            I2C1_Wr(0x00);             // data
            I2C1_Stop();               // stop

            delay_us(100);

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_RED_HI);             // address
            I2C1_Wr(0x02);             // data
            I2C1_Stop();               // stop

             Delay_100ms();

//***********INT_GREEN***********

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_GREEN_LO);             // address
            I2C1_Wr(0x00);             // data
            I2C1_Stop();               // stop

            Delay_100ms();

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_GREEN_LO);             // address
            I2C1_Wr(0x04);             // data
            I2C1_Stop();               // stop

            Delay_100ms();

//***********INT_BLUE***********

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_BLUE_LO);             // address
            I2C1_Wr(0x00);             // data
            I2C1_Stop();               // stop

            Delay_100ms();

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_BLUE_HI);             // address
            I2C1_Wr(0x08);             // data
            I2C1_Stop();               // stop

      Delay_100ms();

//***********INT_CLEAR***********

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_CLEAR_LO);             // address
            I2C1_Wr(0x00);             // data
            I2C1_Stop();               // stop

            Delay_100ms();

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(INT_CLEAR_HI);             // address
            I2C1_Wr(0x01);             // data
            I2C1_Stop();               // stop

            Delay_100ms();
 lcd_out(1,1,"Values Written...");
 delay_ms(2000);
 Lcd_Cmd(_LCD_CLEAR);
//READING VALUES FROM GSSR REGISTER
  while(1) {
  Lcd_Cmd(_LCD_CLEAR);
             lcd_out(1,1,"start");
             delay_ms(2000);
             Delay_100ms();
   // write ctrl reg with 0x01 to read sensor registers
            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(CTRL);             // address
            I2C1_Wr(0x01);             // data
            I2C1_Stop();               // stop

             delay_ms(100);

            I2C1_Start();              // start
            I2C1_Wr(DEVICE_WRITE);             // device address
            I2C1_Wr(CTRL);             // address
            I2C1_Repeated_Start();     // RS
            I2C1_Wr(DEVICE_READ);             // data
            aa=I2C1_Rd(0u);       // data
            I2C1_Stop();               // stop
            lcd_out(2,1,"READ!!!");
            delay_ms(1000);
            inttostr(aa,read);
            lcd_out(3,5,read);
            delay_ms(2000);
              Lcd_Cmd(_LCD_CLEAR);
                  //READ DATA
                          while(aa!=0) {
                          PORTC.f7=portc.f6=0;
                          portd.f1=1;
                          portd.f0=1;
                          porta=portb=portd=0x81;
                          lcd_out(1,1,"nothing");
                          delay_ms(2000);
                          break;
                          }
            while(aa==0)
                  {
                  lcd_out(1,1,"entered aa==0");
                  delay_ms(2000);
                  PORTC.f7=portc.f6=1;
                  portd.f1=0;
                  portd.f0=0;
                  delay_ms(2000);
                  Lcd_Cmd(_LCD_CLEAR);

lcd_out(1,1,"1");
delay_ms(200);
                  //DATA RED
                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_RED_LO);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  rl8=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_RED_HI);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  rh4=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  red=rh4*256+rl8;

lcd_out(1,2,"2");
delay_ms(200);

                  //DATA GREEN
                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_GREEN_LO);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  gl8=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_GREEN_HI);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  gh4=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  green=gl8+gh4*256;
lcd_out(1,3,"3");
delay_ms(200);
                  //DATA BLUE
                   Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_BLUE_LO);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  bl8=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_BLUE_HI);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  bh4=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  blue=bl8+bh4*256;
lcd_out(1,4,"4");
delay_ms(200);
                  //DATA CLEAR
                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_CLEAR_LO);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  cl8=I2C1_Rd(0u);          // data
                  I2C1_Stop();

                  Delay_100ms();

                  I2C1_Start();              // start
                  I2C1_Wr(DEVICE_WRITE);             // device address
                  I2C1_Wr(DATA_CLEAR_HI);             // address
                  I2C1_Repeated_Start();     // RS
                  I2C1_Wr(DEVICE_READ);             // device read address
                  ch4=I2C1_Rd(0u);          // data
                  I2C1_Stop();
                  clear=ch4*256+cl8;
lcd_out(1,5,"5");
delay_ms(200);
                  LongIntToHex(red,redstr);
                  LongIntToHex(green,greenstr);
                  LongIntToHex(blue,bluestr);
                  LongIntToHex(clear,clearstr);
lcd_out(1,6,"6");
delay_ms(500);
 Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_RETURN_HOME);

                  lcd_out(1,1,redstr);
                  lcd_out(2,1,greenstr);
                  lcd_out(3,1,bluestr);
                  lcd_out(4,1,clearstr);
                  delay_ms(7000);
                  break;
                  }
       }
}
 

@ Sir Arun Sharma

Apologies for the untidy code. :-| I have checked my voltage levels and they are as they should be. I even turned off the stack overflow/underflow reset in addition to WDT but it just doesn't work. Also, previously, in the code, the delays were in microseconds, I increased them to milliseconds. The effect I observed was that controller did reset but not so frequently as it did in the former case. (In case of microsecond delays, the controller resets after 1 or 2 iterations of the while loop, sometimes in between and in case of millisecond delays, the controller resets after 7-8 iterations)

The datasheet of this color sensor can be downloaded at https://pdf1.alldatasheet.com/datasheet-pdf/view/203386/AVAGO/ADJD-S311-CR999.html

---------- Post added at 01:41 ---------- Previous post was at 01:41 ----------

@ Sir Arun Sharma

Apologies for the untidy code. :-| I have checked my voltage levels and they are as they should be. I even turned off the stack overflow/underflow reset in addition to WDT but it just doesn't work. Also, previously, in the code, the delays were in microseconds, I increased them to milliseconds. The effect I observed was that controller did reset but not so frequently as it did in the former case. (In case of microsecond delays, the controller resets after 1 or 2 iterations of the while loop, sometimes in between and in case of millisecond delays, the controller resets after 7-8 iterations)

The datasheet of this color sensor can be downloaded at https://pdf1.alldatasheet.com/datasheet-pdf/view/203386/AVAGO/ADJD-S311-CR999.html
 
can you give me exact proteus chema?

---------- Post added at 22:42 ---------- Previous post was at 22:39 ----------

ı am mading final project.for this project, I need your help.if you help me , ı will be very glad.
ı have a sparkfun adjd-s311-cr999.ı want to separate the ball(red-green-blue) with this color sensor.but ı dont have enough knowledge about I2C protokole(scl-sda).can you help me about pic circuit that scl-sda convert to 3 dijital signal (red-green-blue) in shape of on-off and pic code(picpro basic).thank you for your interest.:)))))
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top