I am frustrated!

main
Mike Lynch 2024-01-03 11:50:22 +11:00
parent cdeb974109
commit 5c8e20e17d
1 changed files with 38 additions and 18 deletions

View File

@ -10,21 +10,39 @@ Adafruit_MCP4728 mcp;
#define nsamp 256 #define nsamp 256
#define dacmax 256 #define dacmax 256
const byte nclk=42; //number of clock cycles of the loop const byte nclk = 200; // a guess
long int freq; //frequency in mHz long int freq; //frequency in Hz
long unsigned int phase; long unsigned int phase;
long unsigned int phase_inc; long unsigned int phase_inc;
void setup() { void setup() {
//TIMSK0 &= ~_BV(TOIE0); // disable timer0 overflow interrupt
// // Set a timer interrupt for the phase accumulation
//Wire.setClock(1400000L); // wooooo cli();//stop interrupts
//set timer1 interrupt at 44Hz
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 = 500; // = (16 * 10**6) / (44000) - 1
// 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();//allow interrupts
Serial.begin(115200); Serial.begin(115200);
while (!Serial) while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens delay(10); // will pause Zero, Leonardo, etc until serial console opens
// Try to initialize! // Try to initialize!
if (!mcp.begin(0x64)) { if (!mcp.begin(0x64)) {
Serial.println("Failed to find MCP4728 chip"); Serial.println("Failed to find MCP4728 chip");
while (1) { while (1) {
@ -34,11 +52,11 @@ void setup() {
Serial.println("Adafruit MCP4728 initialised"); Serial.println("Adafruit MCP4728 initialised");
} }
Wire.setClock(3400000L);
freq=660; freq=6600000;
phase=0; phase=0;
calc_phase_inc(); calc_phase_inc();
//phase_inc = 1;
setwave(); setwave();
} }
@ -52,10 +70,10 @@ void setwave(){
int val=0; int val=0;
//saw //saw
val = dacmax * isamp / nsamp; //val = dacmax * isamp / nsamp;
//val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1; //val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1;
//sine //sine
//val=(sin(phi)+1.0)*dacmax/2; val=(sin(phi)+1.0)*dacmax/2;
//val=((sin(phi)+0.333*sin(3*phi))/0.943+1)*dacmax/2; //val=((sin(phi)+0.333*sin(3*phi))/0.943+1)*dacmax/2;
val=max(val,0); val=max(val,0);
@ -65,19 +83,21 @@ void setwave(){
} }
//calculate the phase increment. 2^32/16E6=268.435456 //calculate the phase increment.
void calc_phase_inc(){ void calc_phase_inc(){
phase_inc=0.268435456*nclk*freq; phase_inc=0.268435456*nclk*freq;
Serial.println("phase inc");
Serial.println(phase_inc);
} }
//regular running: generate waves
ISR(TIMER1_COMPA_vect){// timer1 interrupt
phase += phase_inc;
}
//regular running:
void loop() { void loop() {
//phase+=900; int redphase = phase >> 24;
//int redphase = phase >> 24; mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
phase += analogRead(A0); //Serial.println(redphase);
int redphase = phase % nsamp;
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
} }