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);
}
} |
|
|