#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg256.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg256b"

#include "main_asm.h" /* interface to the assembly module */



             // Pitch Number = 750000 / (frequency)
             // ex: 750,000 / 261.63Hz =  2867
               
# define  AAf     3612   // A flat
# define  AA      3409
# define  BB      3038      
# define  c       2867
# define  d       2554
# define  e       2276
# define  f       2148
# define  g       1914
# define  a       1705
# define  af      1806   // A flat
# define  b       1519
# define  C       1434
# define  D       1277
# define  E       1138
# define  F       1074
# define  G       947
# define  A       853



/*  

  TIME CODES
  ----------

 16 = Whole Note ... O   |
  8 = Half Note  ....... O   |
  4 = Quarter Note ......... *   |-
  2 = Eighth Note  ............. *   |=
  1 = Sixteenth Note ............... *
  
  Note: period after a note makes that note 1.5x as long.


   NOTES
   -----

                          -O-  A
                         O   G
-----------------------O---F
                     O   E
-------------------O---D---
                 O   C
---------------O---b-------            | TREBLE |
             O   a     (af) = A flat   | CLEF   |
-----------O---g-----------
         O   f
-------O---e---------------
     O   d
  -O-  c
  
                     O   BB
-------------------O---AA---   (AAf) = A flat
                 O
---------------O------------         
             O                        |  BASS  |
-----------O----------------          |  CLEF  |
         O
-------O--------------------
     O
---O------------------------  
 O
 
 Note: 0 (zero) indicates a rest.


 */


int j;
int pitch;

// MUSIC ARRAYS

// example: Mary had a little lamb
// int notes[1000] = {b, a, g, a, b, b, b, 0, a, a, a, 0, b, b, b, 0, b, a, g, a, b, b, b, 0, a, a, b, a, g};
// int times[1000] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};

 int notes[1000] = {b, a, g, a, b, b, b, 0, a, a, a, 0, b, b, b, 0, b, a, g, a, b, b, b, 0, a, a, b, a, g};
 int times[1000] = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};

void interrupt 13 handler()
  {
 tone(pitch); 
  }

void main(void) {
  PLL_init();


         while(1){
         
        
   for (j=0; j<1000; j++){
   
          pitch = notes[j];
          
          sound_init();
          
          if (pitch != 0)
              {
              sound_on();
              }
              
          ms_delay(70*times[j]);   // (70 * times[j]) = TIME to play note (in milliseconds)
          sound_off();             
          ms_delay(30);            // 30 = TIME between notes (in milliseconds)
                                   
   }                               // (You may have to play with these numbers to get the tempo just right.)
   }
        

}
