Skip to main content
Visitor II
March 30, 2014
Question

write into a register in ADXL345 through i2c whit stm32f4 's

  • March 30, 2014
  • 23 replies
  • 4328 views
Posted on March 30, 2014 at 09:25

hi guys. for gathering data from adxl345 accelerometer i need to write some data in specified register in this sensor. but in stm32f4xx_i2c.c there isn't any function like :

I2C_SendData(I2Cx, data);

to write in a register which i know it's address.

is there anybody to explain me the procedure of this matter, means connect adxl345(or any sensor which is connected through i2c  ) through i2c to stm32f4

#stm32f4 #i2c #i2c #discovery #accelerometer #discovery #stm32f4
    This topic has been closed for replies.

    23 replies

    Visitor II
    May 9, 2014
    Posted on May 09, 2014 at 14:34

    Hi Armoun.mohamad

    I finally succeeded in reading out all of the axes of the sensor, found a person online who wrote his own libraries for the STM32F429 based on the standard ST libraries (

    http://majerle.eu/

    ). So now I receive the data in hex in my terminal. (it is stable if I don't move the sensor) I am now trying to display it in the terminal in ASCII but I don't know if it is even possible to convert it into ASCII symbols before sending them through serial.. At the moment I have written the following function to merge the data to 1 data for each axis:

    void
    comb_DATA(uint16_t data_axis [], uint8_t data[]){
    data_axis[0] = (data[1] << 8) | data[0];
    data_axis[1] = (data[3] << 8) | data[2];
    data_axis[2] = (data[5] << 8) | data[4];
    }

    but when sending this data through serial just gives me the MSB of the axis and not the complete 2 byte of data. Is it even possible what I try to do? Or do I need to have an extra program on my pc to read out the correct data (in ASCII)? Greetings Ben
    Visitor II
    May 9, 2014
    Posted on May 09, 2014 at 15:31

    hi ben

    well done. the pint is usart send 1 byte each time so if you want to send it by usart you have to use this conversion(each axis to 1 number) in pc.i mean you have to create a program with c or c++ or java or c# or .... in pc to read from your serial port .

     if you cant program in pc you can convert the each axis number to string (a array of char) and then send this string trough usart.

    note that , as i mentioned usart send 8bit or 1byte each time and  you convert integer to string you must send each char per time. because char is 1byte.

    here is some example about converting integer to string in c and c++:

    http://www.cplusplus.com/articles/D9j2Nwbp/%20

    http://www.cplusplus.com/reference/string/to_string/

    http://www.cplusplus.com/forum/beginner/7777/

    http://stackoverflow.com/questions/9655202/how-to-convert-integer-to-string-in-c

    http://stackoverflow.com/questions/5242524/converting-int-to-string-in-c

    Visitor II
    November 1, 2014
    Posted on November 01, 2014 at 18:59

    Hi Mohamad,

    I use your code and I read all values of the sensor. Thanks for that. Now I try to display that on HD447 I've got a library for that and I can display other 8-bit and 16-bit variables (convert that to char with sprintf function). However I've got a troubles with values which I read of the sensor. I'm not sure if I do it correctly, my code:

    ReadFromAccel(DATAX0);
    I2C_start(I2C1,0xA7 , I2C_Direction_Receiver);
    buffer[0] = I2C_read_nack(I2C1);
    Delay(0xFFFF);
    ReadFromAccel(DATAX1);
    I2C_start(I2C1,0xA7 , I2C_Direction_Receiver);
    buffer[1] = I2C_read_nack(I2C1);
    Delay(0xFFFFa);
    data_axis = (int16_t)((buffer[1] << 8) | buffer[0]);
    sprintf (buffer1, ''X: %d'', data_axis);
    TM_HD44780_Puts(0, 1, buffer1);

    Is it a good way to read and display the axes of the sensor?