|
|
View previous topic :: View next topic |
Author |
Message |
keplerforever
Joined: 23 Dec 2005 Posts: 8
|
i2c strange output...any ideas |
Posted: Sun May 07, 2006 9:25 am |
|
|
Hi,
I'm using a pic 16F877A clocked at 19660800 Hz, and I'm trying to connect it to a CMPS03 compass (http://www.robot-electronics.co.uk/htm/cmps3doc.shtml) using I2C protocol.
I've been using 1k8 pullup resitors on SDA and SCL lines, as the documentation indiquates.
And I've been inspiring myself with the code from this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=25869&highlight=cmps03+i2c
My problem is that I cannot recieve correct values of the compass. Instead I recieve a continuous flow of zero in my RS232 terminal.
I cannot figure out what is the issue: should I modify the code , but I'm really not an expert in I2C and I cannot make it clear when I should put delays or not (I tried many different possibilities). Is my cable too long: around 15cm
I don't see what I could test more
Thanks in advance for your inputs or code to test
Olivier |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 07, 2006 4:12 pm |
|
|
The problem is that the original poster in that thread never posted
the final working driver. Also, in his driver, he doesn't do a NACK on
the last i2c read. But all sample drivers do this. So I don't think
you can trust his driver.
If I take the sample code for the CMPS03 which Devantech has posted
at this link, http://www.robot-electronics.co.uk/files/example18f452.c
and translate it to CCS, I get the following code:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, SDA=PIN_C4, SCL=PIN_C3, FORCE_HW)
void read_compass(int8 *buffer)
{
i2c_start();
i2c_write(0xC0);
i2c_write(0x00);
i2c_start();
i2c_write(0xC1);
buffer[0] = i2c_read();
buffer[1] = i2c_read();
buffer[2] = i2c_read();
buffer[3] = i2c_read(0);
i2c_stop();
}
//============================
void main()
{
int8 compass[4];
int8 version;
int8 bearingB;
int16 bearingW;
while(1)
{
read_compass(compass);
version = compass[0];
bearingB = compass[1];
bearingW = ((int16)compass[2] << 8) + (int16)compass[3];
printf("Version = %u\n\r", version);
printf("bearingB = %u\n\r", bearingB);
printf("bearingW = %lu\n\r", bearingW);
printf("\n\r");
delay_ms(1000);
}
} |
|
|
|
keplerforever
Joined: 23 Dec 2005 Posts: 8
|
|
Posted: Mon May 08, 2006 6:41 am |
|
|
great!!!
The code is working perfectly.
Thank you very much, this is a relief for me, since I have always been a bit traumatized with i2c protocol |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|