Fiddling with the old fibomod algorithm

main
Mike Lynch 2024-02-04 15:51:57 +11:00
parent 6efd542027
commit 7aec076356
1 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,88 @@
// can fibomod make an interesting noise?
#include <Adafruit_MCP4728.h>
#include <Wire.h>
Adafruit_MCP4728 mcp;
#define nsamp 256
#define dacmax 256
unsigned int phase = 0;
unsigned int fibo = 20;
unsigned int scale = 0;
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 = 10000;
// 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;
}
}
ISR(TIMER1_COMPA_vect){//timer1
unsigned int flog;
scale = 10;
fibo = 3 + analogRead(A0) >> 2;
flog = fibo;
while( flog ) {
flog = flog >> 1;
scale -= 1;
}
Serial.println(fibo);
Serial.println(scale);
Serial.println(fibo << scale);
}
void loop() {
int p0 = phase - 1;
int p1 = phase + 2;
if( p0 < 0 ) {
p0 += nsamp;
}
if( p1 < 0 ) {
p1 += nsamp;
}
waveform[phase] = (waveform[p1] + waveform[p0]) % fibo;
mcp.fastWrite(waveform[phase] << scale, 0, 0, 0);
phase += 1;
if( phase >= nsamp ) {
phase = 0;
}
}