Electronic – ARM Power/Exponential Function

armmathmicrocontrollerprogramming

I'm trying to code an exponential function for use on a PWM signal.

My question is, does anyone know how to do a power function on an ARM cortex-M (STM32F4 in this case, which has an FPU) microcontroller. Or would a taylor series approximation do the trick better.

I know this is question is very much programming related but as its related to microcontrollers I thought it would be ok to post here(?), plus it seems questions on stackoverflow get lost very quickly.

Best Answer

Generating a PWM signal in an embedded application generally involves a limited number of bits in the counters used to generate the signal timing. This could be as few as 8 bits or 10 bits depending upon your PWM hardware. Keep in mind too that the binary representation of the PWM values establishes a fixed universe of values in the embedded application. You can use this to your advantage in finding a solution to this problem.

The idea is to use an off-line calculation tool such as a spread sheet to compute whatever range of exponential function that you desire. This can be used to produce a look-up table of data that you then place onto the microcontroller code as a constants table. Limited bit width PWM's make the table size be practical. If you happen to have a 16-bit PWM then the table can still contain 16-bit target values but the table size can be limited to a practical size without needing 64K entries. In this latter case the target software uses linear interpolation between the table entries to find target PWM values. This technique is known as piecewise linear interpolation.

You will find this table look-up approach much less compute intensive than trying to calculate complicated floating point formulas in real time on your microcontroller. Sometimes the performance improvement gained is essential for applications that require the PWM value to update and track an input at with minimal latency.