MPS430 cordicPWM sine wave generator

Started by December 13, 2012
```The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
Fedex.
> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx

The person coded the cordic, and then implemented the sine wave with PWM.

```
```On Thursday, December 13, 2012 2:46:18 AM UTC-5, miso wrote:
> The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>
> Fedex.
>
> > http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx
>
>
>
> The person coded the cordic, and then implemented the sine wave with PWM.

Not really getting what the CORDIC was used for..doesn't the MPS430 store literals in memory?
```
```On 12/13/2012 5:34 PM, bloggs.fredbloggs.fred@gmail.com wrote:
> On Thursday, December 13, 2012 2:46:18 AM UTC-5, miso wrote:
>> The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>>
>> Fedex.
>>
>>> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx
>>
>>
>>
>> The person coded the cordic, and then implemented the sine wave with PWM.
>
> Not really getting what the CORDIC was used for..doesn't the MPS430 store literals in memory?
>

I'm sure there is some cordic description on line, but I can describe it
reasonably well from memory. The cordic has a table of sines and
cosines. It essentially goes from 90 deg, 45 deg, 22.5 deg etc in
decreasing powers of two. I say essentially because what you do is
pretend the circle has a power of two number of degrees, i.e.256
degrees, 512, etc. This makes the algorithm easier to program.

The cordic takes the angle in question and uses a successive
approximation scheme to drive it to zero. That is, if you were looking
for the sine or cosine of 50 degrees, your first step would be to
subtract 45 degrees, which is a value in your table. You keep this up
until you have driven it to zero as best as you can. the accumulators
will contain the sine and cosine of the initial angle.

Now you could generate a sine wave with stored samples of the signal,
but that isn't very flexible. To generate a sine wave with a cordic, you
accumulate phase. That is you pick a phase step size which represents
the fraction of a sine wave that you are going to use in the
approximation. Say you wanted 1 degree steps. Then you would feed the
cordic 1, 2, 3, etc. Now if you set up your pretend circle to have 256
degrees in it, then the phase accumulator will always return to zero and
the sequence would continue. So your phase input is a digital sawtooth
signal.

But you could have picked the phase interval to be some number which
won't return to zero. Say you picked 3 degree steps. Then the sequence
would go 0,3,6...255, 2,5,8, etc. This allows for more flexibility in
generating the sine wave relative to the clock.

I haven't looked at code yet, but that should be how it is written, with
some foo I left out. As you do the successive approximation scheme, you
are basically rotating a vector from the initial starting point to zero.
The cordic causes the vector to expand, so you need to seed the cordic
with a scaled version of the vector. I 'd have to dig up a book to
explain this is detail, but it is well documented.

So the person I presume picked or let the user input a phase step, then
creates the sine and cosine on the fly. Those values are then generated
with a PWM type DAC. You get the sine and cosine at the same time. This
means you have a quadrature phase available if need be.

There is a PHd. dissertation at Stanford that is the bible of cordic
processing. The authors name (first or maybe last) is Ahmed, but if you
search Stanford library on line, you would find the dissertation. It is
or was in the Terman library, or perhaps Green. It was way better than
what you find in most signal processing books. At least 90% of PHd
dissertations aren't worth the paper they are printed on, but we built
multi-millions of chips using schemes out of that dissertation back in
the day.

Most DSP books will cover generation  of sine and cosine with the
cordic, but Ahmed's dissertation covered the transcendental functions.
So you could linearize thermocouples with it, etc. Or as I stated in the
other post, take the arcsine and do a FSK demod.

```
```On Thursday, December 13, 2012 10:23:30 PM UTC-5, miso wrote:
> On 12/13/2012 5:34 PM, bloggs.fredbloggs.fred@gmail.com wrote:
>=20
> > On Thursday, December 13, 2012 2:46:18 AM UTC-5, miso wrote:
>=20
> >> The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>=20
> >>
>=20
> >> Fedex.
>=20
> >>
>=20
> >>> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontroll=
erprojects/609573.aspx
>=20
> >>
>=20
> >>
>=20
> >>
>=20
> >> The person coded the cordic, and then implemented the sine wave with P=
WM.
>=20
> >
>=20
> > Not really getting what the CORDIC was used for..doesn't the MPS430 sto=
re literals in memory?
>=20
> >
>=20
>=20
>=20
> I'm sure there is some cordic description on line, but I can describe it=
=20
>=20
> reasonably well from memory. The cordic has a table of sines and=20
>=20
> cosines. It essentially goes from 90 deg, 45 deg, 22.5 deg etc in=20
>=20
> decreasing powers of two. I say essentially because what you do is=20
>=20
> pretend the circle has a power of two number of degrees, i.e.256=20
>=20
> degrees, 512, etc. This makes the algorithm easier to program.
>=20
>=20
>=20
> The cordic takes the angle in question and uses a successive=20
>=20
> approximation scheme to drive it to zero. That is, if you were looking=20
>=20
> for the sine or cosine of 50 degrees, your first step would be to=20
>=20
> subtract 45 degrees, which is a value in your table. You keep this up=20
>=20
> until you have driven it to zero as best as you can. the accumulators=20
>=20
> will contain the sine and cosine of the initial angle.
>=20
>=20
>=20
> Now you could generate a sine wave with stored samples of the signal,=20
>=20
> but that isn't very flexible. To generate a sine wave with a cordic, you=
=20
>=20
> accumulate phase. That is you pick a phase step size which represents=20
>=20
> the fraction of a sine wave that you are going to use in the=20
>=20
> approximation. Say you wanted 1 degree steps. Then you would feed the=20
>=20
> cordic 1, 2, 3, etc. Now if you set up your pretend circle to have 256=20
>=20
> degrees in it, then the phase accumulator will always return to zero and=
=20
>=20
> the sequence would continue. So your phase input is a digital sawtooth=20
>=20
> signal.
>=20
>=20
>=20
> But you could have picked the phase interval to be some number which=20
>=20
> won't return to zero. Say you picked 3 degree steps. Then the sequence=20
>=20
> would go 0,3,6...255, 2,5,8, etc. This allows for more flexibility in=20
>=20
> generating the sine wave relative to the clock.
>=20
>=20
>=20
> I haven't looked at code yet, but that should be how it is written, with=
=20
>=20
> some foo I left out. As you do the successive approximation scheme, you=
=20
>=20
> are basically rotating a vector from the initial starting point to zero.=
=20
>=20
> The cordic causes the vector to expand, so you need to seed the cordic=20
>=20
> with a scaled version of the vector. I 'd have to dig up a book to=20
>=20
> explain this is detail, but it is well documented.
>=20
>=20
>=20
> So the person I presume picked or let the user input a phase step, then=
=20
>=20
> creates the sine and cosine on the fly. Those values are then generated=
=20
>=20
> with a PWM type DAC. You get the sine and cosine at the same time. This=
=20
>=20
> means you have a quadrature phase available if need be.
>=20
>=20
>=20
> There is a PHd. dissertation at Stanford that is the bible of cordic=20
>=20
> processing. The authors name (first or maybe last) is Ahmed, but if you=
=20
>=20
> search Stanford library on line, you would find the dissertation. It is=
=20
>=20
> or was in the Terman library, or perhaps Green. It was way better than=20
>=20
> what you find in most signal processing books. At least 90% of PHd=20
>=20
> dissertations aren't worth the paper they are printed on, but we built=20
>=20
> multi-millions of chips using schemes out of that dissertation back in=20
>=20
> the day.
>=20
>=20
>=20
> Most DSP books will cover generation  of sine and cosine with the=20
>=20
> cordic, but Ahmed's dissertation covered the transcendental functions.=20
>=20
> So you could linearize thermocouples with it, etc. Or as I stated in the=
=20
>=20
> other post, take the arcsine and do a FSK demod.

Thanks for taking the time with that lengthy explanation. Wiki has a suffic=
iently good article on it, an easy read http://en.wikipedia.org/wiki/CORDIC

Your TI link states he does 100KHz PWM, but they can't mean that literally.=
They must mean the PWM counter is being clocked at 100KHz where it's prelo=
aded with the amplitude computed by CORDIC on the fly. CORDIC then makes se=
nse when he's using a very large number of phase samples way in excess of t=
he onboard RAM.
```
```On Thu, 13 Dec 2012 20:27:46 -0800 (PST),
bloggs.fredbloggs.fred@gmail.com wrote:

><snip>

> Your TI link states he does 100KHz PWM, but they can't mean
> that literally. They must mean the PWM counter is being
> clocked at 100KHz where it's preloaded with the amplitude
> computed by CORDIC on the fly. CORDIC then makes sense when
> he's using a very large number of phase samples way in
> excess of the onboard RAM.

I think it was putting out 60Hz, if I remember correctly what

Jon
```
```On Wed, 12 Dec 2012 23:46:18 -0800, the renowned miso <miso@sushi.com>
wrote:

>The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>Fedex.
>> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx
>
>The person coded the cordic, and then implemented the sine wave with PWM.
>

Well, it's an interesting exercise.. it's done in FPGAs when you need
a lot of time resolution, but for a 64-point sine wave with fixed
phase, all you need is 16 words of lookup table.

Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
```
```On Fri, 14 Dec 2012 07:16:55 -0500, Spehro Pefhany
<speffSNIP@interlogDOTyou.knowwhat> wrote:

>On Wed, 12 Dec 2012 23:46:18 -0800, the renowned miso <miso@sushi.com>
>wrote:
>
>>The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>>Fedex.
>>> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx
>>
>>The person coded the cordic, and then implemented the sine wave with PWM.
>>
>
>Well, it's an interesting exercise.. it's done in FPGAs when you need
>a lot of time resolution, but for a 64-point sine wave with fixed
>phase, all you need is 16 words of lookup table.
>
>
>Best regards,
>Spehro Pefhany

I've done 16-bit in/out sine/cos by direct lookup; memory is cheap
these days. OK, once I folded it in half so that I only needed 32K
points in the table.

When ram is scarce, like in a small flash-based uP or a small FPGA,
table lookup with linear interpolation is fast and easy, for stuff
like trigs and thermocouples. Cordic looks like a lot more trouble
than it's worth, and I'd imagine that accumulation of rounding errors
would need a lot of thought.

--

John Larkin         Highland Technology, Inc

jlarkin at highlandtechnology dot com
http://www.highlandtechnology.com

Precision electronic instrumentation
Picosecond-resolution Digital Delay and Pulse generators
Custom laser drivers and controllers
Photonics and fiberoptic TTL data links
VME thermocouple, LVDT, synchro   acquisition and simulation
```
```On Fri, 14 Dec 2012 10:30:04 -0800, John Larkin
<jlarkin@highlandtechnology.com> wrote:

>On Fri, 14 Dec 2012 07:16:55 -0500, Spehro Pefhany
><speffSNIP@interlogDOTyou.knowwhat> wrote:
>
>>On Wed, 12 Dec 2012 23:46:18 -0800, the renowned miso <miso@sushi.com>
>>wrote:
>>
>>>The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>>>Fedex.
>>>> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx
>>>
>>>The person coded the cordic, and then implemented the sine wave with PWM.
>>>
>>
>>Well, it's an interesting exercise.. it's done in FPGAs when you need
>>a lot of time resolution, but for a 64-point sine wave with fixed
>>phase, all you need is 16 words of lookup table.
>>
>>
>>Best regards,
>>Spehro Pefhany
>
>I've done 16-bit in/out sine/cos by direct lookup; memory is cheap
>these days. OK, once I folded it in half so that I only needed 32K
>points in the table.

I was suggesting folding it in quarters.. you go forward through the
table to pi/2, backwards to pi, then repeat with the sign flipped.
That's probably what you meant anyway.

>When ram is scarce, like in a small flash-based uP or a small FPGA,
>table lookup with linear interpolation is fast and easy, for stuff
>like trigs and thermocouples. Cordic looks like a lot more trouble
>than it's worth, and I'd imagine that accumulation of rounding errors
>would need a lot of thought.

We are doing more like 24bits with tens of thousands of samples per
cycle, and very finely adjusted phase differences between generated
signals. The CORDIC (COordinate Rotation DIgital Computer) algorithm
is a reasonable choice for that. It is the algorithm used to calculate
trig functions in your HP35.

```
```On Fri, 14 Dec 2012 16:34:20 -0500, Spehro Pefhany
<speffSNIP@interlogDOTyou.knowwhat> wrote:

>On Fri, 14 Dec 2012 10:30:04 -0800, John Larkin
><jlarkin@highlandtechnology.com> wrote:
>
>>On Fri, 14 Dec 2012 07:16:55 -0500, Spehro Pefhany
>><speffSNIP@interlogDOTyou.knowwhat> wrote:
>>
>>>On Wed, 12 Dec 2012 23:46:18 -0800, the renowned miso <miso@sushi.com>
>>>wrote:
>>>
>>>>The eval kit is a whole \$4.30 including shipping, and it comes 2nd day
>>>>Fedex.
>>>>> http://e2e.ti.com/group/microcontrollerprojects/m/msp430microcontrollerprojects/609573.aspx
>>>>
>>>>The person coded the cordic, and then implemented the sine wave with PWM.
>>>>
>>>
>>>Well, it's an interesting exercise.. it's done in FPGAs when you need
>>>a lot of time resolution, but for a 64-point sine wave with fixed
>>>phase, all you need is 16 words of lookup table.
>>>
>>>
>>>Best regards,
>>>Spehro Pefhany
>>
>>I've done 16-bit in/out sine/cos by direct lookup; memory is cheap
>>these days. OK, once I folded it in half so that I only needed 32K
>>points in the table.
>
>I was suggesting folding it in quarters.. you go forward through the
>table to pi/2, backwards to pi, then repeat with the sign flipped.
>That's probably what you meant anyway.

I didn't need to save that much ram; the quarters thing needs more
thinking and more runtime.

>
>>When ram is scarce, like in a small flash-based uP or a small FPGA,
>>table lookup with linear interpolation is fast and easy, for stuff
>>like trigs and thermocouples. Cordic looks like a lot more trouble
>>than it's worth, and I'd imagine that accumulation of rounding errors
>>would need a lot of thought.
>
>We are doing more like 24bits with tens of thousands of samples per
>cycle, and very finely adjusted phase differences between generated
>signals. The CORDIC (COordinate Rotation DIgital Computer) algorithm
>is a reasonable choice for that. It is the algorithm used to calculate
>trig functions in your HP35.
>

Even at 24 bits, I'd expect that lookup and interpolation would be
pretty good, and certainly fast. Use 32-bit values in the lookup
table. Sine isn't that radical a curve.

--

John Larkin         Highland Technology, Inc

jlarkin at highlandtechnology dot com
http://www.highlandtechnology.com

Precision electronic instrumentation
Picosecond-resolution Digital Delay and Pulse generators
Custom laser drivers and controllers
Photonics and fiberoptic TTL data links
VME thermocouple, LVDT, synchro   acquisition and simulation
```
```You simply don't get it. You can change the frequency on the fly with
the cordic without changing the sample rate. You simply change the phase
step.

```