简体   繁体   中英

STM32F4 float to int conversion

On STM32F4 MCU I need to convert floating point numbers into integer.

I have FP numbers as result from arm_pid_f32() function but later I need to update internal DAC's values which accept 12bit integer.

I know that i will lose some "precision" but if I don't convert it and let it pass directly into DAC update function i got some strange behaviors which makes me problem.

What is the best approach to done that?

I'm using KEIL MDK-ARM IDE + CMSIS DSP library.

BTW: The MCU is used in PID control loop, I have 12bit A/D, PID algorithm with my coefficients and DAC as output device. DAC is currently used for debugging purpose and later will bi replaced with PWM module which also require integer numbers.

I'm assuming that you have a DAC that accepts two's-complement integer inputs.

Divide the float variable by the largest possible value that the float variable can have. That gives you a float in the range [-1.0...+1.0]

Multiply by the integer value that produces a positive full scale output from the DAC, with stated assumption that would be 2047. Round if you want, then cast to an integer.

If the DAC needs an unsigned positive integer input value instead, you need to add 2048 to this result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM