-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_generator.c
200 lines (155 loc) · 5.52 KB
/
table_generator.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
// this file is to generate the lookup tables for P1 of CPE 316
// as of 20231022 5:28p
typedef enum WAVE_TYPE {
SINE = 1,
RAMP = 2,
TRIANGLE = 3
} WAVE_TYPE;
#define TABLE_SIZE 600
#define VPP 3.0
#define DC_BIAS 1.5
#define PI 3.141592653589793
#define Y 0.1 // y calibration
#define MAX 2.93 // max
#define VREF 3.3 // DAC Vref
void print_table(double* table); // prints all values of the table, output looks like an array, for copy+pasting
void print_table_mv(double* table); // prints all values of the table, output looks like an array, for copy+pasting
void print_mv_table(uint16_t* table); // prints a table_size table of uint16_t values
void print_DAC_table(uint16_t* table); // prints a table_size talbe of uint16_t values in hex
void make_sin_table(double* values); // prints table_size number of points to output in the format of an array
void make_ramp_table(double* values); // prints table_size number of points to output in the format of an array
void make_tri_table(double* values); // prints table_size number of points to output in the format of an array
void make_mv_table(double* values, uint16_t* mv); // turns the elements of values into ints and puts them in mv
void make_DAC_table(double* values, uint16_t* table); // makes a table of 16 bit DAC values to input to the DAC
int main()
{
WAVE_TYPE wave = RAMP; /* ---------------- CHANGE THIS TO THE WAVEFORM YOU WANT THE TABLE FROM ---------------- */
double table[TABLE_SIZE];
uint16_t mv_table[TABLE_SIZE];
uint16_t DAC_table[TABLE_SIZE];
switch(wave)
{
case SINE: make_sin_table(table); break;
case RAMP: make_ramp_table(table); break;
case TRIANGLE: make_tri_table(table); break;
default: fprintf(stdout, "idk which wave you want.\n");
}
//print_table(table);
//print_table_mv(table); // doesn't change any tables, just prints
make_mv_table(table, mv_table);
//print_mv_table(mv_table);
make_DAC_table(table, DAC_table);
print_DAC_table(DAC_table);
return 0;
}
// makes a table of 16 bit DAC values to input to the DAC
void make_DAC_table(double* values, uint16_t* table)
{
int i;
for(i = 0; i < TABLE_SIZE; i++)
{
table[i] = ( 0x3000 | ( 0xFFF & (uint16_t)(values[i] / VREF * 0xFFF) ) );
}
}
// turns the elements of values into ints and puts them in mv
void make_mv_table(double* values, uint16_t* mv)
{
int i;
for(i = 0; i < TABLE_SIZE; i++)
{
mv[i] = (uint16_t)(values[i] * 1000);
}
}
// prints table_size number of points to output in the format of an array
void make_tri_table(double* values)
{
int i;
for(i = 0; i < TABLE_SIZE / 2; i ++)
{
values[i] = (double)VPP * i * 2 / TABLE_SIZE;
values[TABLE_SIZE - i - 1] = values[i];
}
}
// prints table_size number of points to output in the format of an array
void make_ramp_table(double* values)
{
int i;
for(i = 0; i < TABLE_SIZE; i++)
{
values[i] = (double)VPP * i / TABLE_SIZE;
}
}
// prints table_size number of points to output in the format of an array
void make_sin_table(double values[])
{
double a = VPP / 2; // amplitude of the wave
int i;
for(i = 0; i < TABLE_SIZE; i++)
{
values[i] = a * sin((double)(2 * PI * i / TABLE_SIZE)) + DC_BIAS;
}
}
// prints all values of the table, output looks like an array, for copy+pasting
void print_table(double* table)
{
int i;
fprintf(stdout, "[ ");
for(i = 0; i < TABLE_SIZE - 1; i++)
{
fprintf(stdout, "%.4f, ", table[i]); // prints the value with 4 decimal places
}
fprintf(stdout, "%.4f ]\n", table[TABLE_SIZE - 1]);
fprintf(stdout, "i = 0: %.4f\n", table[0]);
fprintf(stdout, "i = 1: %.4f\n", table[1]);
fprintf(stdout, "i = 150: %.4f\n", table[150]);
fprintf(stdout, "i = 299: %.4f\n", table[299]);
fprintf(stdout, "i = 300: %.4f\n", table[300]);
fprintf(stdout, "i = 301: %.4f\n", table[301]);
fprintf(stdout, "i = 450: %.4f\n", table[450]);
fprintf(stdout, "i = 599: %.4f\n", table[599]);
}
// prints all values of the table, output looks like an array, for copy+pasting
void print_table_mv(double* table)
{
int i;
fprintf(stdout, "[ ");
for(i = 0; i < TABLE_SIZE - 1; i++)
{
fprintf(stdout, "%d, ", (int)(table[i] * 1000)); // prints the value in millivolts
}
fprintf(stdout, "%d ]\n", (int)(table[TABLE_SIZE - 1] * 1000));
fprintf(stdout, "i = 0: %d\n", (int)(table[0] * 1000));
fprintf(stdout, "i = 1: %d\n", (int)(table[1] * 1000));
fprintf(stdout, "i = 150: %d\n", (int)(table[150] * 1000));
fprintf(stdout, "i = 299: %d\n", (int)(table[299] * 1000));
fprintf(stdout, "i = 300: %d\n", (int)(table[300] * 1000));
fprintf(stdout, "i = 301: %d\n", (int)(table[301] * 1000));
fprintf(stdout, "i = 450: %d\n", (int)(table[450] * 1000));
fprintf(stdout, "i = 599: %d\n", (int)(table[599] * 1000));
}
// prints a table_size table of uint16_t values
void print_mv_table(uint16_t* table)
{
int i;
fprintf(stdout, "[ ");
for(i = 0; i < TABLE_SIZE - 1; i++)
{
fprintf(stdout, "%d, ", table[i]);
}
fprintf(stdout, "%d ]\n", table[TABLE_SIZE - 1]);
}
// prints a table_size talbe of uint16_t values in hex
void print_DAC_table(uint16_t* table)
{
int i;
fprintf(stdout, "[ ");
for(i = 0; i < TABLE_SIZE - 1; i++)
{
fprintf(stdout, "0x%x, ", table[i]);
}
fprintf(stdout, "0x%x ]\n", table[TABLE_SIZE - 1]);
}