I am having trouble interfacing with the SRF02 sensor. If someone has some experience with this sensor, I would like to know what kind of code I should use to test it.
I have confirmation that the following code works:
// Code for testing SRF02 sensor
import stamp.core.*;
public class test
{ · static final int pinTX = CPU.pin0; · static final int pinRX = CPU.pin1; · static Uart srfTX = new Uart(Uart.dirTransmit,pinTX,Uart.invert,Uart.speed9600,Uart.stop2); · static Uart srfRX = new Uart(Uart.dirReceive,pinRX,Uart.invert,Uart.speed9600,Uart.stop2);
· public static void main() · { ··· int data;
··· srfTX.sendByte(0x00); //address ····srfTX.sendByte(0x50); //command Real Ranging Mode (result in inches) ··· while(true) ··· { ······· CPU.delay(10500); //wait 1 second ······· srfTX.sendByte(0x00); //address ······· srfTX.sendByte(0x5E); //command Get Range, returns 2 bytes ······· data = srfRX.receiveByte() << 8; //receive MSB ······· data += srfRX.receiveByte();·· //receive LSB ······· System.out.println(data); ···· } · }
}
Comments
// Code for testing SRF02 sensor
import stamp.core.*;
public class test
{
· static final int pinTX = CPU.pin0;
· static final int pinRX = CPU.pin1;
· static Uart srfTX = new Uart(Uart.dirTransmit,pinTX,Uart.invert,Uart.speed9600,Uart.stop2);
· static Uart srfRX = new Uart(Uart.dirReceive,pinRX,Uart.invert,Uart.speed9600,Uart.stop2);
· public static void main()
· {
··· int data;
··· srfTX.sendByte(0x00); //address
····srfTX.sendByte(0x50); //command Real Ranging Mode (result in inches)
··· while(true)
··· {
······· CPU.delay(10500); //wait 1 second
······· srfTX.sendByte(0x00); //address
······· srfTX.sendByte(0x5E); //command Get Range, returns 2 bytes
······· data = srfRX.receiveByte() << 8; //receive MSB
······· data += srfRX.receiveByte();·· //receive LSB
······· System.out.println(data);
···· }
· }
}
regards peter