You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
7.5 KiB

8 years ago
  1. #include "beeps.h"
  2. #include <math.h>
  3. #include <avr/pgmspace.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/io.h>
  6. #define PI 3.14159265
  7. #define CHANNEL OCR1C
  8. volatile uint16_t sample;
  9. uint16_t lastSample;
  10. const int sounddata_length=200;
  11. const unsigned char sounddata_data[] PROGMEM = {128,
  12. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  13. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  14. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  15. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  16. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  17. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  18. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  19. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  20. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  21. 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129, 127, 129, 128, 127, 133,
  22. 117, 109, 125, 121, 116, 132, 140, 126, 114, 114, 116, 120, 114, 93, 73, 66, 76, 116, 142, 129,
  23. 128, 129, 120, 119, 118, 104, 87, 123, 181, 194, 196, 198, 189, 176, 160, 162, 172, 164, 164, 183,
  24. 197, 188, 168, 167, 170, 165, 185, 209, 206, 196, 196, 199, 185, 162, 156, 167, 176, 173, 170, 166,
  25. 151, 142, 140, 134, 130, 127, 113, 86, 67, 66, 69, 75, 73, 75, 86, 90, 91, 84, 65, 48,
  26. 41, 30, 26, 56, 91, 88, 72, 70, 73, 82, 89, 73, 57, 60, 74, 89, 92, 77, 63, 60,
  27. 53, 47, 56, 64, 63, 61, 56, 54, 52, 36, 16, 22, 51, 66, 67, 70, 76, 88, 99, 92,
  28. 77, 74, 85, 100, 106, 97, 83, 85, 96, 108, 133, 160, 164};
  29. void delay_us(int count) {
  30. while(count--) {
  31. _delay_us(1);
  32. }
  33. }
  34. void beeps() {
  35. // DDRB |= (1<<7);
  36. // PORTB &= ~(1<<7);
  37. // // Use full 16-bit resolution.
  38. // ICR1 = 0xFFFF;
  39. // // I could write a wall of text here to explain... but TL;DW
  40. // // Go read the ATmega32u4 datasheet.
  41. // // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  42. // // Pin PB7 = OCR1C (Timer 1, Channel C)
  43. // // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  44. // // (i.e. start high, go low when counter matches.)
  45. // // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  46. // // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  47. // TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  48. // TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  49. // // Turn off PWM control on PB7, revert to output low.
  50. // // TCCR1A &= ~(_BV(COM1C1));
  51. // // CHANNEL = ((1 << level) - 1);
  52. // // Turn on PWM control of PB7
  53. // TCCR1A |= _BV(COM1C1);
  54. // // CHANNEL = level << OFFSET | 0x0FFF;
  55. // // CHANNEL = 0b1010101010101010;
  56. // float x = 12;
  57. // float y = 24;
  58. // float length = 50;
  59. // float scale = 1;
  60. // // int f1 = 1000000/440;
  61. // // int f2 = 1000000/880;
  62. // // for (uint32_t i = 0; i < length * 1000; i++) {
  63. // // // int frequency = 1/((sin(PI*2*i*scale*pow(2, x/12.0))*.5+1 + sin(PI*2*i*scale*pow(2, y/12.0))*.5+1) / 2);
  64. // // ICR1 = f1; // Set max to the period
  65. // // OCR1C = f1 >> 1; // Set compare to half the period
  66. // // // _delay_us(10);
  67. // // }
  68. // int frequency = 1000000/440;
  69. // ICR1 = frequency; // Set max to the period
  70. // OCR1C = frequency >> 1; // Set compare to half the period
  71. // _delay_us(500000);
  72. // TCCR1A &= ~(_BV(COM1C1));
  73. // CHANNEL = 0;
  74. play_notes();
  75. // play_note(55*pow(2, 0/12.0), 1);
  76. // play_note(55*pow(2, 12/12.0), 1);
  77. // play_note(55*pow(2, 24/12.0), 1);
  78. // play_note(55*pow(2, 0/12.0), 1);
  79. // play_note(55*pow(2, 12/12.0), 1);
  80. // play_note(55*pow(2, 24/12.0), 1);
  81. // play_note(0, 4);
  82. // play_note(55*pow(2, 0/12.0), 8);
  83. // play_note(55*pow(2, 12/12.0), 4);
  84. // play_note(55*pow(2, 10/12.0), 4);
  85. // play_note(55*pow(2, 12/12.0), 8);
  86. // play_note(55*pow(2, 10/12.0), 4);
  87. // play_note(55*pow(2, 7/12.0), 2);
  88. // play_note(55*pow(2, 8/12.0), 2);
  89. // play_note(55*pow(2, 7/12.0), 16);
  90. // play_note(0, 4);
  91. // play_note(55*pow(2, 3/12.0), 8);
  92. // play_note(55*pow(2, 5/12.0), 4);
  93. // play_note(55*pow(2, 7/12.0), 4);
  94. // play_note(55*pow(2, 7/12.0), 8);
  95. // play_note(55*pow(2, 5/12.0), 4);
  96. // play_note(55*pow(2, 3/12.0), 4);
  97. // play_note(55*pow(2, 2/12.0), 16);
  98. }
  99. void play_note(float freq, int length) {
  100. DDRB |= (1<<7);
  101. PORTB &= ~(1<<7);
  102. if (freq > 0) {
  103. int frequency = 1000000/freq;
  104. ICR1 = frequency; // Set max to the period
  105. OCR1C = frequency >> 1; // Set compare to half the period
  106. TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  107. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  108. }
  109. for (int i = 0; i < length; i++) {
  110. _delay_us(50000);
  111. }
  112. TCCR1A &= ~(_BV(COM1C1));
  113. }
  114. // This is called at 8000 Hz to load the next sample.
  115. ISR(TIMER1_COMPA_vect) {
  116. if (sample >= sounddata_length) {
  117. if (sample == sounddata_length + lastSample) {
  118. TIMSK1 &= ~_BV(OCIE1A);
  119. // Disable the per-sample timer completely.
  120. TCCR1B &= ~_BV(CS10);
  121. }
  122. else {
  123. OCR1C = sounddata_length + lastSample - sample;
  124. }
  125. }
  126. else {
  127. OCR1C = pgm_read_byte(&sounddata_data[sample]);
  128. }
  129. ++sample;
  130. }
  131. void play_notes() {
  132. // Set up Timer 2 to do pulse width modulation on the speaker
  133. // pin.
  134. DDRB |= (1<<7);
  135. PORTB &= ~(1<<7);
  136. // Use internal clock (datasheet p.160)
  137. // ASSR &= ~(_BV(EXCLK) | _BV(AS2));
  138. // Set fast PWM mode (p.157)
  139. TCCR1A |= _BV(WGM21) | _BV(WGM20);
  140. TCCR1B &= ~_BV(WGM22);
  141. // Do non-inverting PWM on pin OC2A (p.155)
  142. // On the Arduino this is pin 11.
  143. TCCR1A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
  144. TCCR1A &= ~(_BV(COM2B1) | _BV(COM2B0));
  145. // No prescaler (p.158)
  146. TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
  147. // Set initial pulse width to the first sample.
  148. OCR1A = pgm_read_byte(&sounddata_data[0]);
  149. cli();
  150. // Set CTC mode (Clear Timer on Compare Match) (p.133)
  151. // Have to set OCR1A *after*, otherwise it gets reset to 0!
  152. TCCR2B = (TCCR2B & ~_BV(WGM13)) | _BV(WGM12);
  153. TCCR2A = TCCR2A & ~(_BV(WGM11) | _BV(WGM10));
  154. // No prescaler (p.134)
  155. TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
  156. // Set the compare register (OCR1A).
  157. // OCR1A is a 16-bit register, so we have to do this with
  158. // interrupts disabled to be safe.
  159. // OCR2A = F_CPU / SAMPLE_RATE; // 16e6 / 8000 = 2000
  160. OCR2A = 2000;
  161. // Enable interrupt when TCNT1 == OCR1A (p.136)
  162. TIMSK1 |= _BV(OCIE2A);
  163. sample = 0;
  164. sei();
  165. }
  166. void note(int x, float length) {
  167. DDRB |= (1<<1);
  168. int t = (int)(440*pow(2,-x/12.0)); // starting note
  169. for (int y = 0; y < length*1000/t; y++) { // note length
  170. PORTB |= (1<<1);
  171. delay_us(t);
  172. PORTB &= ~(1<<1);
  173. delay_us(t);
  174. }
  175. PORTB &= ~(1<<1);
  176. }
  177. void true_note(float x, float y, float length) {
  178. for (uint32_t i = 0; i < length * 50; i++) {
  179. uint32_t v = (uint32_t) (round(sin(PI*2*i*640000*pow(2, x/12.0))*.5+1 + sin(PI*2*i*640000*pow(2, y/12.0))*.5+1) / 2 * pow(2, 8));
  180. for (int u = 0; u < 8; u++) {
  181. if (v & (1 << u) && !(PORTB&(1<<1)))
  182. PORTB |= (1<<1);
  183. else if (PORTB&(1<<1))
  184. PORTB &= ~(1<<1);
  185. }
  186. }
  187. PORTB &= ~(1<<1);
  188. }