cancel
Showing results for 
Search instead for 
Did you mean: 

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

mohamad_armoon
Associate III
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
23 REPLIES 23
chen
Associate II
Posted on April 02, 2014 at 12:42

Hi

''is there anybody to explain me the procedure of this matter''

There are hundreds of threads all asking - how do I do I2C

Many will have code examples in reply to the original post.

Please search the forum for the information you require.

In particular, look for examples from a poster called clive1

He provides the best example code.

If you have problems - then ask again.

mohamad_armoon
Associate III
Posted on April 03, 2014 at 22:38

hi ,i searched a lot but i couldn't find any thing useful ,(maybe the problem is me), after some effort i could get the data of adxl345, but now i have a question, when i want to get a data (for example X axis data(DATAX0))  i have to write it's register address in this sensor, this sensor has 6 register that saving data in them, DATAX0,DATAX1,DATAY0,DATAY1,DATAZ0,DATAZ1,

if i want to gather all of these 6, i have to always send these 6 register address to sensor in my while loop ? i mean there isn't any configuration in it that i can make it to give me there 6 value just with 1 initialization ??

i don't want to hold micro always busy just for sending this value to my sensor. thank
Posted on April 03, 2014 at 23:22

I2C slaves typically have internal register address auto-incrementing. ie you select the base register, and the read back 6 consecutive register content.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mohamad_armoon
Associate III
Posted on April 04, 2014 at 16:38

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6o7&d=%2Fa%2F0X0000000bvt%2FFlMGCudX63TveaMz8u5HE1uEcP8.ZwCL3_AB8.NH7Fo&asPdf=false
Posted on April 04, 2014 at 17:50

i think i have to always send the registers addresses

You're clearly reading some

http://www.analog.com/static/imported-files/data_sheets/ADXLpdf

. Pg 18 wrt I2C ''Single - or multiple - byte reads/writes are supported, as shown in Figure ''


 I2C_start(I2C1, 0xA6 , I2C_Direction_Transmitter);

 I2C_start(I2C1,0xA7 , I2C_Direction_Receiver);

The direction setting should already infer the state of Bit 0 in the address (DEVICE << 1) I really can't help you with I2C stuff, I don't like it, and really dislike the ST implementation. I'm not specifically familiar with the ADXL
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
stm322399
Senior
Posted on April 04, 2014 at 18:09

If you want to receive multiples bytes at once, it is *very* important to set the Acknowledge flag *before* waiting the

I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED

event: change your I2C_Start function that way.
chen
Associate II
Posted on April 04, 2014 at 18:24

Hi

''but it has problem,when i unplug the discovery board usb from pc and plug it again and program the stm32f407vg , it programs well, but when i want to change a line of my code and reprogram it, i have to reset board from reset button to pass this line of my code :

while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));

i don't find the problem ,but i'm trying to,

i have to add, i send it's data to pc through usart. but the problem which i mentioned  make me doubt about my code and the accuracy of these data :)''

It sounds like you are trying to do this by looking at what the processor can output.

You need to use a debugger.

You mentioned that you are using a Discovery board. This board has an on board ST Link debugger. There are a number of free Intergeted Developmen Enviroment (IDEs) that will work with this. These will allow you to debug to a line of code.

Try :

IAR Workbenck (free edition limited to 32K)

Keil (free edition also limited to 32K)

em:Blocks

CooCox

to name but a few. This should get over your problems of having to reboot to program etc

mohamad_armoon
Associate III
Posted on April 05, 2014 at 08:48

hi 

gonzalez.laurent

in my code i put this 

I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED 

after all of my flag flags.

would you explain more about ''Acknowledge flag'' which i have to put befor the event you mentioned??

thanks in advance 
stm322399
Senior
Posted on April 05, 2014 at 09:30

This is required by the I2C protocol: when a byte is send from one side (master or slave), the other side must acknowledge the reception.

When you read bytes from the slave, the master side must acknowledge, this is exactly what the ACK bit in control register does. As soon as a single byte is not acknowledge, the slave will consider that the transfer is finished.

As soon as the master is put in receive mode, it starts to clock data in from the slave, so it is *very* important to set ACK bit before the clock starts.