-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMCP4725_wave_generator.ino
More file actions
187 lines (162 loc) · 3.72 KB
/
Copy pathMCP4725_wave_generator.ino
File metadata and controls
187 lines (162 loc) · 3.72 KB
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
//
// FILE: MCP4725_wave_generator.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo function generators
// URL: https://github.com/RobTillaart/MCP4725
// URL: https://github.com/RobTillaart/FunctionGenerator
//
// depending on the platform, the range of "smooth" sinus is limited.
// other signals are less difficult so have a slightly larger range.
//
// PLATFORM SINUS SQUARE SAWTOOTH TRIANGLE
// UNO -100 Hz
// ESP32 -200 Hz -1000 -250 -100
//
#include "MCP4725.h"
#include "Wire.h"
// frequency
// use + - * / to control it
uint16_t freq = 100;
uint32_t period = 0;
uint32_t halvePeriod = 0;
// q = square z = zero
// s = sinus m = mid
// w = sawtooth h = high
// t = stair
// r = random
char waveFrom = 'q';
MCP4725 MCP(0x63);
uint16_t count;
uint32_t lastTime = 0;
// LOOKUP TABLE SINE
uint16_t sine[361];
void setup()
{
while(!Serial);
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("MCP4725_LIB_VERSION: ");
Serial.println(MCP4725_LIB_VERSION);
Serial.println();
Wire.begin();
// Wire.setClock(3400000);
// fill table
for (int i = 0; i < 361; i++)
{
sine[i] = 2047 + round(2047 * sin(i * PI / 180));
}
MCP.begin();
Wire.setClock(800000);
MCP.setValue(0);
if (!MCP.isConnected())
{
Serial.println("err");
while (1);
}
period = 1e6 / freq;
halvePeriod = period / 2;
while (1)
{
yield();
uint32_t now = micros();
count++;
if (now - lastTime > 100000)
{
lastTime = now;
// show # updates per 0.1 second
// Serial.println(count);
count = 0;
if (Serial.available())
{
int c = Serial.read();
switch (c)
{
case '+':
freq++;
break;
case '-':
freq--;
break;
case '*':
freq *= 10;
break;
case '/':
freq /= 10;
break;
case '0' ... '9':
freq *= 10;
freq += (c - '0');
break;
case 'c':
freq = 0;
break;
case 'A':
break;
case 'a':
break;
case 'q':
case 's':
case 'w':
case 't':
case 'r':
case 'z':
case 'm':
case 'h':
waveFrom = c;
break;
default:
break;
}
period = 1e6 / freq;
halvePeriod = period / 2;
Serial.print(freq);
// Serial.print('\t');
// Serial.print(period);
// Serial.print('\t');
// Serial.print(halvePeriod);
Serial.println();
}
}
uint32_t t = now % period;
switch (waveFrom)
{
case 'q':
if (t < halvePeriod ) MCP.setValue(4095);
else MCP.setValue(0);
break;
case 'w':
MCP.setValue(t * 4095 / period );
break;
case 't':
if (t < halvePeriod) MCP.setValue(t * 4095 / halvePeriod);
else MCP.setValue( (period - t) * 4095 / halvePeriod );
break;
case 'r':
MCP.setValue(random(4096));
break;
case 'z': // zero
MCP.setValue(0);
break;
case 'h': // high
MCP.setValue(4095);
break;
case 'm': // mid
MCP.setValue(2047);
break;
default:
case 's':
// reference
// float f = ((PI * 2) * t)/period;
// MCP.setValue(2047 + 2047 * sin(f));
//
int idx = (360 * t) / period;
MCP.setValue(sine[idx]); // fetch from lookup table
break;
}
}
}
void loop()
{
}
// -- END OF FILE --