KL25Z Accelerometor Read

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

KL25Z Accelerometor Read

1,022 Views
anthonyverfaill
Contributor II

I am using processor expert (10.5) code warrior, so that I can read the on board KL25Z accelerometer. No matter how I manipulate the board I get the same values from my break point at the end of the code. The idea is, run the program and read x,y,z using the variables view of code warrior.  So in theory if I move the board say 90 degrees the values of the variables should change however, they do not. My code is below any help is appreciated.

/* ###################################################################
**     Filename    : ProcessorExpert.c
**     Project     : ProcessorExpert
**     Processor   : MKL25Z128VLK4
**     Version     : Driver 01.01
**     Compiler    : GNU C Compiler
**     Date/Time   : 2018-01-14, 18:13, # CodeGen: 0
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file ProcessorExpert.c
** @version 01.01
** @brief
**         Main module.
**         This module contains user's application code.
*/        
/*!
**  @addtogroup ProcessorExpert_module ProcessorExpert module documentation
**  @{
*/        
/* MODULE ProcessorExpert */


/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "I2C.h"
#include "IntI2cLdd1.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */

 int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */
 

 char  buf[32];
  word  count;
    int16  xAxis;
  int16  yAxis;
  int16  zAxis;
  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
 
  // Initialize the I2C module
    I2C_Init();

   // Address the on-board accelerometer and put it into an active state
   I2C_SelectSlave(0x1d);
   buf[0] = 0x2a;     // address for CTRL-REG1
   buf[1] = 0x01;     // active
   I2C_SendBlock(buf, 2, &count);

   for (;;)
   {
    do
    {
     I2C_SelectSlave(0x1d);
     buf[0] = 0x00;     // address for STATUS register
     I2C_SendBlock(buf, 1, &count);
  
     I2C_SelectSlave(0x1d);
     I2C_RecvBlock(buf, 7, &count);
    } while (! (buf[0] & 0x08));   // Wait for status register to indicate all 3 axes are ready
   
    // read the x axis (14-bit two's complement)
    xAxis = (buf[1] << 6) | (buf[2] >> 2);
   
    // convert to signed 16-bit integer
    if (xAxis & 0x2000)
   {
       xAxis |= 0xc000;
       // this would also work:
       // xAxis -= 0x4000;
      }

    // read the y axis (14-bit two's complement)
    yAxis = (buf[3] << 6) | (buf[4] >> 2);

    // convert to signed 16-bit integer
    if (yAxis & 0x2000)
     yAxis |= 0xc000;
   
    // read the z axis (14-bit two's complement)
    zAxis = (buf[5] << 6) | (buf[6] >> 2);

    // convert to signed 16-bit integer
    if (zAxis & 0x2000);
     zAxis |= 0xc000;
   }
  

  /* For example: for(;;) { } */

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END ProcessorExpert */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.3 [05.08]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/

Labels (1)
0 Kudos
5 Replies

719 Views
mjbcswitzerland
Specialist V

Hi Anthony

If you are working with the FRDM-KL25Z, load the attached binary to check operation on your board in case you have a HW issue.
On the OpenSDA VCOM (115'200 Bauds) it will display the accelerometer's register set at boot-up:

Hello, world... FRDM-KL25Z [External]
Static memory = 0x00000f40
OS Heap use = 0x027d from 0x3000
Initial stack margin 0x0000000f
3-axis accelerometer:
 0x00 0x00 0x01 0x00 0x1a 0x00 0x00 0x00 0x80 0x00 0x44 0x84 0x00 0x00 0x00
 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
 0x00 0x00 0x00 0xa5 0x00 0x00 0x00 0x00 0x00 0x00 0x00

and then you can hit the enter key and move to the I2C menu (7)

    I2C menu
===================
up           go to main menu
acc_on       enable accelerometer output
acc_off      disable output
help         Display menu specific help
quit         Leave command mode

command "acc_on" or "acc_off" to see the accelerometer values

Eg.

3-axis state: 0x00 0x08ec 0xff20 0x4184
3-axis state: 0x00 0x082c 0xfed8 0x3fd8
3-axis state: 0x00 0x0824 0xfef4 0x3f68
3-axis state: 0x00 0x0808 0xfef4 0x3f9c
3-axis state: 0x00 0x07dc 0xff08 0x3f74
3-axis state: 0x00 0x07ac 0xfed0 0x3f54
3-axis state: 0x00 0x07d4 0xfef0 0x3fa0
3-axis state: 0x00 0x07dc 0xff04 0x3fb0
3-axis state: 0x0b 0x0788 0xfed0 0x3f58
3-axis state: 0x00 0x07dc 0xfee8 0x3fbc
3-axis state: 0x00 0x07cc 0xfedc 0x3f70
3-axis state: 0x00 0x07b4 0xfeb0 0x3f94
3-axis state: 0x00 0x079c 0xfed4 0x3f7c
3-axis state: 0x00 0x0798 0xfeec 0x3f8c


Connect to the KL25's USB to do the same via direct USB-CDC and also view the internal Flash and SRAM as a hard driver (USB-MSD).

Takes 3 minutes to do in the uTasker project with the settings:
#define FRDM_KL25Z
#define USB_INTERFACE
#define USE_USB_CDC
#define USE_USB_MSD
#define FAT_EMULATION
#define I2C_INTERFACE
#define TEST_MMA8451Q
#define MMA8451Q_14BIT_RES

I would estimate faster than PE and a more interesting result - and it even works...;-)

Regards

Mark

Kinetis: http://www.utasker.com/kinetis.html
Kinetis KL25, KL26, KL27, KL28:
- http://http://www.utasker.com/kinetis/FRDM-KL25Z.html
- http://www.utasker.com/kinetis/TWR-KL25Z48M.html
- http://www.utasker.com/kinetis/FRDM-KL26Z.html
- http://www.utasker.com/kinetis/TEENSY_LC.html
- http://www.utasker.com/kinetis/FRDM-KL27Z.html
- http://www.utasker.com/kinetis/Capuccino-KL27.html
- http://www.utasker.com/kinetis/FRDM-KL28Z.html
RTC and time keeping: http://www.utasker.com/docs/uTasker/uTasker_Time.pdf


Free Open Source solution: https://github.com/uTasker/uTasker-Kinetis
Working project in 15 minutes video: https://youtu.be/K8ScSgpgQ6M

Professional Kinetis support, one-on-one training and complete fast-track project solutions: http://www.utasker.com/support.html

0 Kudos

719 Views
anthonyverfaill
Contributor II

The hardware is working fine I know because multiple boards have the same issue. If I run the program multiple times the accelerometer starts working. Its almost like it hangs up and then unhangs its self. It always seems to happen on the first build then clears up after multiple restarts. 

0 Kudos

719 Views
mjbcswitzerland
Specialist V

Hi Anthony

If the HW is Ok it can only be a software problem.

If it sometimes works you maybe have slaves holding the bus, for which you would need to have a dead-lock recovery technique built in - see https://community.nxp.com/message/398627 

Regards

Mark

0 Kudos

719 Views
anthonyverfaill
Contributor II

Thank you for your response! I am however, very new at this and need a little "hand holding" Could you recommend a fix to the code I have posted on here? 

0 Kudos

719 Views
mjbcswitzerland
Specialist V

Hi Anthony

Emergency debugging services are available at http://www.utasker.com/support.html
I may be able to look at it later in the week if it is personal hobby work; is it reproducible on a FRDM-KL25Z?

Regards

Mark

0 Kudos