Reading from the Slave
This is a little more complicated - but not too much more. Before reading data from the slave device, you must tell it which of its internal addresses you want to read. So a read of the slave actually starts off by writing to it. This is the same as when you want to write to it: You send the start sequence, the I2C address of the slave with the R/W bit low (even address) and the internal register number you want to write to. Now you send another start sequence (sometimes called a restart) and the I2C address again - this time with the read bit set. You then read as many data bytes as you wish and terminate the transaction with a stop sequence. So to read the compass bearing as a byte from the CMPS03 module:
1. Send a start sequence
2. Send 0xC0 ( I2C address of the CMPS03 with the R/W bit low (even address)
3. Send 0x01 (Internal address of the bearing register)
4. Send a start sequence again (repeated start)
5. Send 0xC1 ( I2C address of the CMPS03 with the R/W bit high (odd address)
6. Read data byte from CMPS03
7. Send the stop sequence.
so i send a start
then 7bit address of 2 with an added r/w bit. 0 is write therefore byte 1 = 4
next 2nd byte 66 decimal 42h
start
then byte 5 i.e. 7bits 2 + LSB 1 read bit
listen for a byte
stop
I have a pull up resistor 4.7K on the blue data wire
yellow is clock
red and black both at 0v relative to my PIC
Green +5V
I keep getting 255 back, like as if the pull up is the wrong value and holding all bits on.
I haven't got a scope here, on the girlfriends dining room table
help
regards Michael
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Have you got a pull up resistor on the clock line?
I've had trouble in the past getting the software I2C library to do a repeated start - that was reading from a 3 axis accelerometer (ST's LSM303) as well but I didn't look into it. The pic I was using had an MSSP module so it was easier to get the hardware I2C library working as a master rather than figure out why the software I2C library and the accelerometer didn't want to talk. Not much use to you with a 16F84A though except as an indication that there could be a library problem or maybe a careful arrangement of the I2C sub calls is required.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am trying to use a PIC16F84A to read a value out of a lego mindstorms accelerometer.
has anybody got this working, what is the byte sequence
lego say byte 0 = 0x2h
byte 1 = 0x42h for x axis msbyte
byte 2 = R+0x3h
I have no idea what is R
Regards Michael
Reading from the Slave
This is a little more complicated - but not too much more. Before reading data from the slave device, you must tell it which of its internal addresses you want to read. So a read of the slave actually starts off by writing to it. This is the same as when you want to write to it: You send the start sequence, the I2C address of the slave with the R/W bit low (even address) and the internal register number you want to write to. Now you send another start sequence (sometimes called a restart) and the I2C address again - this time with the read bit set. You then read as many data bytes as you wish and terminate the transaction with a stop sequence. So to read the compass bearing as a byte from the CMPS03 module:
1. Send a start sequence
2. Send 0xC0 ( I2C address of the CMPS03 with the R/W bit low (even address)
3. Send 0x01 (Internal address of the bearing register)
4. Send a start sequence again (repeated start)
5. Send 0xC1 ( I2C address of the CMPS03 with the R/W bit high (odd address)
6. Read data byte from CMPS03
7. Send the stop sequence.
so i send a start
then 7bit address of 2 with an added r/w bit. 0 is write therefore byte 1 = 4
next 2nd byte 66 decimal 42h
start
then byte 5 i.e. 7bits 2 + LSB 1 read bit
listen for a byte
stop
I have a pull up resistor 4.7K on the blue data wire
yellow is clock
red and black both at 0v relative to my PIC
Green +5V
I keep getting 255 back, like as if the pull up is the wrong value and holding all bits on.
I haven't got a scope here, on the girlfriends dining room table
help
regards Michael
Hi Michael,
Have you got a pull up resistor on the clock line?
I've had trouble in the past getting the software I2C library to do a repeated start - that was reading from a 3 axis accelerometer (ST's LSM303) as well but I didn't look into it. The pic I was using had an MSSP module so it was easier to get the hardware I2C library working as a master rather than figure out why the software I2C library and the accelerometer didn't want to talk. Not much use to you with a 16F84A though except as an indication that there could be a library problem or maybe a careful arrangement of the I2C sub calls is required.
no, i only have a pull up on the data line, I will try this later this evening
thank you
Michael