Skip to content

Instantly share code, notes, and snippets.

@Robotonics Robotonics/main.c
Created Jan 12, 2013

Embed
What would you like to do?
#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"
unsigned long srf02datalow=0;
unsigned long srf02datahigh=0;
void main(void) {
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); // 40MHz system clock
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // enable clock for peripheral (PORTF)
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); // config PF1(red) 2(blue) 3(green) as output.
I2CSetup(I2C3_MASTER_BASE,false); // false = 100KHz , true = 400KHz
while(1)
{
I2CRegWrite(I2C3_MASTER_BASE, 0xF0,0x00,0x51); // slave address of SRF02 is 0xE0 / 0xF0 is the address right shifted by 1 bit (7bit)
srf02datalow=I2CRegRead(I2C3_MASTER_BASE,0xF0,0x03); // reading from register 3 gives Low Byte of ranging
if (srf02datalow<0x1E){
SysCtlDelay(4000000);
GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x02); // switch on red at 0x1E (30cms)
}
else {
SysCtlDelay(4000000);
GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x08); // switch on green
}
};
}
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.