1.//
2.// CreatingSound3.C
3.// Austin - EN
4.//
5.
6. #include <p18f1320.h>
7. #include <delays.h>
8. #include <adc.h>
9. #pragma config OSC=INTIO2, WDT=OFF, LVP=OFF, DEBUG=ON
10. #define PAUSE Delay1KTCYx(50);
11.
12. rom const unsigned int songs[6][80] = {
13. {0, 5, 7, 8, 10, 8, 0, 0, 5, 7, 8, 0, 8, 5, 12, 10, 12, 17, 19, 20, 22, 20, 12, 12, 17, 19, 20, 12, 20, 17, 24, 22, 12, 12, 17, 19, 20, 17, 24, 20, 29, 17, 17, 20, 19, 17, 24, 20, 17, 12, 12, 17, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
14. {500, 1000, 1500, 250, 250, 1000, 500, 500, 1500, 250, 250, 250, 250, 250, 250, 1500, 500, 1000, 1500, 250, 250, 1000, 500, 500, 1500, 250, 250, 250, 250, 250, 250, 1500, 250, 250, 500, 250, 250, 250, 250, 250, 1000, 250, 250, 250, 250, 250, 1500, 250, 250, 500, 500, 1500, 500, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
15. {7, 9, 10, 9, 10, 12, 14, 15, 14, 12, 10, 14, 15, 14, 19, 17, 19, 12, 14, 15, 14, 30, 10, 12, 14, 12, 10, 14, 9, 10, 9, 9, 10, 12, 10, 9, 7, 9, 7, 6, 10, 9, 10, 9, 7, 6, 14, 7, 12, 10, 9, 14, 15, 17, 15, 14, 12, 9, 7, 6, 7, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
16. {500, 500, 500, 250, 500, 500, 500, 500, 500, 250, 500, 500, 500, 500, 500, 250, 500, 500, 500, 500, 1500, 500, 500, 500, 500, 250, 500, 1500, 500, 250, 500, 500, 500, 500, 500, 250, 500, 500, 500, 500, 1000, 500, 500, 250, 500, 1000, 500, 500, 250, 500, 1500, 500, 250, 500, 500, 250, 500, 1500, 250, 500, 1500, 5000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
17. {5, 5, 5, 3, 5, 7, 9, 9, 10, 7, 9, 10, 14, 2, 14, 9, 10, 12, 14, 14, 158, 12, 12, 14, 7, 3, 5, 5, 12, 12, 12, 12, 10, 9, 10, 10, 10, 10, 9, 7, 7, 9, 9, 2, 17, 17, 17, 17, 15, 14, 15, 15, 15, 15, 14, 12, 9, 10, 9, 7, 5, 9, 10, 12, 14, 7, 5, 5, 14, 15, 14, 12, 10, 9, 7, 5, 7, 9, 9, 20},
18. {500, 500, 500, 1500, 250, 500, 500, 500, 500, 1500, 250, 500, 500, 500, 500, 1500, 250, 500, 500, 500, 500, 1500, 250, 500, 500, 1000, 250, 1500, 500, 500, 500, 1500, 250, 500, 500, 500, 500, 1500, 250, 500, 500, 500, 250, 1500, 500, 500, 500, 1500, 250, 500, 500, 500, 500, 1500, 250, 500, 500, 250, 250, 250, 250, 1500, 250, 500, 500, 1500, 250, 1500, 500, 250, 250, 250, 250, 1500, 250, 500, 500, 1500, 250, 1500}
19. };
20.
21.void playNote(unsigned int note, unsigned int timer) // Note & Timer driver; determines which note will be played and for how long
22. {
23. unsigned int times, freq, notes[] = {58725, 59107, 59468, 59809, 60131, 60434, 60720, 60990, 61245, 61486, 61714, 61928, 62131, 62322, 62502, 62672, 62833, 62985, 63128, 63263, 63391, 63511, 63625, 63732, 63833, 63929, 64019, 64104, 64104, 64184, 64260, 65536};
24. freq = notes[note]; // Determine frequency based on array value
25. INTCONbits.TMR0IF = 0; // Clear OVF Flag
26. LATA = 0x01; // Initialize LATA
27. TMR0L = freq & 0xFF;
28. TMR0H = (freq >> 8) & 0xFF;
29. T0CON|=0x80;
30.
31. for(times=timer;times>0;times--) // Duration of playing
32. {
33. TMR0H = (freq >> 8) & 0xFF; //Load TMR0H byte first
34. TMR0L = freq & 0xFF; // Load TMR0L byte next
35. while(!INTCONbits.TMR0IF); // Wait for timer
36.` INTCONbits.TMR0IF = 0; // Clear OVF Flag
37. LATA = ~LATA; // Invert output
38. }
39. PAUSE;
40. }
41.
42.void playSong(unsigned int song)
43.{
44. int time, note, counter;
45.
46.while(1)
47.{
48. if(song==0) // Song 1
49. {
50. for(counter=0;counter<53;counter++)
51. {
52. note = songs[0][counter]; // Song 1 notes
53. time = songs[1][counter]; // Song 1 time values
54. playNote(note, time); // Play through notes and times
55. }
56. break; // When song 1 finishes, break
57. }
58.
59. else if(song==1) // Song 2
60. {
61. for(counter=0;counter<63;counter++)
62. {
63. note = songs[2][counter]; // Song 2 notes
64. time = songs[3][counter]; // Song 2 time values
65. playNote(note, time); // Play through notes and times
66. }
67. break; // When song 2 finishes, break
68. }
69.
70. else if(song==2) // Song 3
71. {
72. for(counter=0;counter<80;counter++)
73. {
74. note = songs[4][counter]; // Song 3 notes
75. time = songs[5][counter]; // Song 3 time values
76. playNote(note, time); // Play through notes and times
77. }
78. break; // When song 3 finishes, break
79. }
80. else
81. {
82. LATB = 0x08; // Otherwise, LED 4
83. }
84. }
85.}
86.
87.
88. void main()
89. {
90. ADCON1 = 0x0E; // ADC Settings as digital inputs from RA1-RA3
91. OSCCONbits.IRCF0=1;
92. OSCCONbits.IRCF1=1;
93. OSCCONbits.IRCF2=1;
94. T0CONbits.TMR0ON = 0; // Don't turn timer on yet
95. T0CONbits.T08BIT = 0; // Timer0 is configured as 16-bit timer
96. T0CONbits.T0CS = 0; // Use internal clock
97. T0CONbits.PSA = 1; // Prescaler is not assigned
98. T0CONbits.T0PS2 = 0;
99. T0CONbits.T0PS1 = 0;
100. T0CONbits.T0PS0 = 0;
101. while(!OSCCONbits.IOFS);
102. TRISA = 0xFE;
103. TRISB = 0xF0;
104.
105. switch(type)
106. {
107. case 0:
108. {
109. LATB = 0x01;
110. playSong(0);
111. }
112. case 1:
113. {
114. LATB = 0x02;
115. playSong(1);
116. }
117. case 2:
118. {
119. LATB = 0x04;
120. playSong(2);
121. }
122. case 3:
123. {
124. LATB = 0x08;
125. }
126. }
127.
128. while(1)
129. {
130. if(!PORTAbits.RA1) // Switch 1
131. {
132. type 0;
133. }
134. else if(!PORTAbits.RA2) // Switch 2
135. {
136. type 1;
137. }
138. else if(!PORTAbits.RA3) // Switch 3
139. {
140. type 2;
141. }
142. else
143. {
144. type 3;
145. }
146. }
147.}