SPWM with MCF52230

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

SPWM with MCF52230

1,188 Views
elghouchma_rach
Contributor II

Hello guys,

It is my first post in this community, and I hope that you can help me.

I am working with the MCF52230 Microcontroller in order to controle an H-bridge inverter, so I am wondering if it is possible to implement a Three-Level SPWM (Sinusoidal as a reference signal and triangular carrier signal)  in which the duty cycle change in real time depending on a controle law.

If it so, how can I do it (using the PWM module, the GPT or something else ...) ?

Thanks a lot for your help guys. 

7 Replies

903 Views
TomE
Specialist II

There are no App Notes helping you to do that on that CPU.

I searched for "motor control" on NXP's site and found this:

http://www.nxp.com/applications/internet-of-things/smart-things/motor-control/brushless-dc-bldc-moto...

http://www.nxp.com/products/reference-designs/3-phase-brushless-dc-motor-control-reference-design:BR...

That is a demo controlling a motor with a MC9S08MP16.

I would suggest looking at that as well as any other demos and App Notes you can find on NXP's site.


Then compare the features of the PWM hardware in those demos with the one on the MCF5223x series.

The PWM module in this chip has 8 8-bit PWM controllers. They can be set to left-align or centre-align, which is important when you've got them controlling a motor. You can combine them in pairs to get 4 16-bit controllers.

Check the clock divider chain to see if you can get them running at the right frequency for your control.

I don't know what you mean by "Three level SPWM". The PWM controller just generates the signals you request. The control law stuff is something you have to write in software.

I assume you would need to start the PWM going with the outputs in a specific phase relationship, like 90 degrees apart from each other. This PWM controller doesn't let you do that, or doesn't let you do that easily. You can't start them in a specific phase relationship. You'd have to start one counter, wait a while (in software) and then try to start the next one at the right time so it is in the right phase with the other one (or ones). This may tell you it isn't meant to be a motor controller.

You could use the DMA Timers to try and generate PWM, but it would be very difficult and likely to fail (badly). You'd have to have the counter free-run, and then try and set the comparison registers with values just "far enough ahead" of the current count values so they'll toggle their outputs ONCE "then". Then you have to reload for the next edge. Instead of the hardware generating a PWM waveform, the software would have to generate every edge.

You may be better off with a motor controller chip that just does all of this for you, connected to an SPI or I2C bus on the main CPU that tells it what to do.

Tom

903 Views
elghouchma_rach
Contributor II

Thanks a lot for your help and  the time you have given for my question. 

in fact we have already generated the PWM with that µcontroler for the command of the inverter, but using a pre-calculated table of duty_cycle,
currently our chalenge is: how to calculate this duty cycle in the real time,We have tried,but it spends a lot of time to calculate the sinus function ...
so my purpose in this discution is to know If it is already possible to do it with this CPU, because i found that freescale use the DSCs family in such as applications,
for the  "Three level SPWM"   you can check this image.3-Level PWM.PNG 
0 Kudos

903 Views
TomE
Specialist II

By "sinus function" I have to assume you mean "SIN function". "Sinus" means "nose".

Why would you need to calculate SIN? A control routine doesn't need SIN to 24 bits or more of precision. It could probably do with four bits.

If you have 90 spare memory locations you can precalculate the SIN of every angle to the nearest degree from zero to 90. Or whatever a convenient breakdown into fractions of a radian is. You can fill the table by doing the calculations on the device, or fill in a fixed table with values from a spreadsheet, or any way you like. Then it takes only a single memory read into that array to "calculate" the SIN (together with working out which quadrant you're in). At 60MHz you should be able to "calculate" them at about 5 million per second. Grabbing the nearest like this gets you to at least 7 bits of precision. If you want it better you just do a fast linear interpolation between samples. But try to avoid doing divide operations on this CPU as they are relatively slow if you are really pushing it. But this CPU can do 2 million divides per second worst case, and is faster than that most of the time, so don't bother trying to make that faster.

Type "cosine approximation" into Google and you'll get a lot of references.

I don't know what you mean my "DSC", but if you meant "DSP" then I'd be pretty sure they'd use lookup tables for trig functions as well.

If you really need heavy duty floating point, then there are ColdFire CPUs with 64-bit FPUs that can do floating point multiply in 4 clock cycles and divides in 23, but not the one you're using.

Tom

0 Kudos

903 Views
elghouchma_rach
Contributor II

- "Why would you need to calculate SIN" ? 

because the control loop 's output is the angle 'theta' so i would calculate SIN in order to compare it with Triangular signal.

-the approximation of SIN is not souhaitable for our appliquation, we use the function "sin" of math library.

-freescale DSCs family : is  a family of digital signal controllers (DSCs). It combines, on a single chip, the processing power of a DSP and the functionality of a microcontroller with a flexible set of  peripherals to create a cost-effective solution.

thanks again,

Rachid.

0 Kudos

903 Views
TomE
Specialist II

> the approximation of SIN is not souhaitable for our appliquation

You're already using an approximation. How do you think the library "sin" function performs its calculation? It performs an approximation. One that is good to 24 or 56 bits, but still an "approximation".

So if it isn't fast enough for your control loop, you simply replace it with one that is faster. The tradeoff is that you might lose some precision.

The graph (of triangles and triangles) looks like it is calculating the output PWM signal.

Which is only eight bits for that controller, unless you're running two of them in series.

So any more accuracy than that (one part in 256 or about 0.4%) in your "sin" function would be wasted.

I've just typed a few tables into Microsoft Excel, calculating a linear approximation between samples of the sin function's output. With only TEN samples between 0 and 90 degrees, the worst-case error (expressed as a ratio) is 0.9969. Which is 0.3%. Which is more than accurate enough if the output is an 8-bit PWM signal.

If that isn't good enough, using 100 samples between 0 and 90 degrees has the approximation accurate to 0.999969 worst case.

Since you're using a CPU that doesn't have an FPU (hardware floating point), you're using a floating point emulation library. They're pretty slow too. You might find that using "float" instead of "double" gets you more speed. You might also want to look for a single-precision math library - one that gives you single-precision "sin". Together with using "float" that might be fast enough for your needs.

As for NXP's DSCs, the NXP 56F801X DSC is the one recommended for "motor control applications". The CPU in that one is 32 bit, 32MHz and doesn't have floating point. So it wouldn't be any better for you than the one you're using.

Tom

0 Kudos

903 Views
TomE
Specialist II

Here's an implementation of "sin" that uses a Taylor Series Expansion, and might be faster than the library one that you're using:

https://github.com/eblot/newlib/blob/master/newlib/libm/mathfp/sf_sine.c

The double precision equivalent is visible in the parent directory as "s_sine.c". It uses the same Taylor Series, but uses 8 elements in the expansion, while the single precision one only uses 4.

Note the "README" on the Parent directory that says why this has been mothballed.

Tom

0 Kudos

903 Views
elghouchma_rach
Contributor II

Thanks a lot TOM.

0 Kudos