This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C123GH6PM: TM4C123GH6PM+adxl345+ccs

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hello,

I want to use I2c protocol using ADXL345. I am not able to get the proper output .I have attached the code below if any changes let me know

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#define ACCEL_SLAVE_ADDR 0x53
#define XOUT8 0x32
#define XOUT18 0x33
#define YOUT8 0x34
#define ZOUT8 0x36
int8_t Ax, Ay, Az,Ax1,Ay1;
int16_t datax,datay;
void InitI2C0(void)
{
//enable I2C module 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);

//reset module
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);

//enable GPIO peripheral that contains I2C 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

// Configure the pin muxing for I2C0 functions on port B2 and B3.
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);

// Select the I2C function for these pins.
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

// Enable and initialize the I2C0 master module. Use the system clock for
// the I2C0 module. The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps.
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);

//clear I2C FIFOs
HWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000;
}

//read specified register on slave device
uint32_t I2CReceive(uint32_t slave_addr, uint8_t reg)
{
//specify that we are writing (a register address) to the
//slave device
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);

//specify register to be read
I2CMasterDataPut(I2C0_BASE, reg);

//send control byte and register address byte to slave device
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);

//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));

SysCtlDelay(200000);

//specify that we are going to read from slave device
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, true);

//send control byte and read from the register we
//specified
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));
SysCtlDelay(200000);

//return data pulled from the specified register
return I2CMasterDataGet(I2C0_BASE);
//SysCtlDelay(5000);
}

uint8_t ReadAccel(uint8_t reg)
{
uint8_t accelData = I2CReceive(ACCEL_SLAVE_ADDR, reg);

return accelData;
}

void main(void)
{
// Set the clocking to run directly from the external crystal/oscillator.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL | SYSCTL_OSC_INT | SYSCTL_XTAL_16MHZ);

//initialize I2C module 0
InitI2C0();

while(1)
{
Ax = ReadAccel(0x32);
Ax1=ReadAccel(0x33);
datax = (Ax1 << 8) |Ax;

Ay=ReadAccel(0x34);
Ay1=ReadAccel(0x35);
datay = (Ay1 << 8) |Ay;

}

}