-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttinyCZ1Synth.ino
334 lines (159 loc) · 9.26 KB
/
AttinyCZ1Synth.ino
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
// (*) All in the spirit of open-source and open-hardware
// Janost 2017 Sweden
// The 3HP Paperface Euro Modules
// https://janostman.wordpress.com/the-3hp-paperface-euro-modules/
// Copyright 2017 DSP Synthesizers Sweden.
//
// Author: Jan Ostman
//
// This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//Set Fuses to E1 DD FE for PLLCLK 16MHz
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
uint8_t phase;
uint8_t otone1;
uint8_t otone2;
volatile uint8_t level1;
volatile uint8_t PDmod;
volatile uint8_t level3;
volatile uint8_t level4;
volatile uint8_t level5;
uint8_t MUX;
volatile uint16_t pitch;
uint16_t phacc;
uint16_t pdacc;
uint8_t dac;
const uint8_t sinetable[256] PROGMEM = {
0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,124,
127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,200,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,252,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,155,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3,2,2,1,1,1,0,0,0
};
const uint16_t SAMPLEfreq[1024] PROGMEM ={
1024,1027,1030,1034,1037,1041,1045,1048,1052,1055,1059,1062,1066,1070,1073,1077,
1080,1084,1088,1092,1095,1099,1103,1106,1110,1114,1118,1121,1125,1129,1133,1137,
1141,1145,1148,1152,1156,1160,1164,1168,1172,1176,1180,1184,1188,1192,1196,1200,
1204,1208,1212,1216,1221,1225,1229,1233,1237,1241,1246,1250,1254,1258,1263,1267,
1271,1275,1280,1284,1288,1293,1297,1302,1306,1310,1315,1319,1324,1328,1333,1337,
1342,1346,1351,1356,1360,1365,1369,1374,1379,1383,1388,1393,1398,1402,1407,1412,
1417,1421,1426,1431,1436,1441,1446,1451,1456,1460,1465,1470,1475,1480,1485,1490,
1495,1501,1506,1511,1516,1521,1526,1531,1537,1542,1547,1552,1557,1563,1568,1573,
1579,1584,1589,1595,1600,1606,1611,1617,1622,1628,1633,1639,1644,1650,1655,1661,
1667,1672,1678,1684,1689,1695,1701,1707,1712,1718,1724,1730,1736,1742,1747,1753,
1759,1765,1771,1777,1783,1789,1795,1802,1808,1814,1820,1826,1832,1839,1845,1851,
1857,1864,1870,1876,1883,1889,1895,1902,1908,1915,1921,1928,1934,1941,1947,1954,
1961,1967,1974,1981,1987,1994,2001,2008,2014,2021,2028,2035,2042,2049,2056,2063,
2070,2077,2084,2091,2098,2105,2112,2119,2127,2134,2141,2148,2156,2163,2170,2178,
2185,2192,2200,2207,2215,2222,2230,2237,2245,2253,2260,2268,2276,2283,2291,2299,
2307,2314,2322,2330,2338,2346,2354,2362,2370,2378,2386,2394,2402,2410,2419,2427,
2435,2443,2452,2460,2468,2477,2485,2493,2502,2510,2519,2527,2536,2545,2553,2562,
2571,2579,2588,2597,2606,2614,2623,2632,2641,2650,2659,2668,2677,2686,2695,2704,
2714,2723,2732,2741,2751,2760,2769,2779,2788,2798,2807,2817,2826,2836,2845,2855,
2865,2874,2884,2894,2904,2914,2923,2933,2943,2953,2963,2973,2983,2993,3004,3014,
3024,3034,3045,3055,3065,3076,3086,3097,3107,3118,3128,3139,3149,3160,3171,3182,
3192,3203,3214,3225,3236,3247,3258,3269,3280,3291,3302,3313,3325,3336,3347,3359,
3370,3381,3393,3404,3416,3428,3439,3451,3463,3474,3486,3498,3510,3522,3534,3546,
3558,3570,3582,3594,3606,3618,3631,3643,3655,3668,3680,3693,3705,3718,3730,3743,
3756,3768,3781,3794,3807,3820,3833,3846,3859,3872,3885,3898,3911,3924,3938,3951,
3965,3978,3991,4005,4019,4032,4046,4060,4073,4087,4101,4115,4129,4143,4157,4171,
4185,4199,4214,4228,4242,4257,4271,4286,4300,4315,4329,4344,4359,4373,4388,4403,
4418,4433,4448,4463,4478,4494,4509,4524,4539,4555,4570,4586,4601,4617,4633,4648,
4664,4680,4696,4712,4728,4744,4760,4776,4792,4808,4825,4841,4857,4874,4890,4907,
4924,4940,4957,4974,4991,5008,5025,5042,5059,5076,5093,5110,5128,5145,5162,5180,
5198,5215,5233,5251,5268,5286,5304,5322,5340,5358,5376,5395,5413,5431,5450,5468,
5487,5505,5524,5543,5562,5580,5599,5618,5637,5656,5676,5695,5714,5734,5753,5773,
5792,5812,5831,5851,5871,5891,5911,5931,5951,5971,5992,6012,6032,6053,6073,6094,
6114,6135,6156,6177,6198,6219,6240,6261,6282,6304,6325,6346,6368,6390,6411,6433,
6455,6477,6499,6521,6543,6565,6587,6609,6632,6654,6677,6700,6722,6745,6768,6791,
6814,6837,6860,6883,6907,6930,6954,6977,7001,7025,7049,7072,7096,7120,7145,7169,
7193,7217,7242,7267,7291,7316,7341,7366,7391,7416,7441,7466,7491,7517,7542,7568,
7593,7619,7645,7671,7697,7723,7749,7775,7802,7828,7855,7881,7908,7935,7962,7989,
8016,8043,8070,8098,8125,8153,8180,8208,8236,8264,8292,8320,8348,8377,8405,8433,
8462,8491,8520,8548,8577,8606,8636,8665,8694,8724,8753,8783,8813,8843,8873,8903,
8933,8963,8994,9024,9055,9085,9116,9147,9178,9209,9240,9272,9303,9335,9366,9398,
9430,9462,9494,9526,9559,9591,9624,9656,9689,9722,9755,9788,9821,9854,9888,9921,
9955,9989,10022,10056,10090,10125,10159,10193,10228,10263,10298,10332,10367,10403,10438,10473,
10509,10544,10580,10616,10652,10688,10724,10761,10797,10834,10871,10907,10944,10981,11019,11056,
11094,11131,11169,11207,11245,11283,11321,11360,11398,11437,11475,11514,11553,11593,11632,11671,
11711,11751,11790,11830,11871,11911,11951,11992,12032,12073,12114,12155,12196,12238,12279,12321,
12363,12404,12447,12489,12531,12574,12616,12659,12702,12745,12788,12832,12875,12919,12962,13006,
13051,13095,13139,13184,13228,13273,13318,13363,13409,13454,13500,13546,13591,13638,13684,13730,
13777,13823,13870,13917,13965,14012,14059,14107,14155,14203,14251,14299,14348,14396,14445,14494,
14543,14593,14642,14692,14742,14792,14842,14892,14943,14993,15044,15095,15146,15198,15249,15301,
15353,15405,15457,15509,15562,15615,15668,15721,15774,15828,15881,15935,15989,16043,16098,16152,
16207,16262,16317,16372,16428,16484,16540,16596,16652,16708,16765,16822,16879,16936,16993,17051,
17109,17167,17225,17283,17342,17401,17460,17519,17578,17638,17698,17758,17818,17878,17939,18000,
18061,18122,18184,18245,18307,18369,18431,18494,18557,18620,18683,18746,18810,18873,18937,19002,
19066,19131,19195,19261,19326,19391,19457,19523,19589,19656,19722,19789,19856,19924,19991,20059,
20127,20195,20264,20332,20401,20470,20540,20609,20679,20749,20820,20890,20961,21032,21104,21175,
21247,21319,21391,21464,21537,21610,21683,21756,21830,21904,21978,22053,22128,22203,22278,22353,
22429,22505,22582,22658,22735,22812,22889,22967,23045,23123,23201,23280,23359,23438,23518,23597,
23677,23758,23838,23919,24000,24081,24163,24245,24327,24410,24492,24575,24659,24742,24826,24910,
24995,25080,25165,25250,25336,25421,25508,25594,25681,25768,25855,25943,26031,26119,26208,26297,
26386,26475,26565,26655,26745,26836,26927,27018,27110,27202,27294,27387,27479,27573,27666,27760,
27854,27948,28043,28138,28234,28329,28425,28522,28619,28716,28813,28911,29009,29107,29206,29305,
29404,29504,29604,29704,29805,29906,30007,30109,30211,30313,30416,30519,30623,30727,30831,30935,
31040,31145,31251,31357,31463,31570,31677,31784,31892,32000,32109,32218,32327,32436,32546,32657
};
void setup() {
// Enable 64 MHz PLL and use as source for Timer1
PLLCSR = 1<<PCKE | 1<<PLLE;
// Set up Timer/Counter1 for PWM output
TIMSK = 0; // Timer interrupts OFF
TCCR1 = 1<<PWM1A | 2<<COM1A0 | 1<<CS10; // PWM A, clear on match, 1:1 prescale
//GTCCR = 1<<PWM1B | 2<<COM1B0; // PWM B, clear on match
OCR1A = 128; //OCR1B = 128; // 50% duty at start
pinMode(1, OUTPUT); // Enable PWM output pin
pinMode(2, INPUT);
// Set up Timer/Counter0 for 66979Hz interrupt to output samples.
TCCR0A = 3<<WGM00; // Fast PWM
TCCR0B = 1<<WGM02 | 2<<CS00; // 1/8 prescale
TIMSK = 1<<OCIE0A; // Enable compare match, disable overflow
OCR0A = 29; // 66979Hz
uint16_t dummy=analogRead(0);
}
void loop() {
if (digitalRead(2)) level1=255; //Test for Gate VCA
if (!digitalRead(2)) level1=0;
//————— ADC block ————————————-
if (!(ADCSRA & 64)) {
uint16_t level=((ADCL+(ADCH<<8)));
if (MUX==3) pitch=(pgm_read_word_near( SAMPLEfreq + level));
if (MUX==2) PDmod=(level>>3)+1;
MUX++;
MUX&=3;
ADMUX = MUX; //Select MUX
sbi(ADCSRA, ADSC); //start next conversation
}
//—————————————————————
}
ISR(TIMER0_COMPA_vect) {
phacc+=pitch;
if (phacc & 0x8000) {
phacc &= 0x7FFF;
otone1+=1;
uint8_t env=(255-otone1);
pdacc+=PDmod;
if (!otone1) pdacc=0;
otone2=(pdacc>>3)&255;
dac=((pgm_read_byte(&sinetable[otone2])+pgm_read_byte(&sinetable[(otone2+(127-env))&255]))>>1);
OCR1A = dac&level1; //Gate the signal
}
//—————————————————————–
}