Guest User

msp430music

a guest
Jan 12th, 2012
241
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <msp430g2231.h>
  2. #include <signal.h>
  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, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4,
  96.         NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, NOTE_C5,
  97.         NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5,
  98.         NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6,
  99.         NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6,
  100.         NOTE_AS6, NOTE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7,
  101.         NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7 };
  102. //char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8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,2b.";
  108. //char *song = "Looney:d=4,o=5,b=140:32p,c6,8f6,8e6,8d6,8c6,a.,8c6,8f6,8e6,8d6,8d#6,e.6,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6,8g,8a#,8a,8f";
  109. //char *song = "20thCenFox:d=16,o=5,b=140:b,8p,b,b,2b,p,c6,32p,b,32p,c6,32p,b,32p,c6,32p,b,8p,b,b,b,32p,b,32p,b,32p,b,32p,b,32p,b,32p,b,32p,g#,32p,a,32p,b,8p,b,b,2b,4p,8e,8g#,8b,1c#6,8f#,8a,8c#6,1e6,8a,8c#6,8e6,1e6,8b,8g#,8a,2b";
  110. //char *song = "Bond:d=4,o=5,b=80:32p,16c#6,32d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6,32e6,16e6,8e6,16d#6,16d#6,16d#6,16c#6,32d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6,32e6,16e6,8e6,16d#6,16d6,16c#6,16c#7,c.7,16g#6,16f#6,g#.6";
  111. //char *song = "MASH:d=8,o=5,b=140:4a,4g,f#,g,p,f#,p,g,p,f#,p,2e.,p,f#,e,4f#,e,f#,p,e,p,4d.,p,f#,4e,d,e,p,d,p,e,p,d,p,2c#.,p,d,c#,4d,c#,d,p,e,p,4f#,p,a,p,4b,a,b,p,a,p,b,p,2a.,4p,a,b,a,4b,a,b,p,2a.,a,4f#,a,b,p,d6,p,4e.6,d6,b,p,a,p,2b";
  112. //char *song = "StarWars:d=4,o=5,b=45:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6";
  113. //char *song = "GoodBad:d=4,o=5,b=56:32p,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,d#,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,c#6,32a#,32d#6,32a#,32d#6,8a#.,16f#.,32f.,32d#.,c#,32a#,32d#6,32a#,32d#6,8a#.,16g#.,d#";
  114. //char *song = "TopGun:d=4,o=4,b=31:32p,16c#,16g#,16g#,32f#,32f,32f#,32f,16d#,16d#,32c#,32d#,16f,32d#,32f,16f#,32f,32c#,16f,d#,16c#,16g#,16g#,32f#,32f,32f#,32f,16d#,16d#,32c#,32d#,16f,32d#,32f,16f#,32f,32c#,g#";
  115. //char *song = "A-Team:d=8,o=5,b=125:4d#6,a#,2d#6,16p,g#,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p,c#.6,16c6,16a#,g#.,2a#";
  116. //char *song = "Flinstones:d=4,o=5,b=40:32p,16f6,16a#,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,d6,16f6,16a#.,16a#6,32g6,16f6,16a#.,32f6,32f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,a#,16a6,16d.6,16a#6,32a6,32a6,32g6,32f#6,32a6,8g6,16g6,16c.6,32a6,32a6,32g6,32g6,32f6,32e6,32g6,8f6,16f6,16a#.,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#6,16c7,8a#.6";
  117. //char *song = "Jeopardy:d=4,o=6,b=125:c,f,c,f5,c,f,2c,c,f,c,f,a.,8g,8f,8e,8d,8c#,c,f,c,f5,c,f,2c,f.,8d,c,a#5,a5,g5,f5,p,d#,g#,d#,g#5,d#,g#,2d#,d#,g#,d#,g#,c.7,8a#,8g#,8g,8f,8e,d#,g#,d#,g#5,d#,g#,2d#,g#.,8f,d#,c#,c,p,a#5,p,g#.5,d#,g#";
  118. //char *song = "Gadget:d=16,o=5,b=50:32d#,32f,32f#,32g#,a#,f#,a,f,g#,f#,32d#,32f,32f#,32g#,a#,d#6,4d6,32d#,32f,32f#,32g#,a#,f#,a,f,g#,f#,8d#";
  119. //char *song = "Smurfs:d=32,o=5,b=200:4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8f#,p,8a#,p,4g#,4p,g#,p,a#,p,b,p,c6,p,4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8b,p,8f,p,4f#";
  120. //char *song = "MahnaMahna:d=16,o=6,b=125:c#,c.,b5,8a#.5,8f.,4g#,a#,g.,4d#,8p,c#,c.,b5,8a#.5,8f.,g#.,8a#.,4g,8p,c#,c.,b5,8a#.5,8f.,4g#,f,g.,8d#.,f,g.,8d#.,f,8g,8d#.,f,8g,d#,8c,a#5,8d#.,8d#.,4d#,8d#.";
  121. //char *song = "LeisureSuit:d=16,o=6,b=56:f.5,f#.5,g.5,g#5,32a#5,f5,g#.5,a#.5,32f5,g#5,32a#5,g#5,8c#.,a#5,32c#,a5,a#.5,c#.,32a5,a#5,32c#,d#,8e,c#.,f.,f.,f.,f.,f,32e,d#,8d,a#.5,e,32f,e,32f,c#,d#.,c#";
  122. char *song =
  123.         "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";
  124. volatile unsigned int time = 0;
  125. #define isdigit(n) (n >= '0' && n <= '9')
  126. /** Delay function. **/
  127. void delay(unsigned int ms) {
  128.     unsigned int i, ms2; //Modified here, eliminated volatile
  129.     i = time;
  130.     ms2 = ms * 2;
  131.     while ((time - i) < ms2) {
  132.         __no_operation();
  133.     }
  134.     //__delay_cycles(500);
  135. }
  136.  
  137. play(unsigned int hz) {
  138.     CCR0 = (1000000 / hz) - 1;
  139.     CCR1 = (1000000 / hz) / 2;
  140.     TACTL = TASSEL_2 + MC_1;
  141. }
  142.  
  143. stop() {
  144.     TACTL = TASSEL_2 + MC_3; //stop
  145.     CCR0 = 0;
  146. }
  147.  
  148. int main(void) {
  149.     WDTCTL = WDTPW + WDTTMSEL + WDTCNTCL + WDTIS1; // Set interval mode, set to zero and interval to 0.5 ms
  150.     IE1 |= WDTIE; // Enable WDT interrupt
  151.     BCSCTL1 = CALBC1_1MHZ;
  152.     DCOCTL = CALDCO_1MHZ;
  153.     //asm("EINT");
  154.     __enable_interrupt();
  155.  
  156.     P1DIR |= BIT2; // P1.2 to output
  157.     P1DIR |= BIT1; // P1.1 to output
  158.     P1OUT |= ~BIT1;
  159.     P1SEL |= BIT2; // P1.2 to TA0.1
  160.  
  161.     CCTL1 = OUTMOD_7; // CCR1 reset/set
  162.  
  163.     byte default_dur = 4;
  164.     byte default_oct = 6;
  165.     int bpm = 63;
  166.     int num;
  167.     long wholenote;
  168.     long duration;
  169.     byte note;
  170.     byte scale;
  171.     char *p = song;
  172.  
  173.     while (*p != ':')
  174.         p++; // ignore name
  175.     p++; // skip ':'
  176.  
  177.     // get default duration
  178.     if (*p == 'd') {
  179.         p++;
  180.         p++; // skip "d="
  181.         num = 0;
  182.         while (isdigit(*p)) {
  183.             num = (num * 10) + (*p++ - '0');
  184.         }
  185.         if (num > 0)
  186.             default_dur = num;
  187.         p++; // skip comma
  188.     }
  189.  
  190.     // get default octave
  191.     if (*p == 'o') {
  192.         p++;
  193.         p++; // skip "o="
  194.         num = *p++ - '0';
  195.         if (num >= 3 && num <= 7)
  196.             default_oct = num;
  197.         p++; // skip comma
  198.     }
  199.  
  200.     // get BPM
  201.     if (*p == 'b') {
  202.         p++;
  203.         p++; // skip "b="
  204.         num = 0;
  205.         while (isdigit(*p)) {
  206.             num = (num * 10) + (*p++ - '0');
  207.         }
  208.         bpm = num;
  209.         p++; // skip colon
  210.     }
  211.  
  212.     // BPM usually expresses the number of quarter notes per minute
  213.     wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in milliseconds)
  214.  
  215.     // now begin note loop
  216.     while (*p) {
  217.         // first, get note duration, if available
  218.         num = 0;
  219.         while (isdigit(*p)) {
  220.             num = (num * 10) + (*p++ - '0');
  221.         }
  222.  
  223.         if (num)
  224.             duration = wholenote / num;
  225.         else
  226.             duration = wholenote / default_dur; // we will need to check if we are a dotted note after
  227.  
  228.         // now get the note
  229.         note = 0;
  230.  
  231.         switch (*p) {
  232.         case 'c':
  233.             note = 1;
  234.             break;
  235.         case 'd':
  236.             note = 3;
  237.             break;
  238.         case 'e':
  239.             note = 5;
  240.             break;
  241.         case 'f':
  242.             note = 6;
  243.             break;
  244.         case 'g':
  245.             note = 8;
  246.             break;
  247.         case 'a':
  248.             note = 10;
  249.             break;
  250.         case 'b':
  251.             note = 12;
  252.             break;
  253.         case 'p':
  254.         default:
  255.             note = 0;
  256.         }
  257.         p++;
  258.  
  259.         // now, get optional '#' sharp
  260.         if (*p == '#') {
  261.             note++;
  262.             p++;
  263.         }
  264.  
  265.         // now, get optional '.' dotted note
  266.         if (*p == '.') {
  267.             duration += duration / 2;
  268.             p++;
  269.         }
  270.  
  271.         // now, get scale
  272.         if (isdigit(*p)) {
  273.             scale = *p - '0';
  274.             p++;
  275.         } else {
  276.             scale = default_oct;
  277.         }
  278.  
  279.         scale += OCTAVE_OFFSET;
  280.  
  281.         if (*p == ',')
  282.             p++; // skip comma for next note (or we may be at the end)
  283.  
  284.         // now play the note
  285.  
  286.         if (note) {
  287.             play(notes[(scale - 4) * 12 + note]);
  288.             delay(duration);
  289.             stop();
  290.         } else {
  291.             delay(duration);
  292.         }
  293.     }
  294.     __disable_interrupt();
  295.     //asm("dint");
  296.     _BIS_SR(LPM4_bits);
  297. }
  298.  
  299. #pragma vector=WDT_VECTOR
  300. __interrupt void watchdog_timer(void) //__interrupt void watchdog_timer
  301. {
  302.     time++;
  303. }
RAW Paste Data