Section 3: Exercises

Stellaris® ARM® CortexTM-M4F Training
Floating Point Unit
Section 3: Exercises
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 1
Agenda
1. Overview
4
2. Exercise 1: Sine Wave Amplitude
Objective
Theory
Code Modifications
Results
5
5
6
7
9
3. Exercise 2: Square Wave
Objective
Theory
Code Modifications
Results
10
10
11
12
14
4. Exercise 3: Traingular Wave
Objective
Theory
Code Modifications
Results
15
15
16
17
19
5. Exercise 4: Saw tooth Wave
Objective
Theory
Code Modifications
Results
19
19
20
21
23
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 2
Note:
IMPORTANT: Before you move ahead with the exercises in this section, you must make sure
that you have performed the following steps. Ideally, you should have performed the following
steps in Section 2, if so then you can jump to the overview on the next page.
1) Assuming that you have installed StellarisWare at its default location on your computer’s
hard drive, browse to “C:\StellarisWare\boards\ek-lm4f232\sine_demo”
2) Rename the sine_demo.c file to sine_demo_default.c
3) Copy sine_demo.c provided to you with the lab (should be located in the same
directory as this document) to the above directory.
4) The comment in the beginning of sine_demo.c must look as follows:
//****************************************************************************
* // // sine_demo.c ‐ Stellaris LM4F FPU Lab // // Copyright (c) 2011 Texas Instruments Incorporated. All rights reserved. // Software License Agreement Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 3
Overview
In this section, we will use the floating point unit on Stellaris M4F MCUs to compute nonsinusoidal waveforms such as a square wave, triangle wave and sawtooth wave from a
sinusoidal waveform and plot them on the OLED screen of EK-LM4F232 evaluation board.
Image source: http://en.wikipedia.org/wiki/File:Waveforms.svg
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 4
Exercise 1: Objectives
1. Take an existing FPU demo project (sine_demo project from section 2) and modify it to
configure the amplitude of the sine wave.
2. Upon successful completion of this exercise, the following should appear on the screen.
Exercise 1: Screen Shot
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 5
Theory
In the project provided in section 2 of this workshop, we declared three variable of float datatype
as follows:
float fElapsedTime; float fRadians; float fSine;
We computed the value of sine wave using fsin() function in C.
fSine = sinf(fRadians) * 0.5F;
In this exercise, we will compute a sine wave with different amplitude by simply
multiplying/dividing the original sine wave by a factor that we will first declare and initialize.
unsigned long ulNumHarmonics=1;
fSine = sinf(fRadians) * 1/ulNumHarmonics; By changing the value of ulNumHarmonics, rebuilding and redownloading the code, you we will
see a change in the amplitude of the sine wave on the OLED screen.
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 6
Code Modifications
Please refer to file sine_demo.c in the CCS project explorer window. Double click on this file to
open it in the project window for editing.
Various sections have been marked in the code for the purpose of pointing users to the relevant
locations in the code to perform modifications.
1. Before we move ahead with the exercise, please perform the following modification in the
sine_demo.c file. This following displays text on screen. We want to remove this text from the
screen so that we can examine the results waveforms on a clutter-free screen.
Locate “Defines the Y-axis for the strip chart.“ in the code and and remove the text
for the title keeping everything else the same.
Change from:
//***************************************************************************
**
//
// Defines the Y-axis for the strip chart.
//
//***************************************************************************
**
static tStripChartAxis sAxisY =
{
"SIN(2pi*t/4)*0.5", // title of the axis
"-1",
// label for minimum of axis
"+1",
// label for maximum of axis
Change to:
//***************************************************************************
**
//
// Defines the Y-axis for the strip chart.
//
//***************************************************************************
**
static tStripChartAxis sAxisY =
{
"",
// title of the axis
"-1",
// label for minimum of axis
"+1",
// label for maximum of axis
2. To change the ampiltude of the sine wave, we will first comment the expression for the
default sine wave.
Change from:
//
// Compute the sine.
Multiply by 0.5 to reduce the amplitude.
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 7
// Section 2
//
fSine = sinf(fRadians) * 0.5F;
Change to:
//
// Compute the sine. Multiply by 0.5 to reduce the amplitude.
// Section 2
//
// fSine = sinf(fRadians) * 0.5F;
3.Now, we will enable the code that calculates the sine wave with the increased amplitude.
Locate “Exercise 1“ in the code and simply change while(0) to while (1).
Change from:
//
// Exercise 1 : Increasing the amplitude of sine wave
//
while(0)
{
fSine = sinf(fRadians) * 1/ulNumHarmonics;
break;
}
Change to:
//
// Exercise 1 : Increasing the amplitude of sine wave
//
while(1)
{
fSine = sinf(fRadians) * 1/ulNumHarmonics;
break;
}
4. Rebuild the code, and download it to the microcontroller.
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 8
Results
Upon successful completion of this exercise, the following should appear on the screen.
Congratulations, you have completed exercise 1 of this lab while using the floating point unit in
Stellaris M4F MCU!
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 9
Exercise 2: Objectives
1. Take an existing FPU demo project and modify it to configure the sine wave to generate
a square wave.
2. Upon successful completion of this exercise, the following should appear on the screen.
Exercise 2: Screen Shot
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 10
Theory
A square wave is a non-sinusoidal waveform that alternates regularly and instantaneously
between two levels. A square wave can be approximated from a sine wave and can be
expressed as an infinite series using the Fourier expansion with cycle frequency f over time t as
follows:
For a reasonable approximation to the square-wave shape, at least the fundamental and third
harmonic need to be present, with the fifth harmonic being desirable.
4
sin 2
4
sin 2 2
1
2
1
1
sin 6
3
1
sin 10
5
…
Source: http://en.wikipedia.org/wiki/Square_wave
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 11
Code Modifications
In this exercise, we will modify the code to generate a square wave from a sine wave. Please
peform the following modifications:
1. First, we will disable the code that calculates the sine wave with increased amplitude. Locate
“Exercise 1“ in the code and simply change while(0) to while (1).
Change from:
//
// Exercise 1 : Increasing the amplitude of sine wave
//
while(1)
{
fSine = sinf(fRadians) * 1/ulNumHarmonics;
break;
}
Change to:
//
// Exercise 1 : Increasing the amplitude of sine wave
//
while(0)
{
fSine = sinf(fRadians) * 1/ulNumHarmonics;
break;
}
2. Now, we will enable the code that generates a square wave. Locate “Exercise 2“ in the
code and simply change while(0) to while (1).
Change from:
//
// Exercise 2 : Square wave
//
while(0)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=3; uli<ulNumHarmonics; uli+=2)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float) (uli));
}
break;
}
Change to:
//
// Exercise 2 : Square wave
//
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 12
while(1)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=3; uli<ulNumHarmonics; uli+=2)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float) (uli));
}
break;
}
3. Rebuild the code, and download it to the microcontroller. You may not see a perfect square
wave on the screen yet.
4. Please find the location in the code where variable ulNumHarmonics is declared. This should
be located in the beginning of main().
int
main(void)
{
unsigned
unsigned
unsigned
unsigned
long
long
long
long
ulItemCount = 0;
ulLastTickCount = 0;
uli;
ulNumHarmonics=1;
5. Change the value of ulNumHarmonics from 1 to a higher number (say 5 or 10). Rebuild the
code, and download it to the microcontroller. You may see a some what square wave on the
screen.
6. Continue to increase the value of ulNumHarmonics to about 30. Rebuild the code, and
download it to the microcontroller. You will the square wave on the screen.
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 13
Results
Upon successful completion of this exercise, the following should appear on the screen.
Congratulations, you have completed exercise 2 of this lab while using the floating point unit in
Stellaris M4F MCU!
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 14
Exercise 3: Objectives
1. Take an existing FPU demo project and modify it to configure the sine wave to generate
a triangular wave.
2. Upon successful completion of this exercise, the following should appear on the screen.
Exercise 3: Screen Shot
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 15
Theory
A triangle wave is a non-sinusoidal waveform named for its triangluar shape. Like a square
wave, the triangle wave contains only odd harmonics. It is possible to approximate a triangle
wave with additive synthesis by adding odd harmonics of the fundamental, multiplying every
(4n−1)th harmonic by −1 (or changing its phase by π), and rolling off the harmonics by the
inverse square of their relative frequency to the fundamental.
It can be approximated from a sine wave and can be expressed as an infinite series using
the Fourier expansion with angular frequency ω over time t as follows:
8
sin
8
1
1
sin 3
9
sin 2
2
1
1
1
sin 5
25
…
Source: http://en.wikipedia.org/wiki/Triangle_wave
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 16
Code Modifications
In this exercise, we will modify the code to generate a triangular wave from a sine wave. Please
peform the following modifications:
1. First, we will disable the code that generates a square wave. Locate “Exercise 2“ in the
code and simply change while(0) to while (1).
Change from:
//
// Exercise 2 : Square wave
//
while(1)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=3; uli<ulNumHarmonics; uli+=2)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float) (uli));
}
break;
}
Change to:
//
// Exercise 2 : Square wave
//
while(0)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=3; uli<ulNumHarmonics; uli+=2)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float) (uli));
}
break;
}
2. Now, we will enable the code that generates a triangular wave. Locate “Exercise 3“ in the
code and simply change while(0) to while (1).
Change from:
//
// Exercise 3 : Triangle wave
//
while(0)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=5; uli<ulNumHarmonics; uli+=4)
{
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 17
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
for (uli=3; uli<ulNumHarmonics; uli+=4)
{
fSine -= (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
break;
}
Change to:
//
// Exercise 3 : Triangle wave
//
while(1)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=5; uli<ulNumHarmonics; uli+=4)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
for (uli=3; uli<ulNumHarmonics; uli+=4)
{
fSine -= (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
break;
}
3. Rebuild the code, and download it to the microcontroller. You should see a triangular wave on
the screen.
4. Please find the location in the code where variable ulNumHarmonics is declared. This should
be located in the beginning of main().
int
main(void)
{
unsigned
unsigned
unsigned
unsigned
long
long
long
long
ulItemCount = 0;
ulLastTickCount = 0;
uli;
ulNumHarmonics=1;
5. Change the value of ulNumHarmonics, rebuild the code, download it to the microcontroller
and notice the change in the shape of the waveform.
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 18
Results
Upon successful completion of this exercise, the following should appear on the screen.
Congratulations, you have completed exercise 3 of this lab while using the floating point unit in
Stellaris M4F MCU!
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 19
Exercise 4: Objectives
1. Take an existing FPU demo project and modify it to configure the sine wave to generate
a sawtooth wave.
2. Upon successful completion of this exercise, the following should appear on the screen.
Exercise 4: Screen Shot
Theory
A triangle wave is a non-sinusoidal waveform named for its sawtooth shape.
This sawtooth function has the same phase as the sine function. A sawtooth waveform can be
constructed using additive synthesis. A sawtooth can be approximated from a sine wave and
can be expressed as an infinite series using the Fourier expansion with frequency f over time t
as follows:
2
1
sin 2
Source: http://en.wikipedia.org/wiki/Sawtooth_wave
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 20
Code Modifications
In this exercise, we will modify the code to generate a sawtooth wave from a sine wave. Please
peform the following modifications:
1. First, we will disable the code that generates a triangular wave. Locate “Exercise 3“ in the
code and simply change while(0) to while (1).
Change from:
//
// Exercise 3 : Triangle wave
//
while(1)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=5; uli<ulNumHarmonics; uli+=4)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
for (uli=3; uli<ulNumHarmonics; uli+=4)
{
fSine -= (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
break;
}
Change to:
//
// Exercise 3 : Triangle wave
//
while(0)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=5; uli<ulNumHarmonics; uli+=4)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
for (uli=3; uli<ulNumHarmonics; uli+=4)
{
fSine -= (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli * uli));
}
break;
}
2. Now, we will enable the code that generates a sawtooth wave. Locate “Exercise 4“ in the
code and simply change while(0) to while (1).
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 21
Change from:
//
// Exercise 4 : Saw tooth wave
//
while (0)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=2; uli<ulNumHarmonics; uli++)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli));
}
fSine = -fSine;
break;
}
Change to:
//
// Exercise 4 : Saw tooth wave
//
while (1)
{
fSine = sinf(fRadians) * 0.5F;
for (uli=2; uli<ulNumHarmonics; uli++)
{
fSine += (sinf((((float) (uli))) * fRadians) * 0.5F)/((float)
(uli));
}
fSine = -fSine;
break;
}
3. Rebuild the code, and download it to the microcontroller. You should see a sawtooth wave on
the screen.
4. Please find the location in the code where variable ulNumHarmonics is declared. This should
be located in the beginning of main().
int
main(void)
{
unsigned
unsigned
unsigned
unsigned
long
long
long
long
ulItemCount = 0;
ulLastTickCount = 0;
uli;
ulNumHarmonics=1;
5. Change the value of ulNumHarmonics, rebuild the code, download it to the microcontroller
and notice the change in the shape of the waveform.
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 22
Results
Upon successful completion of this exercise, the following should appear on the screen.
Congratulations, you have completed exercise 4 of this lab while using the floating point unit in
Stellaris M4F MCU!
Stellaris® ARM® CortexTM-M4F Training: Floating Point Unit – Section 3: Exercises
Page 23