 /*
- *
- * DDS Sine Generator mit ATMEGS 168
- * Timer2 generates the  31250 KHz Clock Interrupt
- *
- * KHM 2009 /  Martin Nawrath
- * Kunsthochschule fuer Medien Koeln
- * Academy of Media Arts Cologne
+KidModular - OSCILLATOR 
+by Tiago Angelo (aka p1nho)
 
- */
+Built with: 
+Arduino 1.0.5 (on Arduino MEGA2560)
+Stripped out version of UTFT Library v.2.82 (12 Jul 2016) by Henning Karlsen
+Audio ouput using Direct Digital Synthesis method from an example by Martin Nawrath
+*/
 
+/************************************ INCLUDED LIBS & HEADERS ***********************************************/
 #include "avr/pgmspace.h"
+#include "Wavetables.h"
 #include <UTFT.h>
-#include "Wavetables.h"
 
+/************************************ DEFINITIONS ***********************************************************/
+// Audio
 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
+// MEGA=pin10 UNO=pin11 
+#define AUDIOPIN 10
 
+/************************************ CLASS OBJECTS *********************************************************/
+// LCD
 UTFT myGLCD(CTE32HR,38,39,40,41);
 
-int ledPin = 13;                 // LED pin 7
-int testPin = 7;
-int t2Pin = 6;
+/************************************ VARIABLES *************************************************************/
+// Audio
 byte bb;
-
 double dfreq;
 // const double refclk=31372.549;  // =16MHz / 510
 const double refclk=31376.6;      // measured
-
 // variables used inside interrupt service declared as voilatile
 volatile byte icnt;              // var inside interrupt
 volatile byte icnt1;             // var inside interrupt

volatile unsigned long phaccu;   // pahse accumulator
volatile unsigned long tword_m;  // dds tuning word m

void setup()
{
  // Setup the LCD
  myGLCD.InitLCD();
  
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
//  Serial.begin(115200);        // connect to the serial port
//  Serial.println("DDS Test");

  pinMode(6, OUTPUT);      // sets the digital pin as output
  pinMode(7, OUTPUT);      // sets the digital pin as output
  // pin10 on ArduinoMEGA
  pinMode(10, OUTPUT);     // pin11= PWM  output / frequency output

  Setup_timer2();

  // disable interrupts to avoid timing distortion
  cbi (TIMSK0,TOIE0);              // disable Timer0 !!! delay() is now not available
  sbi (TIMSK2,TOIE2);              // enable Timer2 Interrupt

  dfreq=1000.0;                    // initial output frequency = 1000.o Hz
  tword_m=pow(2,32)*dfreq/refclk;  // calulate DDS new tuning word 
  
  
}
void loop()
{
  myGLCD.fillScr(VGA_RED);
  
  while(1) {
     if (c4ms > 250) {                 // timer / wait fou a full second
      c4ms=0;

    c4ms++;
    icnt1=0;
   }   
}
