Thanks Tom for taking the time to answer! :)
I discovered I can do
Wire.setClock(1400000L) to get the I2C protocol to the maximum speed that the MCP4728 supports, 1.4Mhz.
After doing that, with
#define EXTERNAL_AUDIO_OUTPUT false, I was able to generate a clean looking wave!! I guess the value was updated very slowly, that's why it looked like sample and hold before. For some reason the osc.setFreq() does not generate the correct frequency, but I guess it could be offset in software. Tried doing an ascending pitch and it does sounds and looks fine at any frecuency (of course before alliasing comes to play)
"
or #define EXTERNAL_AUDIO_OUTPUT true, you talk about shifting the output but you do not actually do it, something like uint16_t out = (f.l()>>4) + 1024; should work better?"
It still hangs, even if I just use out=0 to test.
Well I think it works fine for what I was going to use it, a dubsiren!
--------------------------------------HERE IS THE CODE THAT WORKS! (with incorrect pitch anyways)--------------------------
------also use #define EXTERNAL_AUDIO_OUTPUT false ------------------------
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/saw8192_int8.h> // sine table for oscillator
#include <Wire.h>
#include "MCP4728.h"
MCP4728 dac;
// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <SAW8192_NUM_CELLS, AUDIO_RATE> aSin(SAW8192_DATA);
// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 256 // Hz, powers of 2 are most reliable
void setup(){
startMozzi(CONTROL_RATE); // :)
//aSin.setFreq(2000); // set the frequency
Serial.begin(115200); // initialize serial interface for print()
Wire.setSDA(0);
Wire.setSCL(1);
Wire.begin();
Wire.setClock(1400000L); //maximo
dac.attach(Wire, 14);
dac.readRegisters();
dac.selectVref(MCP4728::VREF::VDD, MCP4728::VREF::VDD, MCP4728::VREF::INTERNAL_2_8V, MCP4728::VREF::VDD);
dac.selectPowerDown(MCP4728::PWR_DOWN::GND_100KOHM, MCP4728::PWR_DOWN::GND_100KOHM, MCP4728::PWR_DOWN::GND_500KOHM, MCP4728::PWR_DOWN::GND_500KOHM);
dac.selectGain(MCP4728::GAIN::X1, MCP4728::GAIN::X1, MCP4728::GAIN::X2, MCP4728::GAIN::X2);
dac.analogWrite(MCP4728::DAC_CH::A, 111);
dac.analogWrite(MCP4728::DAC_CH::B, 222);
dac.analogWrite(MCP4728::DAC_CH::C, 333);
dac.analogWrite(MCP4728::DAC_CH::D, 3000);
dac.enable(true);
dac.readRegisters();
//printStatus();
pinMode(28,OUTPUT);
}
void updateControl(){
static int counter = 0;
counter=counter +1;
Serial.println(counter);
aSin.setFreq(1000+counter);
dac.analogWrite(MCP4728::DAC_CH::A, random(0,4092));
dac.analogWrite(MCP4728::DAC_CH::C, counter);
if (counter > 4092) counter = 0;
}
AudioOutput_t updateAudio(){
int out= aSin.next()+1024;
return dac.analogWrite(MCP4728::DAC_CH::B, out);
//return dac.analogWrite(2046, out, 0, 0);
//return MonoOutput::from8Bit(aSin.next()); // return an int signal centred around 0
}
void loop(){
audioHook(); // required here
}