0% found this document useful (0 votes)
45 views5 pages

ADXL345 Sensor I2C Interface

The document includes header files and function definitions for initializing an I2C module and UART module on a TM4C123 microcontroller in order to communicate with an ADXL345 accelerometer sensor and output acceleration data to the UART.

Uploaded by

avi kishu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

ADXL345 Sensor I2C Interface

The document includes header files and function definitions for initializing an I2C module and UART module on a TM4C123 microcontroller in order to communicate with an ADXL345 accelerometer sensor and output acceleration data to the UART.

Uploaded by

avi kishu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdarg.

h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <string.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/uart.h"
#include "driverlib/pin_map.h"
#include "utils/uartstdio.h"
#include "inc/TM4C123GH6PM.h"

#define ADXL345_SLAVE_ADRESS 0x53


#define DATAX0 0x32
#define DATAX1 0x33
#define DATAY0 0x34
#define DATAY1 0x35
#define DATAZ0 0x36
#define DATAZ1 0x37

char Data_buffer[15]={0};
char Data_buffer1[15]={0};
char Data_buffer2[15]={0};
uint8_t Ax0, Ax1, Ay0 ,Ay1, Az0, Az1;
float X_out, Y_out, Z_out;
uint8_t buffer[8]={0};

// Function prototypes initialize, tranmit and rea functions


void I2C3_Init ( void );
char I2C3_read_Multiple(int slave_address, char slave_memory_address, int
bytes_count, uint8_t* data);
void InitUART0(void);
void InitConsole(void);

// reverses a string 'str' of length 'len'


void reverse(char *str, int len)
{
int i=0, j=len-1, temp;
while (i<j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++; j--;
}
}

// Converts a given integer x to string str[]. d is the number


// of digits required in output. If d is more than the number
// of digits in x, then 0s are added at the beginning.
int intToStr(int x, char str[], int d)
{
int i = 0;
while (x)
{
str[i++] = (x%10) + '0';
x = x/10;
}

// If number of digits required is more, then


// add 0s at the beginning
while (i < d)
str[i++] = '0';

reverse(str, i);
str[i] = ' ';
return i;
}

// Converts a floating point number to string.


void ftoa(float n, char *res, int afterpoint)
{
// Extract integer part
int ipart = (int)n;

// Extract floating part


float fpart = n - (float)ipart;

// convert integer part to string


int i = intToStr(ipart, res, 0);

// check for display option after point


if (afterpoint != 0)
{
res[i] = '.'; // add dot

// Get the value of fraction part upto given no.


// of points after dot. The third parameter is needed
// to handle cases like 233.007
fpart = fpart * pow(10, afterpoint);

intToStr((int)fpart, res + i + 1, afterpoint);


}
}

int 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 UART module 0


// InitUART0();
InitConsole();

//initialize I2C module 3


I2C3_Init();
UARTprintf("ADXL345 SENSOR\n\r");

memset(Data_buffer,' ',sizeof(Data_buffer));
memset(Data_buffer1,' ',sizeof(Data_buffer1));
memset(Data_buffer2,' ',sizeof(Data_buffer2));

while(1)
{
/* I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAX0,1,&Ax0);
UARTprintf("AX0:%x\n\r",Ax0);
I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAX1,1,&Ax1);
UARTprintf("AX1:%x\n\r",Ax1);
I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAY0,1,&Ay0);
UARTprintf("AY0:%x\n\r",Ay0);
I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAY1,1,&Ay1);
UARTprintf("AY1:%x\n\r",Ay1);
I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAZ0,1,&Az0);
UARTprintf("AZ0:%x\n\r",Az0);
I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAZ1,1,&Az1);
UARTprintf("AZ1:%x\n\r",Az1);

X_out = ( Ax0 | Ax1 << 8);


X_out = X_out/256;
Y_out = ( Ay0 | Ay1 << 8);
Y_out = Y_out/256;
Z_out = ( Az0 | Az1 << 8);
Z_out = Z_out/256; */

I2C3_read_Multiple(ADXL345_SLAVE_ADRESS,DATAX0,6,&buffer[0]);
UARTprintf("%x %x %x %x %x %x\n\
r",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5]);

X_out = (((int)buffer[1]) << 8) | buffer[0];


X_out = X_out/256;
Y_out = (((int)buffer[3]) << 8) | buffer[2];
Y_out = Y_out/256;
Z_out = (((int)buffer[5]) << 8) | buffer[4];
Z_out = Z_out/256;

// UARTprintf("X-Axis : %f Y-Axis: %f Z-Axis: %f\n\r",X_out,Y_out,Z_out);


// sprintf(Data_buffer, "X-Axis : %f" ,X_out );
ftoa(X_out, Data_buffer, 2);
UARTprintf("X-AXIS:%s",Data_buffer);
ftoa(Y_out, Data_buffer1, 2);
UARTprintf("Y-AXIS:%s",Data_buffer1);
ftoa(Z_out, Data_buffer2, 2);
UARTprintf("Z-AXIS:%s\n\r",Data_buffer1);

SysCtlDelay(2000000);

memset(Data_buffer,0,sizeof(Data_buffer));
memset(Data_buffer1,0,sizeof(Data_buffer1));
memset(Data_buffer2,0,sizeof(Data_buffer2));

X_out = 0;
Y_out = 0;
Z_out = 0;

}
}

/*void InitUART0(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,


(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));

}*/

void InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

UARTStdioConfig(0, 115200, 16000000);


}

// I2C intialization and GPIO alternate function configuration


void I2C3_Init ( void )
{
SYSCTL_RCGCGPIO_R = (1<<3); // Enable the clock for port D
SYSCTL_RCGCI2C_R = (1<<3); // Enable the clock for I2C 3
GPIO_PORTD_DEN_R = (1<<0)|(1<<1); // Assert DEN for port D
// Configure Port D pins 0 and 1 as I2C 3
GPIO_PORTD_AFSEL_R = (1<<0)|(1<<1);
GPIO_PORTD_PCTL_R = 0x00000033 ;
GPIO_PORTD_ODR_R = (1<<1) ; // SDA (PD1 ) pin as open darin
I2C3_MCR_R = (1<<4) ; // Enable I2C 3 master function
/* Configure I2C 3 clock frequency
(1 + TIME_PERIOD ) = SYS_CLK /(2*
( SCL_LP + SCL_HP ) * I2C_CLK_Freq )
TIME_PERIOD = 16 ,000 ,000/(2(6+4) *100000) - 1 = 7 */
I2C3_MTPR_R = 0X07 ;

HWREG(I2C3_BASE + I2C_O_FIFOCTL) = 80008000;


}
/* wait untill I2C Master module is busy */
/* and if not busy and no error return 0 */
static int I2C_wait_till_done(void)
{
while(I2C3_MCS_R & 1); /* wait until I2C master is not busy */
return I2C3_MCS_R & 0xE; /* return I2C error code, 0 if no error*/
}

char I2C3_read_Multiple(int slave_address, char slave_memory_address, int


bytes_count, uint8_t* data)
{
char error;

if (bytes_count <= 0)
return 1; /* no read was performed */

/* send slave address and starting address */


I2C3_MSA_R = slave_address << 1;
I2C3_MDR_R = slave_memory_address;
I2C3_MCS_R = 3;
error = I2C_wait_till_done();
if (error)
return error;

I2C3_MSA_R = (slave_address << 1) + 1; /* restart: -R-(saddr+r)-ACK */

if (bytes_count == 1) /* if last byte, don't ack */


I2C3_MCS_R = 7; /* -data-NACK-P */
else /* else ack */
I2C3_MCS_R = 0xB; /* -data-ACK- */
error = I2C_wait_till_done();
if (error) return error;

*data++ = I2C3_MDR_R; /* store the data received */

if (--bytes_count == 0)
{
while(I2C3_MCS_R & 0x40);
return 0;
}

while (bytes_count > 1)


{
I2C3_MCS_R = 9;
error = I2C_wait_till_done();
if (error) return error;
bytes_count--;
*data++ = I2C3_MDR_R;
}

I2C3_MCS_R = 5;
error = I2C_wait_till_done();
*data = I2C3_MDR_R;
while(I2C3_MCS_R & 0x40);

return 0; /* no error */
}

You might also like