Skip to content

Instantly share code, notes, and snippets.

@Robotonics Robotonics/srf02.c
Created Jan 27, 2013

Embed
What would you like to do?
I2C driver for SRF02
/*
* srf02.c
*
* Created on: 26 Jan 2013
* Author: David
*/
//includes
#include "lm4f120h5qr.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/i2c.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "I2C_Stellaris_API.h"
#include "utils/uartstdio.h"
#include "driverlib/sysctl.h"
// variables
int srf02datalow=0;
int srf02datahigh=0;
int readings[10]={0,0,0,0,0,0,0,0,0,0}; // the readings from SRF02 pings
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0;
int i=0;
// functions
int read_srf02(void){
for(i = 0; i < 10; i++){
total= total - readings[index];
// read from the sensor:
I2CRegWrite(I2C3_MASTER_BASE, 0xF0,0x00,0x51);
SysCtlDelay(800000);
readings[index] = I2CRegRead(I2C3_MASTER_BASE,0xF0,0x03);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= 10)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / 10;
}
return average;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.