-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionGenerator.c
334 lines (312 loc) · 10.1 KB
/
FunctionGenerator.c
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
333
334
/* ========================================
*
* Copyright 2016 John Harkins and Li He
* All Rights Reserved
*
* ========================================
*/
#include <math.h>
#include "FunctionGenerator.h"
extern void WaveDAC_0_Clock_SetDividerRegister(uint16 clkDivider, uint8 restart);
extern void WaveDAC_1_Clock_SetDividerRegister(uint16 clkDivider, uint8 restart);
extern void WaveDAC_0_Stop();
extern void WaveDAC_1_Stop();
extern void WaveDAC_0_StartEx(const uint8 * wavePtr1, uint16 sampleSize1,
const uint8 * wavePtr2, uint16 sampleSize2);
extern void WaveDAC_1_StartEx(const uint8 * wavePtr1, uint16 sampleSize1,
const uint8 * wavePtr2, uint16 sampleSize2);
uint8 waveform0[MAX_NUM_SAMPLES];
uint8 waveform1[MAX_NUM_SAMPLES];
/*******************************************************************************
* Function Name: StopWaveDac
********************************************************************************
*
* Summary
* Turns off the waveform for the specified function generator
*
* Parameters:
* fnGenerator - the index of the function generator to be stopped
*
* Return:
* None.
*
*******************************************************************************/
void StopWaveDac(int fnGenerator)
{
if (fnGenerator == 0) {
WaveDAC_0_Stop();
} else if (fnGenerator == 1) {
WaveDAC_1_Stop();
}
}
/*******************************************************************************
* Function Name: StartWaveDac
********************************************************************************
*
* Summary
* Starts waveform generation on a specified WaveDAC
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* fnGenerator - the index of the function generator to be started
* waveform - the raw array of waveform samples
*
* Return:
* None.
*
*******************************************************************************/
void StartWaveDac(int numSamples, int fnGenerator, uint8 *waveform)
{
switch (fnGenerator) {
case WAVEDAC_LOW_0:
WaveDAC_0_Clock_SetDividerRegister(LOW_DIVIDER_REGISTER, 1);
WaveDAC_0_StartEx(waveform, numSamples, NULL, 0);
break;
case WAVEDAC_HIGH_0:
WaveDAC_0_Clock_SetDividerRegister(HIGH_DIVIDER_REGISTER, 1);
WaveDAC_0_StartEx(waveform, numSamples, NULL, 0);
break;
case WAVEDAC_LOW_1:
WaveDAC_1_Clock_SetDividerRegister(LOW_DIVIDER_REGISTER, 1);
WaveDAC_1_StartEx(waveform, numSamples, NULL, 0);
break;
case WAVEDAC_HIGH_1:
WaveDAC_1_Clock_SetDividerRegister(HIGH_DIVIDER_REGISTER, 1);
WaveDAC_1_StartEx(waveform, numSamples, NULL, 0);
break;
}
}
/*******************************************************************************
* Function Name: SetSquare
********************************************************************************
*
* Summary
* Sets currentWaveform as a square wave with 50% duty cycle
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* currentWaveform - the raw array of waveform samples to be filled
*
* Return:
* None.
*
*******************************************************************************/
void SetSquare(int numSamples, uint8 voltage, uint8 *currentWaveform)
{
int i;
for (i = 0; i < numSamples; i++) {
if (i < numSamples / 2) {
currentWaveform[i] = voltage;
}
else {
currentWaveform[i] = 0;
}
}
}
/*******************************************************************************
* Function Name: SetSine
********************************************************************************
*
* Summary
* Sets currentWaveform as a sine wave
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* currentWaveform - the raw array of waveform samples to be filled
*
* Return:
* None.
*
*******************************************************************************/
void SetSine(int numSamples, uint8 voltage, uint8 *currentWaveform)
{
int i;
for (i = 0; i < numSamples; i++) {
currentWaveform[i] = voltage / 2 + (voltage / 2 - 1) *
sin((2 * PI * i) / numSamples);
}
}
/*******************************************************************************
* Function Name: SetTriangle
********************************************************************************
*
* Summary
* Sets currentWaveform as a triangle wave
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* currentWaveform - the raw array of waveform samples to be filled
*
* Return:
* None.
*
*******************************************************************************/
void SetTriangle(int numSamples, uint8 voltage, uint8 *currentWaveform)
{
int i;
for (i = 0; i < numSamples; i++) {
if (i < numSamples / 2) {
currentWaveform[i] = voltage * (1.0 * i / (numSamples / 2.0));
} else {
currentWaveform[i] = voltage * (1.0 * (numSamples - i) / (numSamples / 2.0));
}
}
}
/*******************************************************************************
* Function Name: SetSawtooth
********************************************************************************
*
* Summary
* Sets currentWaveform as a sawtooth wave
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* currentWaveform - the raw array of waveform samples to be filled
*
* Return:
* None.
*
*******************************************************************************/
void SetSawtooth(int numSamples, uint8 voltage, uint8 *currentWaveform)
{
int i;
for (i = 0; i < numSamples; i++) {
currentWaveform[i] = voltage * (1.0 * i / numSamples);
}
}
/*******************************************************************************
* Function Name: SetSine
********************************************************************************
*
* Summary
* Sets currentWaveform as a DC signal (no variation)
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* currentWaveform - the raw array of waveform samples to be filled
*
* Return:
* None.
*
*******************************************************************************/
void SetDC(int numSamples, uint8 voltage, uint8 *currentWaveform)
{
int i;
for (i = 0; i < numSamples; i++) {
currentWaveform[i] = voltage;
}
}
/*******************************************************************************
* Function Name: SetWaveform
********************************************************************************
*
* Summary
* Sets currentWaveform as a wave with the specified shape (mode)
*
* Parameters:
* numSamples - the number of samples (number of elements in waveform)
* mode - the waveform type (square, sine, triangle, sawtooth, or DC)
* currentWaveform - the raw array of waveform samples to be filled
*
* Return:
* None.
*
*******************************************************************************/
void SetWaveform(int numSamples, int mode, uint8 voltage, uint8 *currentWaveform)
{
switch (mode) {
case SQUARE:
SetSquare(numSamples, voltage, currentWaveform);
break;
case SINE:
SetSine(numSamples, voltage, currentWaveform);
break;
case TRIANGLE:
SetTriangle(numSamples, voltage, currentWaveform);
break;
case SAWTOOTH:
SetSawtooth(numSamples, voltage, currentWaveform);
break;
case DC:
SetDC(numSamples, voltage, currentWaveform);
break;
}
}
/*******************************************************************************
* Function Name: GetCurrentWaveform
********************************************************************************
*
* Summary
* Retrieves the waveform currently loaded into fnGenerator
*
* Parameters:
* fnGenerator - the function generator to be queried
*
* Return:
* an array of uint8 containing the raw samples
*
*******************************************************************************/
uint8 *GetCurrentWaveform(int fnGenerator)
{
switch (fnGenerator) {
case WAVEDAC_LOW_0:
case WAVEDAC_HIGH_0:
return waveform0;
case WAVEDAC_LOW_1:
case WAVEDAC_HIGH_1:
return waveform1;
default:
return NULL;
}
}
/*******************************************************************************
* Function Name: ChangeFrequency
********************************************************************************
*
* Summary
* Stops the selected WaveDAC, modifies its array of samples according to the
* desired frequency, and starts the DAC again.
*
* Parameters:
* freq - the desired frequency
*
* Return:
* The new frequency, or -1 if the desired frequency was invalid
*
*******************************************************************************/
int ChangeFrequency(int freq, int mode, uint8 voltage, int waveDacId)
{
int fnGenerator, numSamples;
uint8 *currentWaveform;
uint8 voltage255;
if (freq < FREQ_LOWER_LIMIT) {
freq = FREQ_LOWER_LIMIT;
} else if (freq > FREQ_UPPER_LIMIT) {
freq = FREQ_UPPER_LIMIT;
}
if (freq < 2000) {
if (waveDacId == 0) {
fnGenerator = WAVEDAC_LOW_0;
} else if (waveDacId == 1) {
fnGenerator = WAVEDAC_LOW_1;
}
numSamples = WAVEDAC_CLOCK_LOW_FREQ / freq;
} else {
if (waveDacId == 0) {
fnGenerator = WAVEDAC_HIGH_0;
} else if (waveDacId == 1) {
fnGenerator = WAVEDAC_HIGH_1;
}
numSamples = WAVEDAC_CLOCK_HIGH_FREQ / freq;
}
voltage255 = voltage * (255.0 / 100.0);
currentWaveform = GetCurrentWaveform(fnGenerator);
if (currentWaveform != NULL) {
StopWaveDac(waveDacId);
SetWaveform(numSamples, mode, voltage255, currentWaveform);
StartWaveDac(numSamples, fnGenerator, currentWaveform);
return freq;
}
return -1;
}
/* [] END OF FILE */