-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfunctionGenerator_MCP4725.ino
More file actions
157 lines (138 loc) · 2.84 KB
/
functionGenerator_MCP4725.ino
File metadata and controls
157 lines (138 loc) · 2.84 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
//
// FILE: functionGenerator_MCP4725.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo function generators
// DATE: 2021-01-06
// 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.
// see readme.md for mac frequency table.
//
// uses an MCP4725 as output device
#include "functionGenerator.h"
#include "MCP4725.h"
#include "Wire.h"
funcgen gen;
float value = 0;
float frequency = 40;
float amplitude = 1.0;
float d = 0;
// q = square
// s = sinus
// w = sawtooth
// t = stair
// r = random
char mode = 's';
MCP4725 MCP(0x63);
uint32_t count;
uint32_t lastTime = 0;
void setup()
{
Serial.begin(230400);
Serial.println();
Serial.println(__FILE__);
Serial.println("FUNCTIONGENERATOR_LIB_VERSION: ");
Serial.println(FUNCTIONGENERATOR_LIB_VERSION);
Serial.println();
gen.setAmplitude(1);
gen.setYShift(1);
gen.setFrequency(frequency);
value = 0;
Wire.begin();
MCP.begin();
Wire.setClock(100000);
MCP.setValue(0);
if (!MCP.isConnected())
{
Serial.println("err");
while (1);
}
while (1)
{
count++;
float t = micros() * 0.000001;
if (millis() - lastTime > 1000)
{
Serial.println(count);
count = 0;
lastTime = millis();
}
if (Serial.available())
{
int c = Serial.read();
switch (c)
{
case '+':
frequency += 0.01;
break;
case '-':
frequency -= 0.01;
break;
case '*':
frequency *= 10;
break;
case '/':
frequency /= 10;
break;
case '0' ... '9':
frequency *= 10;
frequency += (c - '0');
break;
case 'c':
frequency = 0;
break;
case 'A':
break;
case 'a':
break;
case 'D':
d = gen.getDutyCycle();
d++;
gen.setDutyCycle(d);
break;
case 'd':
d = gen.getDutyCycle();
d--;
gen.setDutyCycle(d);
break;
break;
case 'q':
case 's':
case 'w':
case 't':
case 'r':
mode = c;
break;
default:
break;
}
gen.setFrequency(frequency);
Serial.println(frequency);
}
switch (mode)
{
case 'q':
value = 2047 * gen.square(t);
break;
case 'w':
value = 2047 * gen.sawtooth(t);
break;
case 't':
value = 2047 * gen.triangle(t);
break;
case 'r':
value = 2047 * gen.random();
break;
default:
case 's':
value = 2047 * gen.sinus(t);
break;
}
MCP.setValue(value);
}
}
void loop()
{
}
// -- END OF FILE --