//waveform generator // hacked from https://www.instructables.com/Arduino-Waveform-Generator-1/ #include #include Adafruit_MCP4728 mcp; #define nsamp 32 #define dacmax 256 const byte nclk = 200; // a guess long int freq; //frequency in Hz long unsigned int phase; long unsigned int phase_inc; void setup() { TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt // 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) // // 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(400000L); // mcp.setSpeed(800000L); mcp.setSpeed(800000L); freq=440; phase=0; calc_phase_inc(); setwave(); } const float pi=3.14159265; byte waveform[nsamp]; byte phaseb = 0; void setwave(){ for (int isamp=0; isamp> 27; freq = 55 + 1760.0 * analogRead(A0) / 1024.0; calc_phase_inc(); mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0); }