/* * BigBot522irq * Arduino Diecimila sketch * February 23, 2011, G. Forrest Cook * Released under the GPLv3 license. * * Modulate a MAX522 8 bit DAC with an exponentially weighted waveform. * Stepped by an interrupt on digital pin 2 (IRQ 0) * PB0 is used for the SPI chip select because the normal SS pin * only works for 8 bit transfers and the MAX522 takes 16 bits. */ #include volatile int indx = 0; const int DacCS = 8; // SPI chip select pin (PB0). #define STEPS 256 // 2.1 exp sine wave byte wavetable[STEPS] = {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}; void setup() { DDRB = 3; // Set port B to output for bits 0 and 1. digitalWrite(DacCS, HIGH); // DAC chip select, not normal SS pin. SPI.begin(); attachInterrupt(0, dacstep, RISING); } void loop() { // All of the code is in the interrupt routine. } void dacstep() { digitalWrite(DacCS, LOW); SPI.transfer(0x23); SPI.transfer(wavetable[indx]); digitalWrite(DacCS, HIGH); indx++; if (indx >= STEPS) indx = 0; }