by roboard » Fri Jul 10, 2009 6:14 am
by roboard
Fri Jul 10, 2009 6:14 am
Some people asked us how to interfacing with SRF08/SRF10 UltraSonic Ranger by RoBoard. This is easy using RoBoIO library; the following is a brief sample code.
- Code: Select all
#include <stdio.h>
#include <conio.h>
#include <roboard.h>
//you need to change this function for Linux and DOS
void wait_ms(unsigned long ms) {
unsigned long nowtime = GetTickCount();
while ((GetTickCount() - nowtime) <= ms);
}
int main(void) {
unsigned b1, b2;
if (i2c_Initialize(I2CIRQ_DISABLE) == false)
{
printf("fail to initialize I2C (%s)!\n", roboio_GetErrMsg());
return -1;
}
printf("set I2C speed to 400Kbps\n\n");
i2c0_SetSpeed(I2CMODE_STANDARD, 400000L);
do
{
printf("send SRF ranging command\n");
//START with address 0xe0>>1 to write 2 bytes
i2c0master_StartN(0xe0>>1, I2C_WRITE, 2);
//Note: the SRF device address 0xe0 actually contains the I2C r/w bit,
// so we divide it by 2 to get the correct I2C slave address;
// see the I2C lib section of RoBoard SW intro. slide for more details.
i2c0master_WriteN(0); //set SRF command register
i2c0master_WriteN(81); //set ranging Mode - result in cm
wait_ms(100); //wait 100ms
printf("read SRF ranging data......\n");
//START with address 0xe0>>1 to write 1 bytes
i2c0master_StartN(0xe0>>1, I2C_WRITE, 1);
//set RESTART to read 2 bytes after I2C writing
i2c0master_SetRestartN(I2C_READ, 2);
i2c0master_WriteN(2); //set 1st SRF range register
b1 = i2c0master_ReadN(); //read echo high byte
b2 = i2c0master_ReadN(); //read echo low byte
printf("result = %u\n\n", b1*256 + b2);
} while (getch() != 27); //press ESC to quit
i2c_Close();
return 0;
}
The above sample code is for WinXP and WinCE; if you want to compile it at Linux or DOS, just change the wait_ms() function.
Note: in our test, SRF08/SRF10 can connect to RoBoard's I2C0_SCL, I2C0_SDA pins without pull-up resistors and run on 400Kbps. But for other I2C sensors, pull-up resistors may be necessary for fast I2C transmission (>100Kbps).
Some people asked us how to interfacing with SRF08/SRF10 UltraSonic Ranger by RoBoard. This is easy using RoBoIO library; the following is a brief sample code.
- Code: Select all
#include <stdio.h>
#include <conio.h>
#include <roboard.h>
//you need to change this function for Linux and DOS
void wait_ms(unsigned long ms) {
unsigned long nowtime = GetTickCount();
while ((GetTickCount() - nowtime) <= ms);
}
int main(void) {
unsigned b1, b2;
if (i2c_Initialize(I2CIRQ_DISABLE) == false)
{
printf("fail to initialize I2C (%s)!\n", roboio_GetErrMsg());
return -1;
}
printf("set I2C speed to 400Kbps\n\n");
i2c0_SetSpeed(I2CMODE_STANDARD, 400000L);
do
{
printf("send SRF ranging command\n");
//START with address 0xe0>>1 to write 2 bytes
i2c0master_StartN(0xe0>>1, I2C_WRITE, 2);
//Note: the SRF device address 0xe0 actually contains the I2C r/w bit,
// so we divide it by 2 to get the correct I2C slave address;
// see the I2C lib section of RoBoard SW intro. slide for more details.
i2c0master_WriteN(0); //set SRF command register
i2c0master_WriteN(81); //set ranging Mode - result in cm
wait_ms(100); //wait 100ms
printf("read SRF ranging data......\n");
//START with address 0xe0>>1 to write 1 bytes
i2c0master_StartN(0xe0>>1, I2C_WRITE, 1);
//set RESTART to read 2 bytes after I2C writing
i2c0master_SetRestartN(I2C_READ, 2);
i2c0master_WriteN(2); //set 1st SRF range register
b1 = i2c0master_ReadN(); //read echo high byte
b2 = i2c0master_ReadN(); //read echo low byte
printf("result = %u\n\n", b1*256 + b2);
} while (getch() != 27); //press ESC to quit
i2c_Close();
return 0;
}
The above sample code is for WinXP and WinCE; if you want to compile it at Linux or DOS, just change the wait_ms() function.
Note: in our test, SRF08/SRF10 can connect to RoBoard's I2C0_SCL, I2C0_SDA pins without pull-up resistors and run on 400Kbps. But for other I2C sensors, pull-up resistors may be necessary for fast I2C transmission (>100Kbps).
Last edited by roboard on Mon Aug 17, 2009 1:05 pm, edited 1 time in total.