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.
Tool/software: Code Composer Studio
Hello,
i am interfacing the gy80 accelerometer (ADXL345) with cc2640r2F . my problem is that i m not getting right output values
plz check my program and resolve my problem
____________________________________________________________________________________________________________________________________________________________________
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/display/Display.h>
#include <xdc/runtime/system.h>
/* Example/Board Header files */
#include "Board.h"
#define TASKSTACKSIZE 640
#define DATAX0 0X32 /*X-Axis Data 0 */
#define DATAX1 0X33 /*X-Axis Data 1 */
#define DATAY0 0X34 /*Y-Axis Data 0 */
#define DATAY1 0X35 /*Y-Axis Data 1 */
#define DATAZ0 0X36 /*Z-Axis Data 0 */
#define DATAZ1 0X37 /*Z-Axis Data 1 */
#define DEVID 0x00 /* Device address */
static Display_Handle display;
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
unsigned int i;
uint8_t txBuffer[8];
uint8_t rxBuffer[8];
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
/* Call driver init functions */
Display_init();
GPIO_init();
I2C_init();
/* Open the HOST display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
while (1);
}
/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
Display_printf(display, 0, 0, "Starting the i2ctmp007 example\n");
/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C_TMP, &i2cParams);
if (i2c == NULL) {
Display_printf(display, 0, 0, "Error Initializing I2C\n");
while (1);
}
else {
Display_printf(display, 0, 0, "I2C Initialized!\n");
}
txBuffer[0] = DEVID;
txBuffer[1] = DEVID;
txBuffer[2] = DATAX0;
txBuffer[3] = DATAX1;
txBuffer[4] = DATAY0;
txBuffer[5] = DATAY1;
txBuffer[6] = DATAZ0;
txBuffer[7] = DATAZ1;
i2cTransaction.slaveAddress = Board_TMP_ADDR; /* 0x53 i2c address*/
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 2;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.readCount = 8;
for (i = 0; i < 20; i++)
{
Display_printf(display, 0, 0, "Sample %d \n", i); /* number of samples*/
if (I2C_transfer(i2c, &i2cTransaction))
{
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[0], rxBuffer[0]);
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[1], rxBuffer[1]);
Display_printf(display, 0, 0,"value %x: %x \n", txBuffer[2], rxBuffer[2]);
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[3], rxBuffer[3]);
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[4], rxBuffer[4]);
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[5], rxBuffer[5]);
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[6], rxBuffer[6]);
Display_printf(display, 0, 0, "value %x: %x \n", txBuffer[7], rxBuffer[7]);
}
else
{
Display_printf(display, 0, 0, "I2C Bus fault\n");
}
/* Sleep for 1 second */
sleep(1);
}
/* Deinitialized I2C */
I2C_close(i2c);
Display_printf(display, 0, 0, "I2C closed!\n");
return (NULL);
}
____________________________________________________________________________________________________________________________________________________
output
Hello kalpesh,
Your code does not match the print out you provided, can you please update the post with the most up to date code?
Also, I notice you have the write count set to 2 and you are providing a buffer with 8 entries. From what I see you are tryinig to do is reach each 8 bit register and save it on the address, for that I would recommend the following:
#include <stdint.h> #include <stddef.h> #include <unistd.h> /* Driver Header files */ #include <ti/drivers/GPIO.h> #include <ti/drivers/I2C.h> #include <ti/display/Display.h> #include <xdc/runtime/system.h> /* Example/Board Header files */ #include "Board.h" #define TASKSTACKSIZE 640 #define DATAX0 0X32 /*X-Axis Data 0 */ #define DATAX1 0X33 /*X-Axis Data 1 */ #define DATAY0 0X34 /*Y-Axis Data 0 */ #define DATAY1 0X35 /*Y-Axis Data 1 */ #define DATAZ0 0X36 /*Z-Axis Data 0 */ #define DATAZ1 0X37 /*Z-Axis Data 1 */ #define DEVID 0x00 /* Device address */ static Display_Handle display; /* * ======== mainThread ======== */ void *mainThread(void *arg0) { unsigned int i; uint8_t txBuffer[8]; uint8_t rxBuffer[8]; I2C_Handle i2c; I2C_Params i2cParams; I2C_Transaction i2cTransaction; /* Call driver init functions */ Display_init(); GPIO_init(); I2C_init(); /* Open the HOST display for output */ display = Display_open(Display_Type_UART, NULL); if (display == NULL) { while (1); } /* Turn on user LED */ GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON); Display_printf(display, 0, 0, "Starting the i2ctmp007 example\n"); /* Create I2C for usage */ I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_400kHz; i2c = I2C_open(Board_I2C_TMP, &i2cParams); if (i2c == NULL) { Display_printf(display, 0, 0, "Error Initializing I2C\n"); while (1); } else { Display_printf(display, 0, 0, "I2C Initialized!\n"); } txBuffer[0] = DEVID; txBuffer[1] = DEVID; txBuffer[2] = DATAX0; txBuffer[3] = DATAX1; txBuffer[4] = DATAY0; txBuffer[5] = DATAY1; txBuffer[6] = DATAZ0; txBuffer[7] = DATAZ1; for (i = 0; i < 8; i++) { i2cTransaction.slaveAddress = Board_TMP_ADDR; /* 0x53 i2c address*/ i2cTransaction.writeBuf = txBuffer[i]; i2cTransaction.writeCount = 1; i2cTransaction.readBuf = rxBuffer[i]; i2cTransaction.readCount = 1; if (I2C_transfer(i2c, &i2cTransaction)) { Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[i], rxBuffer[i]); } else { Display_printf(display, 0, 0, "I2C Bus fault\n"); } /* Sleep for 1 second */ sleep(1); } /* Deinitialized I2C */ I2C_close(i2c); Display_printf(display, 0, 0, "I2C closed!\n"); return (NULL); }
Best Regards,
AB
yes there is a problem in printout. the correct image is
if i using the 2 and 8 in my code ,i get the correct reset values . e5 shows the reset address of DEVID (device address) and 0 denote reset values of other registers .
if i used your code i get only device address reset values (e5) and not getting right registers reset values .
your output is
my problem is that, i want the final output (x,y,z) and i forget something in my code.
can u plz give the final output code .
Thanks
Hello,
Can you please show me your modified code, the printout of the modified code does not match what I recommended.
Regards,
AB
Hello,
According to the datasheet of the accelerometer you are using, you need to:
The code would look like this:
#include <stdint.h> #include <stddef.h> #include <unistd.h> /* Driver Header files */ #include <ti/drivers/GPIO.h> #include <ti/drivers/I2C.h> #include <ti/display/Display.h> #include <xdc/runtime/system.h> /* Example/Board Header files */ #include "Board.h" #define TASKSTACKSIZE 640 #define DATAX0 0X32 /*X-Axis Data 0 */ #define DATAX1 0X33 /*X-Axis Data 1 */ #define DATAY0 0X34 /*Y-Axis Data 0 */ #define DATAY1 0X35 /*Y-Axis Data 1 */ #define DATAZ0 0X36 /*Z-Axis Data 0 */ #define DATAZ1 0X37 /*Z-Axis Data 1 */ #define DEVID 0x00 /* Device address */ static Display_Handle display; /* * ======== mainThread ======== */ void *mainThread(void *arg0) { unsigned int i; uint8_t txBuffer[8]; uint8_t rxBuffer[8]; I2C_Handle i2c; I2C_Params i2cParams; I2C_Transaction i2cTransaction; /* Call driver init functions */ Display_init(); GPIO_init(); I2C_init(); /* Open the HOST display for output */ display = Display_open(Display_Type_UART, NULL); if (display == NULL) { while (1); } /* Turn on user LED */ GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON); Display_printf(display, 0, 0, "Starting the i2ctmp007 example\n"); /* Create I2C for usage */ I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_400kHz; i2c = I2C_open(Board_I2C_TMP, &i2cParams); if (i2c == NULL) { Display_printf(display, 0, 0, "Error Initializing I2C\n"); while (1); } else { Display_printf(display, 0, 0, "I2C Initialized!\n"); } txBuffer[0] = DEVID; txBuffer[1] = DEVID; txBuffer[2] = DATAX0; txBuffer[3] = DATAX1; txBuffer[4] = DATAY0; txBuffer[5] = DATAY1; txBuffer[6] = DATAZ0; txBuffer[7] = DATAZ1; for (i = 0; i < 8; i++) { i2cTransaction.slaveAddress = 0xA6; /* (0x53 + 0) i2c address*/ i2cTransaction.writeBuf = txBuffer[i]; i2cTransaction.writeCount = 1; i2cTransaction.readBuf = NULL; i2cTransaction.readCount = 0; if (I2C_transfer(i2c, &i2cTransaction)) { Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[i], rxBuffer[i]); } else { Display_printf(display, 0, 0, "I2C Bus fault\n"); } i2cTransaction.slaveAddress = 0xA7; /* (0x53 + 1) i2c address*/ i2cTransaction.writeBuf = NULL; i2cTransaction.writeCount = 0; i2cTransaction.readBuf = rxBuffer[i]; i2cTransaction.readCount = 1; if (I2C_transfer(i2c, &i2cTransaction)) { Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[i], rxBuffer[i]); } else { Display_printf(display, 0, 0, "I2C Bus fault\n"); } /* Sleep for 1 second */ sleep(1); } /* Deinitialized I2C */ I2C_close(i2c); Display_printf(display, 0, 0, "I2C closed!\n"); return (NULL); }
Regards,
AB
BY Using above code i am not getting any output. and where i used the i2c address(0x53) to interface the board.
if i used the " slave address + write " which can be equal to (0x53+0xA6=F9) and slave adress + read equal to (0x53+0xA7=FA) . again result is same i.e " i2c bus fault".
in Previous reply i understand how to read and the write the data with addition of slave address but practically it is not working.
The output of the previous reply of that code is
Hello,
You do not need to add the write + address to the address. according to the datasheet, the address is a 7bit value and you just add that last bit depending whether you want to read or write.
For WRITE:
((0x53 << 1) + 0x00) = 0xA6
For READ:
((0x53 << 1) + 0x01) = 0xA7
That is the reason behind the slave address being 0xA7 or 0xA6. If you have a digital analyzer that you could use to sniff the lines and see the output of the device, it would be helpful.
Regards,
AB
my final coding is give below. i m unable to read the data from the register, plz check my coding.
my steps to make the program is :
1 . connect the I2C using slave address(i2c address 0x53)
2. define the following registers
3. enable the power controlled register (0x2D) by write the value 0x08(0000 1000 i.e for measurement enable)
4. read the data from axes register
5. calculation
6. output
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/display/Display.h>
#include <xdc/runtime/system.h>
/* Example/Board Header files */
#include "Board.h"
#define TASKSTACKSIZE 640
#define DATAX0 0x32 /*X-Axis Data 0 */
#define DATAX1 0x33 /*X-Axis Data 1 */
#define DATAY0 0x34 /*Y-Axis Data 0 */
#define DATAY1 0x35 /*Y-Axis Data 1 */
#define DATAZ0 0x36 /*Z-Axis Data 0 */
#define DATAZ1 0x37 /*Z-Axis Data 1 */
#define DEVID 0x00 /* Device address */
#define PCR 0x2D /* power controlled register*/
static Display_Handle display;
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
int x,y,z;
float x_out,y_out,z_out;
uint8_t txBuffer[9];
uint8_t rxBuffer[9];
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
/* Call driver init functions */
Display_init();
GPIO_init();
I2C_init();
/* Open the HOST display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
while (1);
}
/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
Display_printf(display, 0, 0, "Starting the i2ctmp007 example\n");
/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C_TMP, &i2cParams);
if (i2c == NULL) {
Display_printf(display, 0, 0, "Error Initializing I2C\n");
while (1);
}
else {
Display_printf(display, 0, 0, "I2C Initialized!\n");
}
Display_printf(display, 0, 0, "%x\n",PCR);
i2cTransaction.slaveAddress = 0x53; /* (0x53 + 0) i2c address*/
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 4;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.readCount = 10;
if (I2C_transfer(i2c, &i2cTransaction))
{
//while(2)
{
txBuffer[0] = DEVID;
txBuffer[1] = PCR;
rxBuffer[1]=0x08;
txBuffer[2] = DATAX0;
txBuffer[3] = DATAX1;
txBuffer[4] = DATAY0;
txBuffer[5] = DATAY1;
txBuffer[6] = DATAZ0;
txBuffer[7] = DATAZ1;
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[0],rxBuffer[0]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[1],rxBuffer[1]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[2],rxBuffer[2]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[3],rxBuffer[3]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[4],rxBuffer[4]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[5],rxBuffer[5]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[6],rxBuffer[6]);
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[7],rxBuffer[7]);
x=rxBuffer[2]+rxBuffer[3];
x_out=x/256;
y=rxBuffer[4]+rxBuffer[5];
y_out=y/256;
z=rxBuffer[6]+rxBuffer[7];
z_out=z/256;
Display_printf(display, 0, 0, "x [%d]: %f \n", x, x_out);
Display_printf(display, 0, 0, "y [%d]: %f \n", y, y_out);
Display_printf(display, 0, 0, "z [%d]: %f \n", z, z_out);
}
}
else
{
Display_printf(display, 0, 0, "I2C Bus fault\n");
}
/* Sleep for 1 second */
sleep(1);
/* Deinitialized I2C */
I2C_close(i2c);
Display_printf(display, 0, 0, "I2C closed!\n");
return (NULL);
}
please check my program and give the solution
Hello,
Try and read through the following code. I go through each line and explain what I am doing.
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/display/Display.h>
#include <xdc/runtime/system.h>
/* Example/Board Header files */
#include "Board.h"
#define TASKSTACKSIZE 640
#define DATAX0 0x32 /*X-Axis Data 0 */
#define DATAX1 0x33 /*X-Axis Data 1 */
#define DATAY0 0x34 /*Y-Axis Data 0 */
#define DATAY1 0x35 /*Y-Axis Data 1 */
#define DATAZ0 0x36 /*Z-Axis Data 0 */
#define DATAZ1 0x37 /*Z-Axis Data 1 */
#define DEVID 0x00 /* Device address */
#define PCR 0x2D /* power controlled register*/
static Display_Handle display;
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
int x,y,z,i;
float x_out,y_out,z_out;
uint8_t txBuffer[9];
uint8_t rxBuffer[9];
I2C_Handle i2c;
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
/* Call driver init functions */
Display_init();
GPIO_init();
I2C_init();
/* Open the HOST display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
while (1);
}
/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
Display_printf(display, 0, 0, "Starting the i2ctmp007 example\n");
/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C_TMP, &i2cParams);
if (i2c == NULL) {
Display_printf(display, 0, 0, "Error Initializing I2C\n");
while (1);
}
else {
Display_printf(display, 0, 0, "I2C Initialized!\n");
}
Display_printf(display, 0, 0, "%x\n",PCR);
//populate the txBuffer before doing the I2C_transaction
txBuffer[0] = PCR;
//set bit 4 and 3 [0000 11000] for measurement mode enable and auto_sleep enable
txBuffer[1] = 0x0D;
i2cTransaction.slaveAddress = (0x53);
//two byes are going to be sent when doing a single byte write (Register + Value)
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 2;
//No reply from the slave when doing a write, no need for a buffer
i2cTransaction.readBuf = NULL;
i2cTransaction.readCount = 0;
if (I2C_transfer(i2c, &i2cTransaction))
{
//Lets wait a second to let the device reset and start calculations after writing to the power control register
sleep(1);
//populate the txBuffer before doing the I2C_transaction
txBuffer[0] = DEVID;
txBuffer[1] = DEVID;
txBuffer[2] = DATAX0;
txBuffer[3] = DATAX1;
txBuffer[4] = DATAY0;
txBuffer[5] = DATAY1;
txBuffer[6] = DATAZ0;
txBuffer[7] = DATAZ1;
//To keep it simple let's do single byte read operations
for(i=0;i<8;i++){
i2cTransaction.slaveAddress = 0x53;
//1 byte is going to be sent when doing a single byte read (Register)
i2cTransaction.writeBuf = txBuffer[i];
i2cTransaction.writeCount = 1;
//1 byte is going to be sent when doing a single byte read (Register)
i2cTransaction.readBuf = txBuffer[i];
i2cTransaction.readCount = 1;
if (I2C_transfer(i2c, &i2cTransaction))
{
Display_printf(display, 0, 0, "[%x]: %x \n", txBuffer[i],rxBuffer[i]);
}
else
{
Display_printf(display, 0, 0, "I2C Bus fault\n");
}
}
x=rxBuffer[2]+rxBuffer[3];
x_out=x/256;
y=rxBuffer[4]+rxBuffer[5];
y_out=y/256;
z=rxBuffer[6]+rxBuffer[7];
z_out=z/256;
Display_printf(display, 0, 0, "x [%d]: %f \n", x, x_out);
Display_printf(display, 0, 0, "y [%d]: %f \n", y, y_out);
Display_printf(display, 0, 0, "z [%d]: %f \n", z, z_out);
}
else
{
Display_printf(display, 0, 0, "I2C Bus fault\n");
}
/* Sleep for 1 second */
sleep(1);
/* Deinitialized I2C */
I2C_close(i2c);
Display_printf(display, 0, 0, "I2C closed!\n");
return (NULL);
}
Regards,
AB
hello,
The above Coding is working by correcting some small things . This is my First I2C Task.
Thanks for help .
Regards
Kalpesh