Yet Another Question on i2c bus...
Moderator: paolo.gai
Yet Another Question on i2c bus...
Hi,
I\'m still working on my i2c Honeywell sensor.. I can\'t send the ACK to the sensor after receiving the first byte of data, the dsPic sends always a NACK and the transmission stops.
Here is my code:
...
// Read the MSB data byte
while(I2C2CONbits.RCEN);
I2C2STATbits.I2COV = 0;
Data_Msb = I2C1RCV;
I2C2CONbits.ACKDT = 0; //set ACKDT bit to send ACK
i2c_SWBIT(I2C1CONbits.ACKEN); //send ACKEN = 1 and wait
i2c_SWBIT(I2C2CONbits.RCEN); //set RCEN = 1 and wait
I2C2STATbits.I2COV = 0;
Data_Lsb = I2C2RCV;
...
Data_Msb and Data_Lsb are the two bytes sent me by the sensor.
i2c_SWBIT is a macro to set high a bit and wait.
Is the code right?? Can you help me??
Tnx,
/Alberto
I\'m still working on my i2c Honeywell sensor.. I can\'t send the ACK to the sensor after receiving the first byte of data, the dsPic sends always a NACK and the transmission stops.
Here is my code:
...
// Read the MSB data byte
while(I2C2CONbits.RCEN);
I2C2STATbits.I2COV = 0;
Data_Msb = I2C1RCV;
I2C2CONbits.ACKDT = 0; //set ACKDT bit to send ACK
i2c_SWBIT(I2C1CONbits.ACKEN); //send ACKEN = 1 and wait
i2c_SWBIT(I2C2CONbits.RCEN); //set RCEN = 1 and wait
I2C2STATbits.I2COV = 0;
Data_Lsb = I2C2RCV;
...
Data_Msb and Data_Lsb are the two bytes sent me by the sensor.
i2c_SWBIT is a macro to set high a bit and wait.
Is the code right?? Can you help me??
Tnx,
/Alberto
Re:Yet Another Question on i2c bus...
The code seems to be ok, but there\'s a problem I think in the following statement:
This is due, I think, to the behaviour of the master during the reading phase as described in the detailed Microchip reference manual for the I2C (Section 19, page 7).
[img width=480]http://www.erika.tuxfamily.org/images/f ... xtract.PNG[/img]
I attach the code I used for the I2C bus communication; it is for I2C port 1, so you need to adapt to port 2.
I hope this will help you. If you have any problem post a more detailed version of the code you are using for the I2C communication.
Regard,
Christian.
because the value should be 1 instead of 0.I2C2CONbits.ACKDT = 0; //set ACKDT bit to send ACK
This is due, I think, to the behaviour of the master during the reading phase as described in the detailed Microchip reference manual for the I2C (Section 19, page 7).
[img width=480]http://www.erika.tuxfamily.org/images/f ... xtract.PNG[/img]
I attach the code I used for the I2C bus communication; it is for I2C port 1, so you need to adapt to port 2.
I hope this will help you. If you have any problem post a more detailed version of the code you are using for the I2C communication.
Regard,
Christian.
- Attachments
-
- microchip_refman_extract.PNG (24 KiB) Viewed 7699 times
-
- i2c.zip
- (24 KiB) Downloaded 516 times
Re:Yet Another Question on i2c bus...
Many thanks, now I try to look at your code.. I hope it will solve my problems.
/Alberto
/Alberto
Re:Yet Another Question on i2c bus...
Yeah!!!! It works!!!! :woohoo:
I tried your code, but nothing worked... So I\'ve decided to rewrite all the i2c protocol using the PORTG 2 and 3 pins (the same of the original i2c1 bus) and move up and down the pins by hand. The result?? All worked fine!!! Here is my code...[file name=i2c-195a1abbfe67fdb6f739ee60a797a060.zip size=895]
The code is for reading an Honeywell HMC6352 magnetometer; its datasheet can be found here: http://www.erika.tuxfamily.org/images/f ... 97a060.zip[/file]
http://www.sparkfun.com/datasheets/Comp ... MC6352.pdf
Thanks for your help!
/Alberto
I tried your code, but nothing worked... So I\'ve decided to rewrite all the i2c protocol using the PORTG 2 and 3 pins (the same of the original i2c1 bus) and move up and down the pins by hand. The result?? All worked fine!!! Here is my code...[file name=i2c-195a1abbfe67fdb6f739ee60a797a060.zip size=895]
The code is for reading an Honeywell HMC6352 magnetometer; its datasheet can be found here: http://www.erika.tuxfamily.org/images/f ... 97a060.zip[/file]
http://www.sparkfun.com/datasheets/Comp ... MC6352.pdf
Thanks for your help!
/Alberto
- Attachments
-
- i2c-195a1abbfe67fdb6f739ee60a797a060.zip
- (24 KiB) Downloaded 509 times
Re:Yet Another Question on i2c bus...
Uhm... :unsure:
I cannot download your code, it seems that it has not been properly uploaded on the server.
Anyhow, if you say that it\'s now working that\'s fine ;) , but I suggest to investigate which could be the error.
I think it would be better to use the I2C interface instead of doing it by hand (that increase the CPU load without any reason).
Try to post the code once more, if you can, and I shall have a look at it.
Bye.
I cannot download your code, it seems that it has not been properly uploaded on the server.
Anyhow, if you say that it\'s now working that\'s fine ;) , but I suggest to investigate which could be the error.
I think it would be better to use the I2C interface instead of doing it by hand (that increase the CPU load without any reason).
Try to post the code once more, if you can, and I shall have a look at it.
Bye.
Re:Yet Another Question on i2c bus...
Sorry, here is the code...
Now it works!
The problem I had was that the master always generate a NACK after reading the first byte, causing the stopping of the communication.
Regards,
/Alberto
Now it works!
The problem I had was that the master always generate a NACK after reading the first byte, causing the stopping of the communication.
Regards,
/Alberto
- Attachments
-
- i2c-295cfdc840b9db35967b59c30de7e6f2.zip
- (24 KiB) Downloaded 517 times
Re:Yet Another Question on i2c bus...
Ok, as I said before, if the code works (and it seems that should do) that is fine, but just for testing I dare say.
I mean that this is good to be sure that the peripheral you want to communicate with is working properly, but just this!
I strongly discourage you to use that code for general purpose, because it will be not flexible at all.
I have been able to use the I2C interface, and I think that you will be able to. :)
I suggest to go back to the version with the I2C interface and to debug it. Post the previous code, the full version is better, and I shall look for the problem.
Regards.
I mean that this is good to be sure that the peripheral you want to communicate with is working properly, but just this!
I strongly discourage you to use that code for general purpose, because it will be not flexible at all.
I have been able to use the I2C interface, and I think that you will be able to. :)
I suggest to go back to the version with the I2C interface and to debug it. Post the previous code, the full version is better, and I shall look for the problem.
Regards.
Re:Yet Another Question on i2c bus...
Hi,
I\'ve used your code adapted for my sensor. First of all I have to send to sensor 0x42 adress the 0x41 command to make start the reading; then sending the sensor 0x43 adress will give me back two bytes of data: MSB and LSB. Between the two bytes the sensor expects an ack from the master, that is the master must set the sda line low at the 9th clock after received the last bit of the MSB data Byte. Well, I don\'t know why, I always get a NACK, causing the end of the transmission.
Here is the revised code, that doesn\'t work at all: the scope doesn\'t see anything nor on the SDA line neither on the SCL line.
PS: Sorry for my code, it\'s messy...
Bye,
/Alberto
EDIT: and sorry for my english!!!:S
I\'ve used your code adapted for my sensor. First of all I have to send to sensor 0x42 adress the 0x41 command to make start the reading; then sending the sensor 0x43 adress will give me back two bytes of data: MSB and LSB. Between the two bytes the sensor expects an ack from the master, that is the master must set the sda line low at the 9th clock after received the last bit of the MSB data Byte. Well, I don\'t know why, I always get a NACK, causing the end of the transmission.
Here is the revised code, that doesn\'t work at all: the scope doesn\'t see anything nor on the SDA line neither on the SCL line.
PS: Sorry for my code, it\'s messy...
Bye,
/Alberto
EDIT: and sorry for my english!!!:S
- Attachments
-
- i2c-a67682b98eed6b61a99d9f12e4791511.zip
- (24 KiB) Downloaded 510 times
Re:Yet Another Question on i2c bus...
As far as I understood from the last code that you posted you are explicitly using the NACK after the first transmission. What you want, and what you should need, is the following transmission:
select_device -> ack_from_device -> read_msb -> ACK -> read_lsb -> NACK.
But looking at the code you send NACK after the msb byte, telling the device to close the transmission.
This is due to the following line
The problem with the scope is more serious (remember you are now using the i2c port 1 :)).
Did you call the i2c_init() function before doing everything?
You should at least see the clock line and some transition on the SDA.
Check also the I2C1BRG = 362; assignment in the init function, since this specify the i2c bus frequency and should match the peripheral specifiication (please refer to the microchip refman for i2c).
I hope this can help somewhat.
I don\'t mind your code, and above all your English (I\'m sorry for the mine :silly:)
Bye!
select_device -> ack_from_device -> read_msb -> ACK -> read_lsb -> NACK.
But looking at the code you send NACK after the msb byte, telling the device to close the transmission.
Code: Select all
/* Read the MSB data byte */
I2C1CONbits.RCEN = 1;
while (I2C1CONbits.RCEN);
I2C1STATbits.I2COV = 0;
data = I2C1RCV & 0x00FF;
_I2C1_CHECK_IDLE();
/* send NACK condition back to the I2C slave indicating master
* received the data byte
*/
I2C1CONbits.ACKDT = 1;
I2C1CONbits.ACKEN = 1;
while (I2C1CONbits.ACKEN); /* wait until NACK sequence is over */
_I2C1_CHECK_IDLE();
Try to put 0 (meaning ACK) instead of 1 after reading the msb byte (but keep the last NACK after the lsb byte).I2C1CONbits.ACKDT = 1;
The problem with the scope is more serious (remember you are now using the i2c port 1 :)).
Did you call the i2c_init() function before doing everything?
You should at least see the clock line and some transition on the SDA.
Check also the I2C1BRG = 362; assignment in the init function, since this specify the i2c bus frequency and should match the peripheral specifiication (please refer to the microchip refman for i2c).
I hope this can help somewhat.
I don\'t mind your code, and above all your English (I\'m sorry for the mine :silly:)
Bye!
Re:Yet Another Question on i2c bus...
Yes, you are right, in the code post before I used ACKDT bit set to 1. But either giving it zero value the result is unfortunatly always the same. In fact, as I told you before, this version of the code does not work at all. The scope was setted up fine, one input on SDA and another on the SCL, and the i2c port I used was the number 1. I\'m using an old scope and I cannot capture the screen, but tomorrow I\'ll try to take a picture of the wave... Anyhow, for now I\'ll use my code, I don\'t have too much time to finish my thesis!
Thank you a lot,
/Alberto
Thank you a lot,
/Alberto
Re:Yet Another Question on i2c bus...
Ok, don\'t worry for the driver. Use your own version and ...
in bocca al lupo per la tua tesi!
;)
in bocca al lupo per la tua tesi!
;)