SRF02 I2C Interface

General & specific discussions on our Ultrasonic Rangers

Moderator: chris

SRF02 I2C Interface

Postby mohit » Sat Nov 10, 2007 9:37 pm

Hello,
I have successfully used th srf02 module in serial mode with my PIC18F452 board. I'm using the C18 compiler and its built-in libraries.
But Im simply unable to get it working in the I2C mode. Im relatively new to the topic of I2C but have ready quite a few articles on it.
Heres the setup I have created:
connected an 16x4 lcd to the PIC
connected the necessary pull up resistors on SDA/SCL
Clock is 10Mhz with PLL enabled ->> 40Mhz

and heres the code:

void main(void)

{
initialize();
while(1)
{
StartI2C();//start I2C
WriteI2C(0xE0);//send srf02 address
WriteI2C(0x00);//send command reg address
WriteI2C(0x50);//send mode command (inches)
StopI2C();//stop i2c

//delay of minimum 70mS required
Delay10KTCYx(70);

StartI2C();//starti2c
WriteI2C(0xE0);//send srf02 address
WriteI2C(0x02);//send data reg address
RestartI2C();
WriteI2C(0xE1);
datah = ReadI2C();
datal = ReadI2C();
StopI2C();

result= (( datah << 8 ) | datal);
itoa(result,string);
SetDDRamAddr(0x80);//put cursor back on start line
while( BusyXLCD() );
putsXLCD(string);//display the result
while( BusyXLCD() );

Delay10KTCYx(70);

}

}

//-------------------------------------------------------------

void initialize(void)
{
OpenXLCD( FOUR_BIT & LINES_5X7 ); //configure LCD
while( BusyXLCD() );
OpenI2C(MASTER, SLEW_ON);
return;
}

am I missing something? the led on the srf module flickers only at the start.
any pointers would be highly appreciated.
Thank you.
Mohit
mohit
 
Posts: 1
Joined: Sat Nov 10, 2007 9:22 pm

Postby chris » Mon Nov 12, 2007 10:30 am

I think the macro included in the C18 compiler does not setup the bus speed for I2C port, this is done with I2C Clock = Fosc /(4*(SSPADD+1)).
Therefore at 40mhz SSPADD = 99 will give a 100khz clock.
User avatar
chris
 
Posts: 172
Joined: Wed Nov 08, 2006 3:13 pm
Location: Norfolk, England

Postby alshukrimi » Fri Dec 14, 2007 9:34 pm

I used the SRF02 with the TINI microcontroller through the I2C interface. It didn't work with me until I found that the MSB of the address of the slave (E0) should be set when reading and clear when writing. Ex:

when writing to the SRF02 the address E0 (1110 0000) should be 70 (0111 0000) its like shifting the address to the right

and when reading it should be F0 (1111 0000) like shifting to the right and setting the MSB (though I didn't try to read)

I did that and it worked with the TINI

hope it will work with you,
alshukrimi
 
Posts: 4
Joined: Sun Nov 25, 2007 5:00 pm

Postby chris » Mon Dec 17, 2007 12:59 pm

I used the SRF02 with the TINI microcontroller through the I2C interface. It didn't work with me until I found that the MSB of the address of the slave (E0) should be set when reading and clear when writing. Ex:

when writing to the SRF02 the address E0 (1110 0000) should be 70 (0111 0000) its like shifting the address to the right

and when reading it should be F0 (1111 0000) like shifting to the right and setting the MSB (though I didn't try to read)


This is not actually correct, The read/write bit is the LSB of the address.

for example to start a measurement a write command is required:

1. Send a start sequence
2. Send 0xE0 ( I2C address of the SRF02 with the R/W bit low (even address - write))
3. Send 0x00 (Internal address of the command register)
4. Send 0x51 (The command to start the SRF02 ranging in cm)
5. Send the stop sequence.

when reading the range the correct sequence should be:

1. Send a start sequence
2. Send 0xE0 ( I2C address of the SRF02 with the R/W bit low (even address - write))
3. Send 0x02 (Internal address of the first distance register)
4. Send a start sequence again (repeated start)
5. Send 0xE1 ( I2C address of the SRF02 with the R/W bit high (odd address - read))
6. Read the two data bytes, high and low from SRF02
7. Send the stop sequence.

A tutorial of the I2C bus is here http://www.robot-electronics.co.uk/htm/ ... 2c_bus.htm
User avatar
chris
 
Posts: 172
Joined: Wed Nov 08, 2006 3:13 pm
Location: Norfolk, England

SRF08

Postby jagjit.sehra » Mon Feb 11, 2008 5:50 pm

Hi Folks,
Hello to everyone. I am expierencing a problem using the SRF08 ultra-range sensor with the PIC18F452. I have written the code using the micro-chip C18 compilier which has macros for the I2C bus. However I am not getting any values when I am reading the registers. I have just read a question by Mohit and it seems as though he has had the same problem. One of the replies was that the macros dont reduce the clock speed to 100 khz. I wanted to know did that solve the problem and if so how do you define it. I have shown my code below. Thank you in advance for any help that you can provide.

#include <p18f452>
#include <sw_i2c>

SSPADD=99;
void start(void);
void read(void);
void delay(void);
unsigned char var;
unsigned char value;
unsigned char i;
unsigned char hibyte, lobyte;
unsigned char i;


void main(void)
{


start();
delay(); // thsi delay is 70 ms
read();
Nop();




}

void start(void)
{
SWStartI2C();
var=SWPutcI2C(0xE0);
SWAckI2C();
var=SWPutcI2C(0x00);
SWAckI2C();
var=SWPutcI2C(0x51);
SWAckI2C();
SWStopI2C();
}

void read(void)
{

SWStartI2C();
var=SWPutcI2C(0xE0);
SWAckI2C();
var=SWPutcI2C(0x01);
SWAckI2C();
SWRestartI2C();
var=SWPutcI2C(0xE1);
SWAckI2C();
light=SWReadI2C();
SWAckI2C();
hibyte=SWReadI2C();
SWAckI2C();
lobyte=SWReadI2C();
SWStopI2C();
Nop();
}


void delay(void)
{
unsigned double j;

for(j=0;j<=1000;j++);

}
jagjit.sehra
 
Posts: 1
Joined: Mon Feb 11, 2008 5:42 pm

Postby chris » Tue Feb 12, 2008 10:21 am

Hi Folks,
Hello to everyone. I am expierencing a problem using the SRF08 ultra-range sensor with the PIC18F452. I have written the code using the micro-chip C18 compilier which has macros for the I2C bus. However I am not getting any values when I am reading the registers. I have just read a question by Mohit and it seems as though he has had the same problem. One of the replies was that the macros dont reduce the clock speed to 100 khz. I wanted to know did that solve the problem and if so how do you define it. I have shown my code below. Thank you in advance for any help that you can provide.


Hi Jagjit,

I must confess I have not actually used the C18 compiler, although I have taken a look at the documentation and believe I can give you a bit of help!

1, why are you sending an acknowledge bit for transmission of I2C data? it is needed only in the parts of the code where you are the reciever.

2, There appears to be no call to the function OpenI2Cx, this sets the port up for I2C
User avatar
chris
 
Posts: 172
Joined: Wed Nov 08, 2006 3:13 pm
Location: Norfolk, England


Return to Ultrasonic Sensors

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron