Skip to content

Instantly share code, notes, and snippets.

@Robotonics
Created January 27, 2013 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robotonics/4651118 to your computer and use it in GitHub Desktop.
Save Robotonics/4651118 to your computer and use it in GitHub Desktop.
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