Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

SRF02 ranger interface via USART (Serial)

Status
Not open for further replies.

haseeb123

New Member
Hello

Please can someone help me to get the SRF02 ranger to register correctly by sending the correct distance
in CM. At the moment it is only replying back '0' and its LED flashs according to my program loop.

First I used the ranger via I2C interface and that worked well. Now I changed that to serial interface via
changing tie the SRF02 pin 4 to zero volts.

Im using the PIC18F2620 internal OSC @ 4MHz programming in CCS compiler. Below is my small test
program. Please can someone help me to get the SRF02 ranger to register correctly by sending the correct
distance to my serial port and print that out to hypertermial.

Thanks
Haseeb


Code:
#include <18F2620.h>
#include <stdio.h> 

#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,NOPUT, NOPBADEN 
 
// Set Speed to 4Mhz
#use delay(clock=4000000)

// Sonar
#use rs232(baud=9600, stop=2, parity=N, xmit=PIN_B2, rcv=PIN_C7, STREAM=Sonar)

// PC
#use rs232(baud=9600, xmit=PIN_B4, rcv=PIN_B3 ,STREAM=PC)



int range,

	sonar_address = 0x00, //sonar address (factory default)

	sonar_command = 0x54; //Enable Real Ranging Mode command '0x54' for... 
						  //Result in centimeters & automatically Tx range back to...
						  //controller as soon as ranging is complete


void main(void)
{

	delay_ms(100); //startup delay




		while(true)
		{

			fputc(sonar_address, Sonar);  //Sonar address '0'
		
			fputc(sonar_command, Sonar);  //data 0x54
	
			
			while(!kbhit(Sonar)); //wait for char in USART buffer		

			range = fgetc(Sonar);	//dump range in variable	
	
	
			fprintf(PC,"Range is %u cm\n\r",range);	
			delay_ms(250);


		}




}
 
Not an expert but I did read this from the tech data for the sensor, maybe this will solve the problem..

To send a command to the SRF02, you need to send two bytes. The first is the SRF02's address 0 to 15, (0x00 to 0x0F) and then the actual command itself - see below. The are three commands to initiate a ranging (80 to 82), to produce the result in inches, centimeters or microseconds. These three commands don't Tx the result back to your controller. You should wait 70mS and then use command 94 to get the result of the ranging
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top