Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. //Author: Montaj Pennant
  2. //Title: Homework4
  3.  
  4. #define LA 0x8E0B
  5. #define LAS 0x8612
  6. #define LB 0x7E8C
  7. #define LC 0x7772
  8. #define LCS 0x70BD
  9. #define LD 0x6A69
  10. #define LDS 6470
  11. #define LE 0x5ECD
  12. #define LF 0x597B
  13. #define LFS 0x5475
  14. #define LG 0x4FB8
  15. #define LGS 0x4B3E
  16. #define MA 0x4705
  17. #define MAS 0x4309
  18. #define MB 0x3F46
  19. #define MC 0x3BB9
  20. #define MC# 0x385E
  21. #define MD 0x3534
  22. #define MDS 0x3238
  23. #define ME 0x2F66
  24. #define MF 0x2CBD
  25. #define MFS 0x2A3A
  26. #define MG 0x27DC
  27. #define MGS 0x259F
  28. #define HA 0x2382
  29.  
  30. //define the length of each note (in milliseconds)
  31. #define DOTTED_HALF 750
  32. #define QUARTER 250
  33. #define HALF 500
  34. #define WHOLE 1000
  35.  
  36. #define delayTime 50 //post-note delay to avoid perceived overlap of notes (in milliseconds)
  37.  
  38.  
  39.  
  40. void setup() {
  41.   //Best Practice: Clear TCCR registers before setting-up
  42.   TCCR1A &= 0;
  43.   TCCR1B &= 0;
  44.  
  45.   DDRB = 0x0F; //sets PB3-PB0 as outputs
  46.  
  47. }
  48.  
  49. void loop() {
  50.   playNote(MB, QUARTER);
  51.   playNote(MA, QUARTER);
  52.   playNote(LG, QUARTER);
  53.   playNote(MA, QUARTER);
  54.   playNote(MB, QUARTER);
  55.   playNote(MB, QUARTER);
  56.   playNote(MB, HALF);
  57.   playNote(MA, QUARTER);
  58.   playNote(MA, QUARTER);
  59.   playNote(MA, HALF);
  60.   playNote(MB, QUARTER);
  61.   playNote(MD, QUARTER);
  62.   playNote(MD, HALF);
  63.   playNote(MB, QUARTER);
  64.   playNote(MA, QUARTER);
  65.   playNote(LG, QUARTER);
  66.   playNote(MA, QUARTER);
  67.   playNote(MB, QUARTER);
  68.   playNote(MB, QUARTER);
  69.   playNote(MB, QUARTER);
  70.   playNote(MB, QUARTER);
  71.   playNote(MA, QUARTER);
  72.   playNote(MA, QUARTER);
  73.   playNote(MB, QUARTER);
  74.   playNote(MA, QUARTER);
  75.   playNote(LG, WHOLE);
  76.  
  77.  
  78.  
  79. }
  80.  
  81.  
  82. void playNote(int max_count, int note_size) {
  83.  
  84. TCCR1A |= 0x40; //Sets Timer 1 Channel A to toggle on Compare Match
  85. TCCR1B |= 0x09; //Sets Timer 1 to CTC mode
  86.  
  87. OCR1A = max_count; //sets the period of the given note as the toggle point for Timer 1 Channel A
  88.  
  89. delay(note_size);
  90.  
  91. TCCR1B |= 0x08; //Turn off Timer 1's connection to the system clock
  92.  
  93. delay(delayTime);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement