EmbeddedRelated.com
Forums

regarding srf08(ultrasonic sensor) comm prob with lpc2138

Started by Viren Dobariya March 17, 2009
hi,
i have interface srf08-ultrasonic sensor with i2c bus of lpc2138.but i am facing communication problem with this interfacing. can anybody give me sample code of srf08-ultrasonic sensor with lpc21xx??

thanks
viren
Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/



An Engineer's Guide to the LPC2100 Series

--- In l..., Viren Dobariya wrote:
> hi,
> i have interface srf08-ultrasonic sensor with i2c bus of lpc2138.but i am facing communication problem with this interfacing. can anybody give me sample code of srf08-ultrasonic sensor with lpc21xx??
>
> thanks
> viren

It would be much easier to post your code here.

I am sure you did something wrong in your code, lets see it.

don

hi,
see the code of sfr08.
and guide me.

Thanks
viren
///////////////*************************/////////////////////////////////
unsigned char message[1] = {0x51};

//unsigned char message1[1] = {0x01};
//unsigned char messageIn[4];
unsigned char *I2CData,
I2Counter,
I2CAddress,
MemAddress,
lock; //Define Function prototypes and Global variables
unsigned int data = 0;
/**************************************************************/
void I2CTransferByte(unsigned int I2CAddr,unsigned char MemAddr,unsigned char count,...)
{
va_list ap;
va_start(ap,count);

while(lock == 1) //Wait for interrupt to signal end of I2C activity
{
;
}
lock = 1; //Set I2C bus as active

I2CAddress = I2CAddr; //Place address and data in Globals

to be used by the interrupt
if(count >0)
{
I2CData = va_arg(ap,unsigned char *);
}
I2Counter = count;
MemAddress = MemAddr;
I2CONCLR = 0x000000FF; //Clear all I2C settings
I2CONSET = 0x00000040; //Enable the I2C interface
I2CONSET = 0x00000020; //Start condition
va_end(ap);
}

/**************************************************************/
void I2CISR (void) __irq //I2C interrupt routine
{
switch (I2STAT) //Read result code and switch to next

action
{
// Start and Send byte conditions

case ( 0x08): //Start bit
I2CONCLR = 0x20; //Clear start bit
I2DAT = I2CAddress; //Send address and write bit
break;

case ( 0x10): //Start bit
I2CONCLR = 0x20; //Clear start bit
I2DAT = I2CAddress; //Send address and write bit
break;

case (0x18): //Slave address+W, ACK
I2DAT = MemAddress; //Write Mem,ory start address to tx register
break;

case (0x20): //Salve address +W, Not ACK
I2DAT = I2CAddress; //Resend address and write bi
break;

case (0x28):
if(I2Counter-->0) //Data sent, Ack
{
I2DAT = *I2CData; //Write data to tx register
I2CData++;
}
else
{
I2CONSET = 0x10; //Stop condition
lock = 0; //Signal end of I2C activity
}
break;

case (0x30) : //Data sent, NOT Ack
I2DAT = *I2CData; //Write data to tx register
break;

//Receive byte conditions

case (0x40) : //Slave Address +R, ACK
I2CONSET = 0x04; //Enable ACK for data byte
break;

case (0x48) : //Slave Address +R, Not Ack
I2CONSET = 0x20; //Resend Start condition
break;

case (0x50) : //Data Received, ACK
if(I2Counter-->0)
{
to_hyper(I2DAT);
data = I2DAT;
//*I2CData = I2DAT;
//I2CData++;
}
else
{
I2CONSET = 0x10; //Stop condition
lock = 0; //Signal end of I2C activity

}
break;

case (0x58): //Data Received, Not Ack
I2CONSET = 0x20; // Resend Start condition
break;

default :
break;

}

I2CONCLR = 0x08; //Clear I2C interrupt flag
VICVectAddr = 0x00000000; //Clear interrupt in

}
/********************************************************************/
void sonar_call(void)
{
unsigned int data1 = 0;
unsigned int i= 0;
I2CTransferByte(0xE0,0x00,1,message); //write data to the I2C Memory

////////////// delay ems ///////////////////////////

I2CTransferByte(0xE0,0x02,1); //write data to the I2C Memory
I2CTransferByte(0xE1,0x02,2); //read higher byte
I2CTransferByte(0xE1,0x03,1); //read lower data

}

/*******************************************************************/
int main(void)
{

lock = 0; //Initilise the lock flag

VICVectCntl1 = 0x00000029; //select a priority slot for a given interrupt
VICVectAddr1 = (unsigned)I2CISR; //pass the address of the IRQ into the VIC slot
VICIntEnable = 0x00000200; //enable interrupt

PINSEL0 = 0x55; //Switch GPIO to I2C pins

I2SCLH = 1875; //Set bit rate = 40khz
I2SCLL = 1875;

while(1)
{
sonar_call();

}
}

________________________________
From: donhamilton2002
To: l...
Sent: Tuesday, 17 March, 2009 5:05:14 PM
Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
--- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
> hi,
> i have interface srf08-ultrasonic sensor with i2c bus of lpc2138.but i am facing communication problem with this interfacing. can anybody give me sample code of srf08-ultrasonic sensor with lpc21xx??
>
> thanks
> viren

It would be much easier to post your code here.

I am sure you did something wrong in your code, lets see it..

don

Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/



--- In l..., Viren Dobariya wrote:
>
> hi,
> see the code of sfr08.
> and guide me.

What compiler are you using ?

What .h files are you using ?

What exactly are you seeing ?

Do you have a O'scope or logic analyzer ?

Have you tried an I2C EEPROM ?

Do you have a data sheet for the sft08 ?
( please link to it )

don

Since you do not set explicitly APB divider, I assume it is
default 1/4 of CPU speed. Assuming CPU speed at 60 MHz,
if you want to get 40 kHz I2C, the divider should be 187.5:

40000 = (0.25*60000000) / (187.5 + 187.5)

I use SRF10 (suppose to be the same as SRF08) at 100kHz on LPC2368 and it works OK (electrically), but device itself is rather crappy. I did not check your other code. Start with the correct dividers first.
--- In l..., Viren Dobariya wrote:
>
> hi,
> see the code of sfr08.
> and guide me.
>
> Thanks
> viren
> ///////////////*************************/////////////////////////////////
> unsigned char message[1] = {0x51};
>
>
> //unsigned char message1[1] = {0x01};
> //unsigned char messageIn[4];
> unsigned char *I2CData,
> I2Counter,
> I2CAddress,
> MemAddress,
> lock; //Define Function prototypes and Global variables
> unsigned int data = 0;
> /**************************************************************/
> void I2CTransferByte(unsigned int I2CAddr,unsigned char MemAddr,unsigned char count,...)
> {
> va_list ap;
> va_start(ap,count);
>
> while(lock == 1) //Wait for interrupt to signal end of I2C activity
> {
> ;
> }
> lock = 1; //Set I2C bus as active
>
> I2CAddress = I2CAddr; //Place address and data in Globals
>
> to be used by the interrupt
> if(count >0)
> {
> I2CData = va_arg(ap,unsigned char *);
> }
> I2Counter = count;
> MemAddress = MemAddr;
> I2CONCLR = 0x000000FF; //Clear all I2C settings
> I2CONSET = 0x00000040; //Enable the I2C interface
> I2CONSET = 0x00000020; //Start condition
> va_end(ap);
> }
>
> /**************************************************************/
> void I2CISR (void) __irq //I2C interrupt routine
> {
> switch (I2STAT) //Read result code and switch to next
>
> action
> {
> // Start and Send byte conditions
>
> case ( 0x08): //Start bit
> I2CONCLR = 0x20; //Clear start bit
> I2DAT = I2CAddress; //Send address and write bit
> break;
>
> case ( 0x10): //Start bit
> I2CONCLR = 0x20; //Clear start bit
> I2DAT = I2CAddress; //Send address and write bit
> break;
>
> case (0x18): //Slave address+W, ACK
> I2DAT = MemAddress; //Write Mem,ory start address to tx register
> break;
>
> case (0x20): //Salve address +W, Not ACK
> I2DAT = I2CAddress; //Resend address and write bi
> break;
>
> case (0x28):
> if(I2Counter-->0) //Data sent, Ack
> {
> I2DAT = *I2CData; //Write data to tx register
> I2CData++;
> }
> else
> {
> I2CONSET = 0x10; //Stop condition
> lock = 0; //Signal end of I2C activity
> }
> break;
>
> case (0x30) : //Data sent, NOT Ack
> I2DAT = *I2CData; //Write data to tx register
> break;
>
>
> //Receive byte conditions
>
> case (0x40) : //Slave Address +R, ACK
> I2CONSET = 0x04; //Enable ACK for data byte
> break;
>
> case (0x48) : //Slave Address +R, Not Ack
> I2CONSET = 0x20; //Resend Start condition
> break;
>
> case (0x50) : //Data Received, ACK
> if(I2Counter-->0)
> {
> to_hyper(I2DAT);
> data = I2DAT;
> //*I2CData = I2DAT;
> //I2CData++;
> }
> else
> {
> I2CONSET = 0x10; //Stop condition
> lock = 0; //Signal end of I2C activity
>
>
> }
> break;
>
> case (0x58): //Data Received, Not Ack
> I2CONSET = 0x20; // Resend Start condition
> break;
>
> default :
> break;
>
> }
>
> I2CONCLR = 0x08; //Clear I2C interrupt flag
> VICVectAddr = 0x00000000; //Clear interrupt in
>
> }
> /********************************************************************/
> void sonar_call(void)
> {
> unsigned int data1 = 0;
> unsigned int i= 0;
> I2CTransferByte(0xE0,0x00,1,message); //write data to the I2C Memory
>
> ////////////// delay ems ///////////////////////////
>
> I2CTransferByte(0xE0,0x02,1); //write data to the I2C Memory
> I2CTransferByte(0xE1,0x02,2); //read higher byte
> I2CTransferByte(0xE1,0x03,1); //read lower data
>
> }
>
> /*******************************************************************/
> int main(void)
> {
>
> lock = 0; //Initilise the lock flag
>
> VICVectCntl1 = 0x00000029; //select a priority slot for a given interrupt
> VICVectAddr1 = (unsigned)I2CISR; //pass the address of the IRQ into the VIC slot
> VICIntEnable = 0x00000200; //enable interrupt
>
> PINSEL0 = 0x55; //Switch GPIO to I2C pins
>
> I2SCLH = 1875; //Set bit rate = 40khz
> I2SCLL = 1875;
>
> while(1)
> {
> sonar_call();
>
> }
> }
> ________________________________
> From: donhamilton2002
> To: l...
> Sent: Tuesday, 17 March, 2009 5:05:14 PM
> Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
> --- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
> >
> >
> > hi,
> > i have interface srf08-ultrasonic sensor with i2c bus of lpc2138.but i am facing communication problem with this interfacing. can anybody give me sample code of srf08-ultrasonic sensor with lpc21xx??
> >
> > thanks
> > viren
>
> It would be much easier to post your code here.
>
> I am sure you did something wrong in your code, lets see it..
>
> don
>
> Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/
>
>
>

hi,
i tried with
I2SCLL = 188;

I2SCLH = 188;

but behavior of the sensor is same.
please check my i2ctransferbyte() function and sonar_call() function.
code is given below.

thanks
________________________________
From: this_ip_address
To: l...
Sent: Thursday, 19 March, 2009 2:20:03 AM
Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
Since you do not set explicitly APB divider, I assume it is
default 1/4 of CPU speed. Assuming CPU speed at 60 MHz,
if you want to get 40 kHz I2C, the divider should be 187.5:

40000 = (0.25*60000000) / (187.5 + 187.5)

I use SRF10 (suppose to be the same as SRF08) at 100kHz on LPC2368 and it works OK (electrically) , but device itself is rather crappy. I did not check your other code. Start with the correct dividers first.

--- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
>
> hi,
> see the code of sfr08.
> and guide me.
>
> Thanks
> viren
> //////////// ///****** ********* ********* *//////// ///////// ///////// ///////
> unsigned char message[1] = {0x51};
> //unsigned char message1[1] = {0x01};
> //unsigned char messageIn[4] ;
> unsigned char *I2CData,
> I2Counter,
> I2CAddress,
> MemAddress,
> lock; //Define Function prototypes and Global variables
> unsigned int data = 0;
> /*********** ********* ********* ********* ********* ********* ******/
> void I2CTransferByte( unsigned int I2CAddr,unsigned char MemAddr,unsigned char count,...)
> {
> va_list ap;
> va_start(ap, count);
>
> while(lock == 1) //Wait for interrupt to signal end of I2C activity
> {
> ;
> }
> lock = 1; //Set I2C bus as active
>
> I2CAddress = I2CAddr; //Place address and data in Globals
>
> to be used by the interrupt
> if(count >0)
> {
> I2CData = va_arg(ap,unsigned char *);
> }
> I2Counter = count;
> MemAddress = MemAddr;
> I2CONCLR = 0x000000FF; //Clear all I2C settings
> I2CONSET = 0x00000040; //Enable the I2C interface
> I2CONSET = 0x00000020; //Start condition
> va_end(ap);
> }
>
> /*********** ********* ********* ********* ********* ********* ******/
> void I2CISR (void) __irq //I2C interrupt routine
> {
> switch (I2STAT) //Read result code and switch to next
>
> action
> {
> // Start and Send byte conditions
>
> case ( 0x08): //Start bit
> I2CONCLR = 0x20; //Clear start bit
> I2DAT = I2CAddress; //Send address and write bit
> break;
>
> case ( 0x10): //Start bit
> I2CONCLR = 0x20; //Clear start bit
> I2DAT = I2CAddress; //Send address and write bit
> break;
>
> case (0x18): //Slave address+W, ACK
> I2DAT = MemAddress; //Write Mem,ory start address to tx register
> break;
>
> case (0x20): //Salve address +W, Not ACK
> I2DAT = I2CAddress; //Resend address and write bi
> break;
>
> case (0x28):
> if(I2Counter- ->0) //Data sent, Ack
> {
> I2DAT = *I2CData; //Write data to tx register
> I2CData++;
> }
> else
> {
> I2CONSET = 0x10; //Stop condition
> lock = 0; //Signal end of I2C activity
> }
> break;
>
> case (0x30) : //Data sent, NOT Ack
> I2DAT = *I2CData; //Write data to tx register
> break;
> //Receive byte conditions
>
> case (0x40) : //Slave Address +R, ACK
> I2CONSET = 0x04; //Enable ACK for data byte
> break;
>
> case (0x48) : //Slave Address +R, Not Ack
> I2CONSET = 0x20; //Resend Start condition
> break;
>
> case (0x50) : //Data Received, ACK
> if(I2Counter- ->0)
> {
> to_hyper(I2DAT) ;
> data = I2DAT;
> //*I2CData = I2DAT;
> //I2CData++;
> }
> else
> {
> I2CONSET = 0x10; //Stop condition
> lock = 0; //Signal end of I2C activity
> }
> break;
>
> case (0x58): //Data Received, Not Ack
> I2CONSET = 0x20; // Resend Start condition
> break;
>
> default :
> break;
>
> }
>
> I2CONCLR = 0x08; //Clear I2C interrupt flag
> VICVectAddr = 0x00000000; //Clear interrupt in
>
> }
> /*********** ********* ********* ********* ********* ********* ********* ***/
> void sonar_call(void)
> {
> unsigned int data1 = 0;
> unsigned int i= 0;
> I2CTransferByte( 0xE0,0x00, 1,message) ; //write data to the I2C Memory
>
> //////////// // delay ems //////////// ///////// //////
>
> I2CTransferByte( 0xE0,0x02, 1); //write data to the I2C Memory
> I2CTransferByte( 0xE1,0x02, 2); //read higher byte
> I2CTransferByte( 0xE1,0x03, 1); //read lower data
>
> }
>
> /*********** ********* ********* ********* ********* ********* ********* **/
> int main(void)
> {
>
> lock = 0; //Initilise the lock flag
>
> VICVectCntl1 = 0x00000029; //select a priority slot for a given interrupt
> VICVectAddr1 = (unsigned)I2CISR; //pass the address of the IRQ into the VIC slot
> VICIntEnable = 0x00000200; //enable interrupt
>
> PINSEL0 = 0x55; //Switch GPIO to I2C pins
>
> I2SCLH = 1875; //Set bit rate = 40khz
> I2SCLL = 1875;
>
> while(1)
> {
> sonar_call() ;
>
> }
> }
> ____________ _________ _________ __
> From: donhamilton2002
> To: lpc2000@yahoogroups .com
> Sent: Tuesday, 17 March, 2009 5:05:14 PM
> Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
> --- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
> >
> >
> > hi,
> > i have interface srf08-ultrasonic sensor with i2c bus of lpc2138.but i am facing communication problem with this interfacing. can anybody give me sample code of srf08-ultrasonic sensor with lpc21xx??
> >
> > thanks
> > viren
>
> It would be much easier to post your code here.
>
> I am sure you did something wrong in your code, lets see it..
>
> don
>
> Add more friends to your messenger and enjoy! Go to http://messenger. yahoo.com/ invite/
>
>
>

Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/



i am using keil uvision3.
i am using lpc21xx.h provided by keil compiler
yes i have logic analayzer.
no, i never tried eeprom of i2c.how can i use it??

data sheet of srf08 is attached with this mail

thanks

________________________________
From: donhamilton2002
To: l...
Sent: Wednesday, 18 March, 2009 5:59:21 PM
Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
--- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
>
> hi,
> see the code of sfr08.
> and guide me.

What compiler are you using ?

What .h files are you using ?

What exactly are you seeing ?

Do you have a O'scope or logic analyzer ?

Have you tried an I2C EEPROM ?

Do you have a data sheet for the sft08 ?
( please link to it )

don

Unlimited freedom, unlimited storage. Get it now, on http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/



On Thu, 19 Mar 2009, Viren Dobariya wrote:

> i tried with

> I2SCLL = 188;
> I2SCLH = 188;

Be careful- I've seen a few lpc21xx.h files that have the wrong type for
these two registers (they're coded as either ushorts or uchars and they
need to be ulongs else the compiler writes 0 to the business end of the
register).

-Kenny

--
Kenneth R. Crudup Sr. SW Engineer, Scott County Consulting, Los Angeles
O: 3630 S. Sepulveda Blvd. #138, L.A., CA 90034-6809 (888) 454-8181
I really don't have time to debug your code, but it looks to me
that your I2C states screwed up. Roghly, this thing should work
like below. You should see states in this sequence. If not
something went wrong. You need an additional timer interrupts
to (1) define ranging frequency and (2) define ranging time interval
on state 5.

1. State 0x00
Prepare I2C to master transmitter mode.
Issue start condition.

2. State 0x08
Send ADDRESS+W

3. State 0x18
Write register address - 0 in simplest case.

4. State 0x28
Write command. Indicate end of data

5. Wait for ranging complete
This depends on gain and ranging registers.
Default 65 ms. If you try to read before sonar finished
ranging, you will get I2C error.

6. You should be in master read mode. Issue START.

7. State 0x10
Send ADDRESS+R

8. State 0x40
Response started. Send ACK

9. State 0x50
First byte

10. State 0x58
Second byte. Stop. Return result to user.

11. Timeout before next ranging.
Go to 1.
--- In l..., Viren Dobariya wrote:
>
> i am using keil uvision3.
> i am using lpc21xx.h provided by keil compiler
> yes i have logic analayzer.
> no, i never tried eeprom of i2c.how can i use it??
>
> data sheet of srf08 is attached with this mail
>
> thanks
> ________________________________
> From: donhamilton2002
> To: l...
> Sent: Wednesday, 18 March, 2009 5:59:21 PM
> Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
> --- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
> >
> > hi,
> > see the code of sfr08.
> > and guide me.
>
> What compiler are you using ?
>
> What .h files are you using ?
>
> What exactly are you seeing ?
>
> Do you have a O'scope or logic analyzer ?
>
> Have you tried an I2C EEPROM ?
>
> Do you have a data sheet for the sft08 ?
> ( please link to it )
>
> don
>
> Unlimited freedom, unlimited storage. Get it now, on http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/
>
>
>

hi,
in my interrupt routine of srf08,
1)in the case 0x08: i am writing 0xE0(I2cAddress of srf08)

2)in the case 0x18: i am writing 0x00(reg addr of srf08)
3)in the case 0x28: i am writing 0x51(to define range)
and I2CONSET = 0X10 (stop condition)

so,i move out from interrupt routine.
now i generate delay of 65ms using timer,but it is outside the i2c interrupt routine.
uptil now is it ok??
please tell me,so i can go ahead.
datasheet of srf08 is attached.

thanks

________________________________
From: this_ip_address
To: l...
Sent: Friday, 20 March, 2009 12:47:59 AM
Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
I really don't have time to debug your code, but it looks to me
that your I2C states screwed up. Roghly, this thing should work
like below. You should see states in this sequence. If not
something went wrong. You need an additional timer interrupts
to (1) define ranging frequency and (2) define ranging time interval
on state 5.

1. State 0x00
Prepare I2C to master transmitter mode.
Issue start condition.

2. State 0x08
Send ADDRESS+W

3. State 0x18
Write register address - 0 in simplest case.

4. State 0x28
Write command. Indicate end of data

5. Wait for ranging complete
This depends on gain and ranging registers.
Default 65 ms. If you try to read before sonar finished
ranging, you will get I2C error.

6. You should be in master read mode. Issue START.

7. State 0x10
Send ADDRESS+R

8. State 0x40
Response started. Send ACK

9. State 0x50
First byte

10. State 0x58
Second byte. Stop. Return result to user.

11. Timeout before next ranging.
Go to 1.

--- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
>
> i am using keil uvision3.
> i am using lpc21xx.h provided by keil compiler
> yes i have logic analayzer.
> no, i never tried eeprom of i2c.how can i use it??
>
> data sheet of srf08 is attached with this mail
>
> thanks
> ____________ _________ _________ __
> From: donhamilton2002
> To: lpc2000@yahoogroups .com
> Sent: Wednesday, 18 March, 2009 5:59:21 PM
> Subject: [lpc2000] Re: regarding srf08(ultrasonic sensor) comm prob with lpc2138
> --- In lpc2000@yahoogroups .com, Viren Dobariya wrote:
> >
> > hi,
> > see the code of sfr08..
> > and guide me.
>
> What compiler are you using ?
>
> What ..h files are you using ?
>
> What exactly are you seeing ?
>
> Do you have a O'scope or logic analyzer ?
>
> Have you tried an I2C EEPROM ?
>
> Do you have a data sheet for the sft08 ?
> ( please link to it )
>
> don
>
> Unlimited freedom, unlimited storage. Get it now, on http://help. yahoo.com/ l/in/yahoo/ mail/yahoomail/ tools/tools- 08.html/
>
>
>

Add more friends to your messenger and enjoy! Go to http://messenger.yahoo.com/invite/