Phased accumulator with a 32-bit waveform is kind of working
parent
5c8e20e17d
commit
66b95576a9
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
Adafruit_MCP4728 mcp;
|
Adafruit_MCP4728 mcp;
|
||||||
|
|
||||||
#define nsamp 256
|
#define nsamp 32
|
||||||
#define dacmax 256
|
#define dacmax 256
|
||||||
|
|
||||||
const byte nclk = 200; // a guess
|
const byte nclk = 200; // a guess
|
||||||
|
@ -16,45 +16,21 @@ 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
|
|
||||||
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)
|
|
||||||
delay(10); // will pause Zero, Leonardo, etc until serial console opens
|
|
||||||
|
|
||||||
|
|
||||||
// Try to initialize!
|
|
||||||
|
|
||||||
if (!mcp.begin(0x64)) {
|
if (!mcp.begin(0x64)) {
|
||||||
Serial.println("Failed to find MCP4728 chip");
|
|
||||||
while (1) {
|
while (1) {
|
||||||
delay(10);
|
delay(100);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Serial.println("Adafruit MCP4728 initialised");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Wire.setClock(3400000L);
|
// mcp.setSpeed(400000L);
|
||||||
|
// mcp.setSpeed(800000L);
|
||||||
|
mcp.setSpeed(800000L);
|
||||||
|
|
||||||
freq=6600000;
|
freq=440;
|
||||||
phase=0;
|
phase=0;
|
||||||
calc_phase_inc();
|
calc_phase_inc();
|
||||||
|
|
||||||
|
@ -63,6 +39,7 @@ void setup() {
|
||||||
|
|
||||||
const float pi=3.14159265;
|
const float pi=3.14159265;
|
||||||
byte waveform[nsamp];
|
byte waveform[nsamp];
|
||||||
|
byte phaseb = 0;
|
||||||
void setwave(){
|
void setwave(){
|
||||||
for (int isamp=0; isamp<nsamp; ++isamp){
|
for (int isamp=0; isamp<nsamp; ++isamp){
|
||||||
float phip=(isamp+0.5)/nsamp;
|
float phip=(isamp+0.5)/nsamp;
|
||||||
|
@ -85,19 +62,17 @@ void setwave(){
|
||||||
|
|
||||||
//calculate the phase increment.
|
//calculate the phase increment.
|
||||||
void calc_phase_inc(){
|
void calc_phase_inc(){
|
||||||
phase_inc=0.268435456*nclk*freq;
|
// by measurement, 2^27 is about 138Hz (C below middle C)
|
||||||
|
// 2^27 / 138 = 972592.231884058
|
||||||
|
phase_inc = freq * 975592.231884058;
|
||||||
|
Serial.println(phase_inc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ISR(TIMER1_COMPA_vect){// timer1 interrupt
|
|
||||||
phase += phase_inc;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//regular running:
|
|
||||||
void loop() {
|
void loop() {
|
||||||
int redphase = phase >> 24;
|
phase += phase_inc;
|
||||||
|
int redphase = phase >> 27;
|
||||||
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
|
mcp.fastWrite(waveform[redphase] << 4, 0, 0, 0);
|
||||||
//Serial.println(redphase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue