Color Sensor with pic16f877a

Status
Not open for further replies.
thanks, but i dont know how to send a data or address to write or read on color sensor, referring mikrobasic you can see an instruction, just to add address and what you need to write, so i need the address for each register.
 
Page 18 of the data sheet has the register addresses.

The communication protocol is explained very well from page 7 onwards. If you haven't used I2C before, I suggest that you try a simple routine to start with. Just send the address (0x74) and a read or write bit and the sensor should acknowledge the I2C transmission with a single low bit. Make sure that that works, and make sure it doesn't work when the sensor is disconnected, and you will have made a start. Don't go on to try to read specific registers until you are sure that the basic communication is connected correctly.
 
Thanks for cooperate with me, but could you please tell me how i can determine No. channels cap and Integration Time Slot Setting Register for xxx Channel
also what is the relation between channels and data reading.
thanks
 
I've got no idea on that. You'll have to read the data sheet or try it.

It will take time. Don't expect answers immediately, and make sure you check things step by step.
 
program ColorSensor
dim R1 as byte
dim R2 as byte
dim G1 as byte
dim G2 as byte
dim B1 as byte
dim B2 as byte
dim C1 as byte
dim C2 as byte


DIM RX AS WORD
DIM GX AS WORD
DIM BX AS WORD
DIM CX AS WORD
DIM K2 AS WORD


DIM RX1 AS STRING[5]
DIM GX1 AS STRING[5]
DIM BX1 AS STRING[5]
DIM CX1 AS STRING[5]
DIM K3 AS STRING[5]



main:

TRISD.6=0
PORTD.6=1
LCD_CONFIG(PORTB,7,6,5,4,PORTB,1,2,3)


LCD_OUT(1,1," COLOR")
I2C_Init(100000)


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X02)
I2C_WR(0X00)

I2C_Stop


I2C_Start

I2C_WR(0xE8)
I2C_WR(0X06)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X07)
I2C_WR(0XFF)

I2C_Stop

I2C_Start

I2C_WR(0XE8)
I2C_WR(0X08)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X09)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X0A)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X0B)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X0C)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X0D)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X0E)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X0F)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X10)
I2C_WR(0XFF)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0X11)
I2C_WR(0XFF)

I2C_Stop


RUN:

I2C_Start

I2C_WR(0XE8)
I2C_WR(0X01)
I2C_WR(0X01)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0x40)
I2C_Repeated_Start
i2c_wr(0xE9)
R1=i2c_rd(0)

I2C_Stop



I2C_Start

I2C_WR(0XE8)
I2C_WR(0x41)
I2C_Repeated_Start
i2c_wr(0xE9)
R2=i2c_rd(0)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0x42)
I2C_Repeated_Start
i2c_wr(0xE9)
G1=i2c_rd(0)

I2C_Stop



I2C_Start

I2C_WR(0XE8)
I2C_WR(0x43)
I2C_Repeated_Start
i2c_wr(0xE9)
G2=i2c_rd(0)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0x44)
I2C_Repeated_Start
i2c_wr(0xE9)
B1=i2c_rd(0)

I2C_Stop



I2C_Start

I2C_WR(0XE8)
I2C_WR(0x45)
I2C_Repeated_Start
i2c_wr(0xE9)
B2=i2c_rd(0)

I2C_Stop


I2C_Start

I2C_WR(0XE8)
I2C_WR(0x46)
I2C_Repeated_Start
i2c_wr(0xE9)
C1=i2c_rd(0)

I2C_Stop



I2C_Start

I2C_WR(0XE8)
I2C_WR(0x47)
I2C_Repeated_Start
i2c_wr(0xE9)
C2=i2c_rd(0)

I2C_Stop

Test:
RX=R1+R2
LCD_OUT(1,1,"RED=")
WORDTOSTR(RX,RX1)
LCD_OUT(2,4,RX1)
DELAY_MS(500)
LCD_CMD(LCD_CLEAR)

GX=G1+G2
LCD_OUT(1,1,"GREEN=")
WORDTOSTR(GX,GX1)
LCD_OUT(2,4,GX1)
DELAY_MS(500)
LCD_CMD(LCD_CLEAR)


BX=B1+B2
LCD_OUT(1,1,"BLUE=")
WORDTOSTR(BX,BX1)
LCD_OUT(2,4,BX1)
DELAY_MS(500)
LCD_CMD(LCD_CLEAR)


CX=C1+C2
LCD_OUT(1,1,"CLEAR=")
WORDTOSTR(CX,CX1)
LCD_OUT(2,4,CX1)
DELAY_MS(500)
LCD_CMD(LCD_CLEAR)


goto run

END.
 
1. use codetags
2. always put comments in your code if you expect anyone to understand it
3. is that working code above or do you have a question?
 
I wrote that code but i got 0 reading .. !! so i think that is not correct, when i write correct code i will put comments.

thanks
 
Do you have access to an I2C bus analyzer? I've used my PICkit2 clone as one and they're really handy for getting the hang of I2C. A DSO could also be used.
Is this a real circuit or simulation?
 
Last edited:
We need comments.

I've lost my crystal ball polish.....

I don't think that you should be sending the I2C_stop every time. I think it should be an I2C restart.
 
referring datasheet for read or write registers you have follow spes. sequence as start and add. slave . write..... stop ... ect

i have not pickit2 or 3


.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…