Accelerometer KL25Z

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

Accelerometer KL25Z

6,231 Views
mauriciofarina
Contributor II

Hey guys!

I need some help!

I'm working on a kl25z for the freescale cup and i'm trying to program the build-in accelerometer...

I just got into college, so I don't know anything about programming processors and stuff like that...

I've tried lots of tutorials, but my CW project does't have the processor expert, and, if I add it, it messes the whole thing...

My software is based on the sample for the TFC...

Freescale Cup Shield for the Freedom KL25Z

could someone help me with the code for it?

Labels (1)
10 Replies

2,230 Views
lander
Contributor IV

Hi there fellow college student!

You'll start recognizing people around these boards (the community) and you'll probably start to notice some of the same people posting tutorials or articles they have written on various useful topics. One person has helped me learn a lot, Erich Styger, his blog helps with common issues or questions people may have while their projects excel (MCU on Eclipse | Everything on Eclipse, Microcontrollers and Software) and he has a github account that holds many different demo projects. There are many projects on his github for KL25z and there is a demo project on there for KL25z's accelerometer using processor expert. Here is his github link: ErichStyger/mcuoneclipse · GitHub.  Just load the .zip onto your computer and import the Freedom_Accel project on to Eclispe!

Hope this helps,

Lander

0 Kudos

2,230 Views
mauriciofarina
Contributor II

Yeah... I've searched a lot... but the problem is that int all of the tutorials that I found they all use the process expert... and my code doesn't support it...

If I try to add the process expert it bugs all my code...

2,230 Views
lander
Contributor IV

I don't really understand. You don't need processor expert because your code doesn't support it, but you try to add it? The rules for the freescale cup say that you can or cannot use processor expert? And when you say bugs, you get errors? What kind of errors?

2,230 Views
mauriciofarina
Contributor II

NOO

I can't use processor expert because it conflicts with the parameters in my code!

when I add it I get like 20 errors... then i correct them and i get many others.........

What I think I need is a library for the accelerometer...

like a .c and a .h code for it...

0 Kudos

2,230 Views
quevedo
Contributor V

Hello Mauricio,

I am giving a software lab course at the University. As students are learning the basics of low-level programming (register level), I do not use Processor Expert in this course. The final experiment is accessing the accelerometer through I2C, so I have developed a library for this purpose, derived from an example library for Kinetis Family, which I simplified and suited for my needs. I am sending the .h and .c files attached to this reply.

For controlling the accelerometer, you just have to write the right values to the right config registers. Remember that the accelerometer address (SlaveID) is 0x1D.

If you just want to read raw (unfiltered) data from XYZ axes, using a full scale range of 2g, you may initialize the accelerometer using this code:

I2CWriteRegister(0x1D, 0x0E, 0x00); // Writes 0x00 to data config register (0x0E).

I2CWriteRegister(0x1D, 0x2A, 0x39); // Writes 0x39 to general config register 1 (0x2A)

Now the accelerometer starts working. It will make a new read (all 3 axes) every 640ms. If you want other periods, you just have to write the right value on 0x2A register, instead of 0x39. For 160ms, write 0x31; for 80ms, write 0x29; for 20ms, write 0x21.

You may read the latest collected values this way:

I2CReadMultiRegisters(0x1D, 0x01, 6, data); // Reads 6 sequential registers starting from MSB of X axis (0x01). "data" is a 6-element array of char.

The result will be the values of 3 axes loaded on "data" array: [MSB_X, LSB_X, MSB_Y, LSB_Y, MSB_Z, LSB_Z]. You may also test if new data is available before reading using:

dr = (I2CReadRegister(0x1D, STATUS) & 0x08); // dr is zero if no new data is available; it is 0x08 if new data is available.

I hope that it helps. Just message me if you need something else.

By the way, Processor Expert should not keep giving you errors. It would be interesting if you could provide more details (including screen captures) about the errors.

Cheers,

Antonio

2,230 Views
joaolouzada
Contributor I

Hello Antonio,

I have tried use your code but the code gets stuck in this line:


void Default_Handler()

{

    __asm("bkpt"); // <-------------

}

after have ran from

I2CWriteRegister(0x1D, 0x0E, 0x00);

and

IIC_StartTransmission(SlaveID,MWSR);

and

i2c_Start();

Do you have any idea about what can be happening?

Thanks.

0 Kudos

2,230 Views
quevedo
Contributor V

When code stucks at the default interrupt handler, it is probably some math error (like divide by zero) or a hardware fault. There is a way to know what kind of error is happening in this case. Just check this link:

Oh my! An Interrupt… | MCU on Eclipse

Doing this you can see if you have a dardware fault, which is the most probable. Then, check on these 2 links:

http://mcuoneclipse.com/2012/11/24/debugging-hard-faults-on-arm-cortex-m/

A Processor Expert Component to Help with Hard Faults | MCU on Eclipse

You may get more details on your error. Also, create a breakpoint inside i2c_Start() function, right at the beginning and use step-by-step execution, and see in what instruction the interrupt is triggered at.

Post your results so I can help you more.

Cheers

Antonio

0 Kudos

2,230 Views
lilyann
Contributor I

Hi Antonio, I am getting the exact same issue as Joao, my code getting an interrupt handler error inside 12c_start();. I tried going to the links you provided, but I can't really relate since I'm not using processor expert. Did you come up with any fix for this?

Thanks.

0 Kudos

2,230 Views
adriancano
NXP Employee
NXP Employee

Hi,

I recommend to post a new question in the Kinetis Microcontrollers space. There it will have more visibility and more members can help you with the issue.

Regards,

Adrian

0 Kudos

2,230 Views
santiago_gonzal
NXP Employee
NXP Employee

Hello mauricio,

Processor Expert just creates a .c and a .h file for every peripheral of the microcontroller.

In the case of the accelerometer, you will communicate with it through I2C. You can check the example that has been suggested to you from Erich's blog, look at the code that processor expert generates and then try to implement it in your project.

Good luck!

Regards,

Santiago

El 17/10/2013, a las 18:17, "Mauricio Farina" <admin@community.freescale.com<mailto:admin@community.freescale.com>> escribió:

<https://community.freescale.com/>

<https://community.freescale.com/>

Accelerometer KL25Z

reply from Mauricio Farina<https://community.freescale.com/people/mauriciofarina?et=watches.email.thread> in Kinetis Microcontrollers - View the full discussion<https://community.freescale.com/message/356413?et=watches.email.thread#356413>