AnsweredAssumed Answered

KL25Z Accelerometor Read

Question asked by anthonyverfaillie on Jan 16, 2018
Latest reply on Jan 29, 2018 by Mark Butcher

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.
**
** ###################################################################
*/

Outcomes