Guest User

Untitled

a guest
May 17th, 2011
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include
  2. #include
  3. typedef unsigned char byte;
  4. #define NOTE_B0  31
  5. #define NOTE_C1  33
  6. #define NOTE_CS1 35
  7. #define NOTE_D1  37
  8. #define NOTE_DS1 39
  9. #define NOTE_E1  41
  10. #define NOTE_F1  44
  11. #define NOTE_FS1 46
  12. #define NOTE_G1  49
  13. #define NOTE_GS1 52
  14. #define NOTE_A1  55
  15. #define NOTE_AS1 58
  16. #define NOTE_B1  62
  17. #define NOTE_C2  65
  18. #define NOTE_CS2 69
  19. #define NOTE_D2  73
  20. #define NOTE_DS2 78
  21. #define NOTE_E2  82
  22. #define NOTE_F2  87
  23. #define NOTE_FS2 93
  24. #define NOTE_G2  98
  25. #define NOTE_GS2 104
  26. #define NOTE_A2  110
  27. #define NOTE_AS2 117
  28. #define NOTE_B2  123
  29. #define NOTE_C3  131
  30. #define NOTE_CS3 139
  31. #define NOTE_D3  147
  32. #define NOTE_DS3 156
  33. #define NOTE_E3  165
  34. #define NOTE_F3  175
  35. #define NOTE_FS3 185
  36. #define NOTE_G3  196
  37. #define NOTE_GS3 208
  38. #define NOTE_A3  220
  39. #define NOTE_AS3 233
  40. #define NOTE_B3  247
  41. #define NOTE_C4  262
  42. #define NOTE_CS4 277
  43. #define NOTE_D4  294
  44. #define NOTE_DS4 311
  45. #define NOTE_E4  330
  46. #define NOTE_F4  349
  47. #define NOTE_FS4 370
  48. #define NOTE_G4  392
  49. #define NOTE_GS4 415
  50. #define NOTE_A4  440
  51. #define NOTE_AS4 466
  52. #define NOTE_B4  494
  53. #define NOTE_C5  523
  54. #define NOTE_CS5 554
  55. #define NOTE_D5  587
  56. #define NOTE_DS5 622
  57. #define NOTE_E5  659
  58. #define NOTE_F5  698
  59. #define NOTE_FS5 740
  60. #define NOTE_G5  784
  61. #define NOTE_GS5 831
  62. #define NOTE_A5  880
  63. #define NOTE_AS5 932
  64. #define NOTE_B5  988
  65. #define NOTE_C6  1047
  66. #define NOTE_CS6 1109
  67. #define NOTE_D6  1175
  68. #define NOTE_DS6 1245
  69. #define NOTE_E6  1319
  70. #define NOTE_F6  1397
  71. #define NOTE_FS6 1480
  72. #define NOTE_G6  1568
  73. #define NOTE_GS6 1661
  74. #define NOTE_A6  1760
  75. #define NOTE_AS6 1865
  76. #define NOTE_B6  1976
  77. #define NOTE_C7  2093
  78. #define NOTE_CS7 2217
  79. #define NOTE_D7  2349
  80. #define NOTE_DS7 2489
  81. #define NOTE_E7  2637
  82. #define NOTE_F7  2794
  83. #define NOTE_FS7 2960
  84. #define NOTE_G7  3136
  85. #define NOTE_GS7 3322
  86. #define NOTE_A7  3520
  87. #define NOTE_AS7 3729
  88. #define NOTE_B7  3951
  89. #define NOTE_C8  4186
  90. #define NOTE_CS8 4435
  91. #define NOTE_D8  4699
  92. #define NOTE_DS8 4978
  93. #define OCTAVE_OFFSET 0
  94. //I've changed here int to const int in order to use the flash
  95. const int notes[] = { 0,
  96. NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
  97. NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
  98. NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
  99. NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
  100. };
  101. char *song = "MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d";
  102. volatile unsigned int time = 0;
  103. #define isdigit(n) (n >= '0' && n <= '9')
  104. /** Delay function. **/
  105. void delay(unsigned int ms) {
  106.   unsigned int i,ms2;  //Modified here, eliminated volatile
  107.   i = time;
  108.   ms2 = ms*2;
  109.   while ((time-i)  0) default_dur = num;
  110.     p++;                   // skip comma
  111.   }
  112.  
  113.   // get default octave
  114.   if(*p == 'o')
  115.   {
  116.     p++; p++;              // skip "o="
  117.     num = *p++ - '0';
  118.     if(num >= 3 && num <=7) default_oct = num;
  119.     p++;                   // skip comma
  120.   }
  121.  
  122.   // get BPM
  123.   if(*p == 'b')
  124.   {
  125.     p++; p++;              // skip "b="
  126.     num = 0;
  127.     while(isdigit(*p))
  128.     {
  129.       num = (num * 10) + (*p++ - '0');
  130.     }
  131.     bpm = num;
  132.     p++;                   // skip colon
  133.   }
  134.  
  135.   // BPM usually expresses the number of quarter notes per minute
  136.   wholenote = (60 * 1000L / bpm) * 4;  // this is the time for whole note (in milliseconds)
  137.  
  138.   // now begin note loop
  139.   while(*p)
  140.   {
  141.     // first, get note duration, if available
  142.     num = 0;
  143.     while(isdigit(*p))
  144.     {
  145.       num = (num * 10) + (*p++ - '0');
  146.     }
  147.    
  148.     if(num) duration = wholenote / num;
  149.     else duration = wholenote / default_dur;  // we will need to check if we are a dotted note after
  150.  
  151.     // now get the note
  152.     note = 0;
  153.  
  154.     switch(*p)
  155.     {
  156.       case 'c':
  157.         note = 1;
  158.         break;
  159.       case 'd':
  160.         note = 3;
  161.         break;
  162.       case 'e':
  163.         note = 5;
  164.         break;
  165.       case 'f':
  166.         note = 6;
  167.         break;
  168.       case 'g':
  169.         note = 8;
  170.         break;
  171.       case 'a':
  172.         note = 10;
  173.         break;
  174.       case 'b':
  175.         note = 12;
  176.         break;
  177.       case 'p':
  178.       default:
  179.         note = 0;
  180.     }
  181.     p++;
  182.  
  183.     // now, get optional '#' sharp
  184.     if(*p == '#')
  185.     {
  186.       note++;
  187.       p++;
  188.     }
  189.  
  190.     // now, get optional '.' dotted note
  191.     if(*p == '.')
  192.     {
  193.       duration += duration/2;
  194.       p++;
  195.     }
  196.  
  197.     // now, get scale
  198.     if(isdigit(*p))
  199.     {
  200.       scale = *p - '0';
  201.       p++;
  202.     }
  203.     else
  204.     {
  205.       scale = default_oct;
  206.     }
  207.  
  208.     scale += OCTAVE_OFFSET;
  209.  
  210.     if(*p == ',')
  211.       p++;       // skip comma for next note (or we may be at the end)
  212.  
  213.     // now play the note
  214.  
  215.     if(note)
  216.     {
  217.       play(notes[(scale - 4) * 12 + note]);
  218.       delay(duration);
  219.       stop();
  220.     }
  221.     else
  222.     {
  223.       delay(duration);
  224.     }
  225.   }
  226.   asm("dint");
  227.   _BIS_SR(LPM4_bits);
  228. }
  229.  
  230.  __interrupt void watchdog_timer(void)  //__interrupt void watchdog_timer
  231. {
  232.   time++;
  233. }
RAW Paste Data