Ultrasonic Ranger

 

Driving the Robot Electronics SRF02 ultrasonic ranger from a PIC 16F877A using Oshonsoft Compiled Basic

Notes .

After much frustration I discovered that the plated through hole of my SRF02 Mode pin did not connect the two sides of the circuit. A wire through the hole soldered to both sides got things working
.

i2c Mode

1) Mode pin open-circuit.
2)
I2CREAD_DELAYUS was set to 80 microseconds - any delay below 20 gave range readings half those expected and some random errors
3) I had to change the address from E0 to F2 since my DS-SCX16S-0 had address E0


'I2C_ultrasonic_delay80mu_f2.bas NB new address set (servo chip is at e0)!!
'test the robot-electronics SRF02 rangefinder I2C

TRISC = 0 'all port C outputs
Symbol sda = PORTC.4 'setSDA for i2c
Symbol scl = PORTC.3 'setSCL for i2c
'prepare to use LCD later
Define LCD_LINES = 4
Define LCD_CHARS = 20
Lcdinit

Define I2CREAD_DELAYUS = 80 '0 and 20 give 1/2 true range, 50 OK
'Define I2CCLOCK_STRETCH = 2 - full speed OK

Dim range As Word

loop:
'--------------------------------keep reading the ultrasonic ranger

I2CWrite sda, scl, 0xf2, 0x00, 0x51 'read range in cm

WaitMs 100

I2CRead sda, scl, 0xf2, 0x02, range.HB, range.LB

WaitMs 100

Lcdcmdout LcdClear
Lcdout " "
Lcdcmdout LcdLine2Home
Lcdout "range ", #range, " cm"
Lcdcmdout LcdLine3Home
Lcdout " "

WaitMs 100

Goto loop


rs232 Serial Mode (this test program also drives a CMPS03 Compass)

1) Mode pin shorted to earth.
2)
SEROUT_DELAYUS was set to 1000 microseconds - if not present it failed to run
3) This test used address E0 (default as delivered)
4) The program uses the Oshonsoft basic software UART since the hardware UART is used for the main radio link to the house
6) use
Serout not SeroutInv - the latter is used if a chip is connected to a bipolar true rs232 signal via a diode clamp that just lets through the positive part of the rs232 signal. Here we are connecting directly to the PIC port wires.
(
If you connect PIC--TTL---MAX232--a---MAX2342--TTL--device then the bipolar signals at a are an inverted version of the TTL signals)


'ultrasonic_and_compass.bas
'drive ultrasonic ranger by software rs232 NOT inverted
'connect directly to PIC D2 and D3 (or via two max232 converters)

TRISC = 0 'all port C outputs
Symbol sda = PORTC.4 'setSDA for i2c
Symbol scl = PORTC.3 'setSCL for i2c
'prepare to use LCD later
Define LCD_LINES = 4
Define LCD_CHARS = 20
Lcdinit
Define SEROUT_DELAYUS = 10000 'allows a string of output bytes with no errors
Define I2CREAD_DELAYUS = 80 'for the slow device compass (50 OK, 10 fails)
'Define I2CCLOCK_STRETCH = 2 'not needed for compass

Dim bearing_word As Word
Dim bearing_degrees As Word
Dim range As Word

loop:
'--------------------------------keep reading the compass and SRF02 and display result

I2CRead sda, scl, 0xc0, 0x02, bearing_word.HB, bearing_word.LB
WaitMs 50

bearing_degrees = bearing_word / 10

Serout PORTD.2, 9600, 0x00, 0x54 'send cm range when available

Serin PORTD.3, 9600, range.HB, range.LB 'open the input BEFORE the pulse is expected

WaitMs 200

Lcdcmdout LcdClear
Lcdout "bearing ", #bearing_degrees, " range ", #range

WaitMs 50

Goto loop