/* * LiquidatorDACirq.ino * Arduino Diecimila sketch * Author: G. Forrest Cook * License: GPL Version 3. * * February 21, 2022 * derived from BigBot522irq * * Modulate a MAX522 8 bit DAC with an exponentially weighted waveform. * The waveform is stepped by an interrupt signal on digital pin 2 (IRQ 0) * which is generated by a CMOS 555 timer chip. * PB0 (8) is used for the SPI chip select because the normal SS pin * only works for 8 bit transfers and the MAX522 takes 16 bits. * PB1 (9) is used for a yellow status LED. */ #include volatile int indx = 0; // interrupt routine table index int mode; // status of the 2 jumpers byte *wavetable; // pointer to the wave table const int DacCS = 8; // SPI chip select pin (PB0) const int LedPin = 9; // Yellow LED pin (PB1) const int Sel0 = 6; // Mode Select Jumper 0 (PD6) const int Sel1 = 7; // Mode Select Jumper 1 (PD7) // Basic sine wave const byte wavetable1[] = {0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 18, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 42, 44, 46, 49, 51, 54, 56, 59, 62, 64, 67, 70, 73, 76, 79, 81, 84, 87, 90, 93, 96, 99, 103, 106, 109, 112, 115, 118, 121, 124, 127, 131, 134, 137, 140, 143, 146, 149, 152, 156, 159, 162, 165, 168, 171, 174, 176, 179, 182, 185, 188, 191, 193, 196, 199, 201, 204, 206, 209, 211, 213, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 237, 239, 240, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 240, 239, 237, 236, 234, 232, 230, 228, 226, 224, 222, 220, 218, 216, 213, 211, 209, 206, 204, 201, 199, 196, 193, 191, 188, 185, 182, 179, 176, 174, 171, 168, 165, 162, 159, 156, 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109, 106, 103, 99, 96, 93, 90, 87, 84, 81, 79, 76, 73, 70, 67, 64, 62, 59, 56, 54, 51, 49, 46, 44, 42, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 18, 16, 15, 13, 12, 10, 9, 8, 7, 6, 5, 4, 3, 3, 2, 1, 1, 0, 0, 0, 0, 0}; // Big Bottom wave const byte wavetable2[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 22, 23, 24, 26, 27, 29, 30, 32, 34, 36, 37, 39, 41, 43, 45, 47, 49, 51, 54, 56, 58, 61, 63, 66, 68, 71, 74, 76, 79, 82, 85, 88, 91, 94, 97, 100, 103, 107, 110, 113, 116, 120, 123, 127, 130, 134, 137, 141, 144, 148, 152, 155, 159, 162, 166, 170, 173, 177, 180, 184, 187, 191, 194, 197, 201, 204, 207, 210, 213, 216, 219, 222, 225, 228, 230, 233, 235, 237, 239, 241, 243, 245, 246, 248, 249, 251, 252, 253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 254, 253, 253, 252, 251, 249, 248, 246, 245, 243, 241, 239, 237, 235, 233, 230, 228, 225, 222, 219, 216, 213, 210, 207, 204, 201, 197, 194, 191, 187, 184, 180, 177, 173, 170, 166, 162, 159, 155, 152, 148, 144, 141, 137, 134, 130, 127, 123, 120, 116, 113, 110, 107, 103, 100, 97, 94, 91, 88, 85, 82, 79, 76, 74, 71, 68, 66, 63, 61, 58, 56, 54, 51, 49, 47, 45, 43, 41, 39, 37, 36, 34, 32, 30, 29, 27, 26, 24, 23, 22, 20, 19, 18, 17, 15, 14, 13, 12, 11, 10, 9, 9, 8, 7, 6, 6, 5, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}; // Big Top wave const byte wavetable3[] = {255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 253, 252, 252, 251, 250, 250, 249, 249, 248, 247, 246, 245, 245, 244, 243, 242, 241, 240, 238, 237, 236, 235, 233, 232, 231, 229, 228, 226, 225, 223, 221, 219, 218, 216, 214, 212, 210, 208, 206, 203, 201, 199, 197, 194, 192, 189, 187, 184, 181, 179, 176, 173, 170, 167, 164, 161, 158, 155, 152, 148, 145, 142, 138, 135, 132, 128, 125, 121, 118, 114, 111, 107, 103, 100, 96, 93, 89, 85, 82, 78, 75, 71, 68, 64, 61, 57, 54, 51, 48, 45, 42, 39, 36, 33, 30, 27, 25, 22, 20, 18, 16, 14, 12, 10, 8, 7, 6, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22, 25, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 61, 64, 68, 71, 75, 78, 82, 85, 89, 93, 96, 100, 103, 107, 111, 114, 118, 121, 125, 128, 132, 135, 138, 142, 145, 148, 152, 155, 158, 161, 164, 167, 170, 173, 176, 179, 181, 184, 187, 189, 192, 194, 197, 199, 201, 203, 206, 208, 210, 212, 214, 216, 218, 219, 221, 223, 225, 226, 228, 229, 231, 232, 233, 235, 236, 237, 238, 240, 241, 242, 243, 244, 245, 245, 246, 247, 248, 249, 249, 250, 250, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255}; void setup() { DDRB = B00101011; // Set port B to output for bits 0,1,3,5. DDRC = B00000000; // Set port C to input for bits 0 through 5. DDRD = B00000000; // Set port D to input for bits 0 through 7. digitalWrite(DacCS, HIGH); // DAC chip select, not the normal SS pin. SPI.begin(); // Start the SPI port, this also sets DDRB mode = digitalRead(Sel0) + 2*digitalRead(Sel1); Blink9 ((byte)mode+1); // blink the yellow LED to show the mode +1 delay(1000); // pause, then leave the LED on. digitalWrite(LedPin, HIGH); switch (mode) // Pick one of 3 waves or enter calibrate mode. { case 0: wavetable = wavetable1; break; case 1: wavetable = wavetable2; break; case 2: wavetable = wavetable3; break; case 3: // Mode 3 outputs 0 to the dac for calibration digitalWrite(DacCS, LOW); // Chip Select the DAC SPI.transfer(0x23); // send a command byte SPI.transfer(0); // send value 0 digitalWrite(DacCS, HIGH); // UnSelect the DAC while (1); // loop forever break; } attachInterrupt(0, dacstep, RISING); // Int 0 causes dacstep to run. } void loop() { // All of the code is in the dacstep interrupt routine. } void dacstep() { digitalWrite(DacCS, LOW); // Chip Select the DAC SPI.transfer(0x23); // send a command byte SPI.transfer(wavetable[indx]); // send waveform data digitalWrite(DacCS, HIGH); // UnSelect the DAC indx++; // Step through the waveform array if (indx >= 256) indx = 0; } void Blink9(byte count) // blink the yellow LED on port 9 (PB1) { byte i; for (i=0; i