FRDM-KL46Z I2C Accelerometer

cancel
Showing results for 
Search instead for 
Did you mean: 

FRDM-KL46Z I2C Accelerometer

1,170 Views
januszkowalski
Contributor I

Hi Everyone,

 

I am trying to make an accelerometer application using built-in MMA8451Q module on FRDM-KL46Z board. I am using Keil uVision editor. It's something new to me so please be patient

My problem starts with I2C communication. I am trying to read a value from WHO_AM_I register but I always receive the WHO_AM_I address - not the value in it (0x1A).
I tried to search for similar projects or codes, but most of them are made with additional libraries (mbed for example) in other editors (CodeWarrior). What am i doing wrong in I2C_ReadRegister_uc function? How it's supposed to be done?

 

I am including my C files so you can see the code.
Please help And thanks in advance

Original Attachment has been moved to: I2C.h.zip

Original Attachment has been moved to: I2C.c.zip

Original Attachment has been moved to: main.c.zip

0 Kudos
3 Replies

364 Views
mjbcswitzerland
Specialist V

Hi Janusz

I took a look at your initialisation code and see that you are enabling I2C1 on PTE0 and PTE1.

As far as I know and have experienced (unless there is a new board that has a different design) the MMA8451Q is connected to I2C0 (PTE24 and PTE25). Please check this before continuing.

Note that you can get a complete project for your board which includes robust I2C for the accelerometer, as well as almost everything else that you may need (sLCD, USB, etc.) at http://www.utasker.com/kinetis/FRDM-KL46Z.html

It also allows you to simuate the (FRDM-KL46Z) KL46 and the I2C operation with accelerometer to learn full details of the KL46.

Regards

Mark

0 Kudos

364 Views
januszkowalski
Contributor I

Hello,

thank you for your advice. Much appreciated. My I2C is working now and I read the correct value from WHO_AM_I register :smileyhappy:
But I'm facing with another problem now. When I read from axis registers of MMA8451Q module I always get the same value from them.

I do it like that:

  //======================================

  //================X AXIS================

  //======================================

  xMSB = I2C_ReadRegister(0x01); //MSB

  xLSB = I2C_ReadRegister(0x02); //LSB

  xMSB = xMSB<<6;

  x = xMSB | xLSB;

  //======================================

  //================Y AXIS================

  //======================================

  yMSB = I2C_ReadRegister(0x03); //MSB

  yLSB = I2C_ReadRegister(0x04); //LSB

  yMSB = yMSB<<6;

  y = yMSB | yLSB;

  //======================================

  //================Z AXIS================

  //======================================

  zMSB = I2C_ReadRegister(0x05); //MSB

  zLSB = I2C_ReadRegister(0x06); //LSB

  zMSB = zMSB<<6;

  z = zMSB | zLSB;

These registers always return the same values. Should I calibrate or initialise the MMA8451Q module before reading these registers? How to do it?

Regards,

Janusz

0 Kudos

364 Views
mjbcswitzerland
Specialist V

Hi

You need to configure and start the accelerometer.

Please refer to its data sheet for all details.

Regards

Mark

0 Kudos