110 lines
2.8 KiB
C++
110 lines
2.8 KiB
C++
// can fibomod make an interesting noise?
|
|
|
|
|
|
#include <Adafruit_MCP4728.h>
|
|
#include <Wire.h>
|
|
|
|
Adafruit_MCP4728 mcp;
|
|
|
|
#define nsamp 1500
|
|
#define dacmax 256
|
|
|
|
unsigned int phase = 0;
|
|
unsigned int fibo = 3;
|
|
unsigned int scale = 0;
|
|
unsigned int fibi = 0;
|
|
unsigned int gate = 4095;
|
|
unsigned int pmax = nsamp;
|
|
unsigned int beat = 0;
|
|
|
|
unsigned int fibosort[] = { 4,3,11,8,29,7,21,19,38,76,5,55,199,6,9,12,16,18,24,36,48,72,144,13,22,31,44,62,124,47,141,17,34,68,136,152,15,33,41,123,165,205,58,116,211,89,139,14,23,28,32,42,46,56,63,69,84,92,96,112,126,138,161,168,184,207,224,252,101,151,233,39,87,59,10,20,40,61,88,110,122,155,220,244,248,71,27,51,54,57,102,107,108,114,153,171,204,214,216,228,37,113,79,158,35,77,105,231,26,52,104,232,43,129,181,209,64,94,188,192,25,53,106,109,212,218,121,49,91,147,203,229,30,45,60,66,80,82,90,93,99,120,132,164,176,180,183,186,198,240,246,131,178,67,201,65,143,145,119,133,189,238,73,149,202,111,235,78,83,117,156,166,174,208,234,249,118,236,179,85,95,170,187,190,191,128,97,75,103,142,81,159,162,74,148,226,177,239,70,115,140,154,160,210,217,230,241,253,251,221,247,127,86,172,137,195,213,219,50,100,200,237,157,163,242,98,167,182,196,173,135,255,169,185,193,197,175,134,130,215,146,223,222,227,125,245,194,150,225,206,243,254,250 };
|
|
|
|
|
|
byte waveform[nsamp];
|
|
|
|
void setup() {
|
|
|
|
cli();
|
|
|
|
//set timer1 interrupt at 1Hz
|
|
TCCR1A = 0;// set entire TCCR1A register to 0
|
|
TCCR1B = 0;// same for TCCR1B
|
|
TCNT1 = 0;//initialize counter value to 0
|
|
// set compare match register for 1hz increments
|
|
//OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
|
|
//OCR1A = 7812;// = (16*10^6) / (1*1024) - 1 (must be <65536)
|
|
OCR1A = 3200;
|
|
// turn on CTC mode
|
|
TCCR1B |= (1 << WGM12);
|
|
// Set CS10 and CS12 bits for 1024 prescaler
|
|
TCCR1B |= (1 << CS12) | (1 << CS10);
|
|
// enable timer compare interrupt
|
|
TIMSK1 |= (1 << OCIE1A);
|
|
|
|
sei();
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
if (!mcp.begin(0x64)) {
|
|
while (1) {
|
|
delay(100);
|
|
}
|
|
}
|
|
|
|
mcp.setSpeed(800000L);
|
|
|
|
for( int i = 0; i < nsamp; i++ ) {
|
|
waveform[i] = 1;
|
|
}
|
|
fibi = 3;
|
|
fibo = 3; //fibosort[fibi];
|
|
setwave();
|
|
}
|
|
|
|
void setwave(){
|
|
waveform[0] = 0;
|
|
waveform[1] = 1;
|
|
fibo = fibi; // fibosort[fibi];// + 3;
|
|
pmax = nsamp;
|
|
for (int i=2; i<nsamp; ++i){
|
|
waveform[i] = (waveform[i - 1] + waveform[i - 2]) % fibo;
|
|
if( waveform[i - 1] == 0 && waveform[i] == 1 ) {
|
|
pmax = i - 1;
|
|
break;
|
|
}
|
|
}
|
|
float scale = 256 / fibo;
|
|
for( int i = 0; i < nsamp; ++i ) {
|
|
waveform[i] *= scale;
|
|
}
|
|
}
|
|
|
|
ISR(TIMER1_COMPA_vect){//timer1
|
|
fibi++;
|
|
if( fibi == 67 ) {
|
|
fibi = 3;
|
|
}
|
|
|
|
setwave();
|
|
phase = 0;
|
|
beat++;
|
|
if ( beat == 4 ) {
|
|
gate = 4095;
|
|
beat = 0;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
mcp.fastWrite(waveform[phase] << 4, gate, 0, 0);
|
|
phase += 1;
|
|
if( phase == pmax ) {
|
|
phase = 0;
|
|
gate = 0;
|
|
}
|
|
}
|
|
|