2024-01-02 23:58:06 +00:00
|
|
|
//waveform generator
|
|
|
|
|
|
|
|
// hacked from https://www.instructables.com/Arduino-Waveform-Generator-1/
|
|
|
|
|
|
|
|
#include <Adafruit_MCP4728.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
|
|
|
|
Adafruit_MCP4728 mcp;
|
|
|
|
|
|
|
|
#define nsamp 256
|
|
|
|
#define dacmax 256
|
|
|
|
|
2024-01-03 00:50:22 +00:00
|
|
|
const byte nclk = 200; // a guess
|
|
|
|
long int freq; //frequency in Hz
|
2024-01-02 23:58:06 +00:00
|
|
|
long unsigned int phase;
|
|
|
|
long unsigned int phase_inc;
|
|
|
|
|
|
|
|
void setup() {
|
2024-01-03 00:50:22 +00:00
|
|
|
|
|
|
|
// // Set a timer interrupt for the phase accumulation
|
|
|
|
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
|
|
|
|
|
2024-01-02 23:58:06 +00:00
|
|
|
Serial.begin(115200);
|
|
|
|
while (!Serial)
|
|
|
|
delay(10); // will pause Zero, Leonardo, etc until serial console opens
|
|
|
|
|
|
|
|
|
|
|
|
// Try to initialize!
|
2024-01-03 00:50:22 +00:00
|
|
|
|
2024-01-02 23:58:06 +00:00
|
|
|
if (!mcp.begin(0x64)) {
|
|
|
|
Serial.println("Failed to find MCP4728 chip");
|
|
|
|
while (1) {
|
|
|
|
delay(10);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Serial.println("Adafruit MCP4728 initialised");
|
|
|
|
}
|
|
|
|
|
2024-01-03 00:50:22 +00:00
|
|
|
Wire.setClock(3400000L);
|
2024-01-02 23:58:06 +00:00
|
|
|
|
2024-01-03 00:50:22 +00:00
|
|
|
freq=6600000;
|
2024-01-02 23:58:06 +00:00
|
|
|
phase=0;
|
|
|
|
calc_phase_inc();
|
|
|
|
|
|
|
|
setwave();
|
|
|
|
}
|
|
|
|
|
|
|
|
const float pi=3.14159265;
|
|
|
|
byte waveform[nsamp];
|
|
|
|
void setwave(){
|
|
|
|
for (int isamp=0; isamp<nsamp; ++isamp){
|
|
|
|
float phip=(isamp+0.5)/nsamp;
|
|
|
|
float phi=2*pi*phip;
|
|
|
|
int val=0;
|
|
|
|
|
|
|
|
//saw
|
2024-01-03 00:50:22 +00:00
|
|
|
//val = dacmax * isamp / nsamp;
|
2024-01-02 23:58:06 +00:00
|
|
|
//val = ( isamp < nsamp / 2 ) ? 0 : dacmax - 1;
|
|
|
|
//sine
|
2024-01-03 00:50:22 +00:00
|
|
|
val=(sin(phi)+1.0)*dacmax/2;
|
2024-01-02 23:58:06 +00:00
|
|
|
//val=((sin(phi)+0.333*sin(3*phi))/0.943+1)*dacmax/2;
|
|
|
|
|
|
|
|
val=max(val,0);
|
|
|
|
val=min(val,dacmax-1);
|
|
|
|
waveform[isamp]=val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-03 00:50:22 +00:00
|
|
|
//calculate the phase increment.
|
2024-01-02 23:58:06 +00:00
|
|
|
void calc_phase_inc(){
|
|
|
|
phase_inc=0.268435456*nclk*freq;
|
|
|
|
}
|
|
|
|
|
2024-01-03 00:50:22 +00:00
|
|
|
|
|
|
|
ISR(TIMER1_COMPA_vect){// timer1 interrupt
|
|
|
|
phase += phase_inc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//regular running:
|
2024-01-02 23:58:06 +00:00
|
|
|
void loop() {
|
2024-01-03 00:50:22 +00:00
|
|
|
int redphase = phase >> 24;
|
|
|
|
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
|
|
|
|
//Serial.println(redphase);
|
2024-01-02 23:58:06 +00:00
|
|
|
}
|
|
|
|
|