1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
//*****************************************************************************
//
// sine_demo.c - Computes a sine wave and displays it on the screen.
//
// Copyright (c) 2011-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the EK-LM4F232 Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "drivers/cfal96x64x16.h"
#include "drivers/stripchartwidget.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>Sine Demo (sine_demo)</h1>
//!
//! This example uses the floating point capabilities of the Tiva C Series
//! processor to compute a sine wave and show it on the display.
//
//*****************************************************************************
//*****************************************************************************
//
// Provide a definition for M_PI, if it was not provided by math.h.
//
//*****************************************************************************
#ifndef M_PI
#define M_PI 3.14159265358979323846F
#endif
//*****************************************************************************
//
// The number of SysTick ticks per second.
//
//*****************************************************************************
#define TICKS_PER_SECOND 20
#define FSECONDS_PER_TICK (1.0F / (float)TICKS_PER_SECOND)
//*****************************************************************************
//
// A counter for system clock ticks, used for tracking time.
//
//*****************************************************************************
static volatile uint32_t g_ui32TickCount;
//*****************************************************************************
//
// Define an off-screen buffer and display structure. This is used by the
// strip chart widget for drawing a scrolling display.
//
//*****************************************************************************
#define OFFSCREEN_BUF_SIZE GrOffScreen4BPPSize(96, 64)
uint8_t g_pui8OffscreenBuf[OFFSCREEN_BUF_SIZE];
tDisplay g_sOffscreenDisplay;
//*****************************************************************************
//
// Create a palette for the off-screen buffer that is used by the strip chart.
//
//*****************************************************************************
uint32_t g_pui32Palette[] =
{
ClrBlack,
ClrWhite,
ClrRed,
ClrDarkGreen,
};
#define NUM_PALETTE_ENTRIES (sizeof(g_pui32Palette) / sizeof(uint32_t))
//*****************************************************************************
//
// Define the series for the strip chart. The SERIES_LENGTH is the maximum
// number of points that will be shown on the chart.
//
//*****************************************************************************
#define SERIES_LENGTH 96
static tStripChartSeries g_sSeries =
{
0, "SINE", ClrRed, 1, 1, 0, 0
};
//*****************************************************************************
//
// Defines the X-axis for the strip chart
//
//*****************************************************************************
static tStripChartAxis i16Axis16X =
{
"TIME", // title of axis
0, // label for minimum of axis
0, // label for maximum of axis
0, // minimum value for the axis
95, // maximum value for the axis
TICKS_PER_SECOND // grid interval for the axis
};
//*****************************************************************************
//
// Defines the Y-axis for the strip chart.
//
//*****************************************************************************
static tStripChartAxis i16Axis16Y =
{
"SIN(2pi*t/4)*0.5", // title of the axis
"-1", // label for minimum of axis
"+1", // label for maximum of axis
-32, // minimum value for the axis
31, // maximum value for the axis
16 // grid interval for the axis
};
//*****************************************************************************
//
// Defines the strip chart widget. This structure requires additional
// run-time initialization.
//
//*****************************************************************************
StripChart(g_sStripChart, WIDGET_ROOT, 0, 0, &g_sCFAL96x64x16, 0, 0, 96, 64,
0, g_psFontFixed6x8, ClrBlack, ClrWhite, ClrWhite, ClrDarkGreen,
&i16Axis16X, &i16Axis16Y, &g_sOffscreenDisplay);
//*****************************************************************************
//
// Creates a buffer for holding the values of the data series. It must be
// large enough to hold the maximum number of data points in the series that
// will be shown on the strip chart.
//
//*****************************************************************************
static int8_t g_i8SeriesData[SERIES_LENGTH];
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// This is the handler for this SysTick interrupt. It simply increments a
// counter that is used for timing.
//
//*****************************************************************************
void
SysTickHandler(void)
{
//
// Update our tick counter.
//
g_ui32TickCount++;
}
//*****************************************************************************
//
// Compute and display a sine wave.
//
//*****************************************************************************
int
main(void)
{
uint_fast16_t ui16ItemCount = 0;
uint32_t ui32LastTickCount = 0;
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPULazyStackingEnable();
//
// Set the clocking to run directly at 50 MHz.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN);
//
// Configure SysTick to generate a periodic time tick interrupt.
//
ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / TICKS_PER_SECOND);
ROM_SysTickEnable();
ROM_SysTickIntEnable();
//
// Initialize the display driver.
//
CFAL96x64x16Init();
//
// Initialize an offscreen display and assign the palette. This offscreen
// buffer is needed by the strip chart widget.
//
GrOffScreen4BPPInit(&g_sOffscreenDisplay, g_pui8OffscreenBuf, 96, 64);
GrOffScreen4BPPPaletteSet(&g_sOffscreenDisplay, g_pui32Palette, 0,
NUM_PALETTE_ENTRIES);
//
// Set the data series buffer pointer to point at the storage where the
// series data points will be stored.
//
g_sSeries.pvData = g_i8SeriesData;
//
// Add the series to the strip chart
//
StripChartSeriesAdd(&g_sStripChart, &g_sSeries);
//
// Add the strip chart to the widget tree.
//
WidgetAdd(WIDGET_ROOT, &g_sStripChart.sBase);
//
// Enter a loop to continuously calculate a sine wave.
//
while(1)
{
float fElapsedTime;
float fRadians;
float fSine;
//
// Wait for the next timer tick.
//
while(ui32LastTickCount == g_ui32TickCount)
{
}
ui32LastTickCount = g_ui32TickCount;
//
// Preparing to add a new data point to the strip chart ...
// If the number count of items in the strip chart has reached the
// maximum value, then the data points need to "slide down" in the
// buffer so new data can be added at the end.
//
if(ui16ItemCount == SERIES_LENGTH)
{
memmove(&g_i8SeriesData[0], &g_i8SeriesData[1], SERIES_LENGTH - 1);
}
//
// Otherwise, the series data buffer is less than full so just
// increment the count of data points.
//
else
{
//
// Increment the number of items that have been added to the strip
// chart series data buffer.
//
ui16ItemCount++;
//
// Since the count of data items has changed, it must be updated in
// the data series.
//
g_sSeries.ui16NumItems = ui16ItemCount;
}
//
// Compute the elapsed time in decimal seconds, in floating point
// format.
//
fElapsedTime = (float)g_ui32TickCount * FSECONDS_PER_TICK;
//
// Convert the time to radians.
//
fRadians = fElapsedTime * 2.0 * (float)M_PI;
//
// Adjust the period of the wave. This will give us a wave period
// of 4 seconds, or 0.25 Hz. This number was chosen arbitrarily to
// provide a nice looking wave on the display.
//
fRadians /= 4.0;
//
// Compute the sine. Multiply by 0.5 to reduce the amplitude.
//
fSine = sinf(fRadians) * 0.5;
//
// Finally, save the sine value into the last location in the series
// data point buffer. Convert the sine amplitude to display pixels.
// (Amplitude 1 = 32 pixels)
//
g_i8SeriesData[ui16ItemCount - 1] = (int8_t)(fSine * 32.0);
//
// Now that a new data point has been added to the series, advance
// the strip chart.
//
StripChartAdvance(&g_sStripChart, 1);
//
// Request a repaint and run the widget processing queue.
//
WidgetPaint(WIDGET_ROOT);
WidgetMessageQueueProcess();
}
}
|