Skip to content

Instantly share code, notes, and snippets.

@donghee donghee/tmp102.c
Last active Dec 15, 2015

Embed
What would you like to do?
Launchpad: tmp102
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/i2c.h"
#include "utils/uartstdio.h"
#define SLAVE_ADDRESS 0x48 // TMP102 I2C Address (ADDR0 is GND)
void initUART(void)
{
//enable portA
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//configure the pin multiplexing
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
//configure the type of the pins for uart tx/rx
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//init the console
UARTStdioInit(0);
}
int main(void){
unsigned char tx;
unsigned char rx[2];
SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
initUART();
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Set up the pins as I2C
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false);//false for 100kHz mode
I2CMasterEnable(I2C0_MASTER_BASE);
UARTprintf("Read TMP102(TEMP SENSOR) \n");
// start I2C transmit
// Set Point Register in TMP102
I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS, false);
tx = 0x00; // TMP102 Point Register is 0x00
I2CMasterDataPut(I2C0_MASTER_BASE, tx); // Point Register byte to be written
while(I2CMasterBusy(I2C0_MASTER_BASE)) {} // Check, the bus isn't busy (low?)
// Single Byte
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy(I2C0_MASTER_BASE)) {} //wait
// end I2C transmit
while(1)
{
// start I2C receive
// Read TEMPERATURE REGISTER
I2CMasterSlaveAddrSet(I2C0_MASTER_BASE, SLAVE_ADDRESS, true); // set to true since it's a read
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C0_MASTER_BASE)) {}
rx[0] = I2CMasterDataGet(I2C0_MASTER_BASE);
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
while(I2CMasterBusBusy(I2C0_MASTER_BASE)) {}
rx[1] = I2CMasterDataGet(I2C0_MASTER_BASE);
// end I2C receive
int temperature = ((rx[0] << 8) | rx[1]) >> 4;
int celsius = temperature*625;
UARTprintf("Temperature: %d\n", (int)(celsius/10000)-5);
//UARTprintf("rx[0]: %x\n", rx[0]);
//UARTprintf("rx[1]: %x\n", rx[1]);
SysCtlDelay(5000000);
}
return(0);
}
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.