_______________________________________________________________________ > < > SRF08 (Ultra sonic range finder) driver < > for < > PICos18 release 2.09 < > < > PICos18 - Real-time kernel for PIC18 family < > < > < > www.picos18.com www.pragmatec.net < >_______________________________________________________________________< This file contains all necessary informations to use properly the SRF08 ultrasonic range finder driver for PICos18 v2.xx. This driver lets you design a multi-task application using this high performance ultrasonic range finder. This sonar has to be access through the I2C interface and this driver allows you to have a safe access to this peripheral without woring about multiple I2C bus access. _______________________________________________________________________ > < > I - Zip file content < >_______________________________________________________________________< The drv_SRF08 zip file contains : - drv_SRF08.c : driver - drv_SRF08.h : his header file - drv_SRF08.txt : this file - change_address.c : simple program for changing the I2C address - change_address.h : his header file - new_address.h : select current and new address of the SRF08 To use the rangefinder and the light intensity sensor driver, first unzip the file. Place only the C and H files of the driver in your project directory, the last 3 files are only necessary for building a program if it is necessary to change the I2C address of the SRF08. If you intend to change the I2C address you might consult section V for further explanations. This driver is supposed to be use with PICos18 and has been tested with MPLAB 7.30 and C18 v3.00. This driver is suitable for PIC18 with SRF08 sonar ranger from Devantech. _______________________________________________________________________ > < > II - Hardware settings < >_______________________________________________________________________< Have a look at the SRF08 datasheet to connect your device properly to the I2C bus. If you already have a I2C install you don't have to plug additional pullup resistors. A basic installation consists to connect the device to the power lines (VDD and VSS) then to connect the SDA and SCL lines to the PIC18 (with pullup resistors if needed), the remaining pin 4 is a "Do Not Connect". _______________________________________________________________________ > < > III - TASCDESC settings < >_______________________________________________________________________< The SRF08 driver needs the I2C driver to be install at first. Have a look at the drv_I2C.txt file to learn more on how to install such a driver. The SRF08 driver is not exactly a driver but much more a library (a set of functions) that let you interface this SRF08 sensor without wasting time to validate your own code during many hours. Then once you have properly install the I2C driver you don't need to specify anything else in the tascdesc.c file. _______________________________________________________________________ > < > IV - SRF08 basic usage < >_______________________________________________________________________< First of all you need to declare two I2C_message structures in your task. It's mandatory for all the tasks that need to use the I2C driver : I2C_message_t I2Cstart_msg; I2C_message_t I2Cget_msg; In the TASK0 task of the MyApp directory, include the driver header: #include "drv_SRF08.h" Define the SRF08_address field for both SRF08Start_t and SRF08Get_t structures with the I2C address of the current target sensor device. In this example : SRF08_DEFAULT is the factory default address this sensors are shipped with, it is the address: 0xE0. Using the modify address procedure it is possible to choose any even 16 addresses from 0xE0 to 0xFE. Select the scale unit you intend to read the distance of the echoes in the conversion_unit field of the SRF08Start_t structure. In this example : CONVERT_cm gives the distance of the echoes in cm, it's also possible to select it in inches or usec. See hereafter a basic example to have a correct access to the SRF08: no need to initialize the device, simply start the sonar ranging and after at least 65ms get the light intensity and the first echo data. You could also request further distance echoes simply increasing the size of the pData[] buffer in the SRF08_GetRanging( ) function and the size of the SRF08_buffer field of the SRF08Get_t structure. #include "define.h" #include "drv_SRF08.h" /********************************************************************** * Definition dedicated to the local functions. **********************************************************************/ #define ALARM_SRF08 0 unsigned char lightsensor; unsigned int first_echo; I2C_message_t I2Cstart_msg; I2C_message_t I2Cget_msg; SRF08Start_t I2Cstart_ranging; SRF08Get_t I2Cget_ranging; /********************************************************************** * ----------------------------- TASK0 -------------------------------- * * First task of the tutorial. * **********************************************************************/ TASK(TASK0) { I2Cstart_ranging.SRF08_address = SRF08_DEFAULT; I2Cstart_ranging.conversion_unit = CONVERT_cm; I2Cget_ranging.SRF08_address = SRF08_DEFAULT; SetRelAlarm(ALARM_SRF08, 500, 200); WaitEvent(ALARM_EVENT); ClearEvent(ALARM_EVENT); while (1) { SRF08_StartRanging(&I2Cstart_msg, &I2Cstart_ranging); /* Start the sonar ranging in cm */ WaitEvent(ALARM_EVENT); ClearEvent(ALARM_EVENT); SRF08_GetRanging(&I2Cget_msg, &I2Cget_ranging); /* Get the light intensity and */ /* the first echo distance in cm */ lightsensor = I2Cget_ranging.SRF08_buffer[0]; first_echo = ((I2Cget_ranging.SRF08_buffer[1] << 8) + I2Cget_ranging.SRF08_buffer[2]); } } _______________________________________________________________________ > < > V - SRF08 I2C address change procedure < >_______________________________________________________________________< To use more than one SRF08 sensor on the I2C bus it is required to give unique addresses to them, follow this procedure for the scope: This operation must be performed with only one SRF08 device present on the I2C bus. Build a simple application including the change_address.c file, the change_address.h file and the new_address.h file. The procedure requires 2 steps: 1) define the current I2C address assigned to the SRF08 device 2) define the new I2C address it is intended to provide the SRF08 with These 2 defines are located for ease of use in the new_address.h file. Assuming you are going to change address in a factory shipped sensor with his I2C default address, simply define CURRENT_ADDRESS with the SRF08_DEFAULT and define SRF08_NEW_ADDRESS with one of the 16 available addresses, for example : SRF08_ADDRESS_5. Program this simple application in your target, if the operation is succesful the SRF08 LED will turn his light on permanently. At the next power-up the sensor will notify his new address with five LED flashes, specificly 1 long flash followed by 4 short ones. At this point if you would change again his I2C address, this time the define for the CURRENT_ADDRESS has to be : SRF08_ADDRESS_5. Again define SRF08_NEW_ADDRESS with a different one at your choice. _______________________________________________________________________ > < > VI - Conclusion < >_______________________________________________________________________< The SRF08 is a high performance sensor that uses sonar to detect objects offering a wide range from 3cm to 6m, communication with this ultrasonic rangefinder is via the I2C bus. On the front of this detector there is a photocell sensor where the reading increases with the brightness. On-board there is also a status LED flashing whilst ranging. It also indicates at power-up its address: one long flash for the default address, followed for the other 15 available addresses by a consequent succesive numbers of short flashes. For changing the default I2C address only one SRF08 device must be connected on the bus,a sequence of 3 specific bytes followed from the new address perform this address change. /* End of File : drv_SRF08.txt */