Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. /*
  2. * Project 2.c
  3. *
  4. * Created: 12/5/2019 1:52:18 PM
  5. * Author : hdm9yt
  6. */
  7.  
  8. #define F_CPU 16000000UL
  9. #include <avr/io.h>
  10. #include <avr/interrupt.h>
  11. #include <avr/delay.h>
  12. #define SPK PORTE4 //Defines portE4 for the speaker
  13.  
  14. //function defining
  15. void USART_Init(unsigned long);
  16. char USART_RxChar();
  17. void USART_TxChar(char);
  18. void Serial_String(char cstring[]);
  19. void Play_music(int music[][2]);
  20. void Play_sound(int length, int x);
  21. void my_delay();
  22. void menu();
  23. void jukebox();
  24. void keyboard();
  25. void Serialkeyboard();
  26.  
  27. //Global variables to store note values.
  28. int A = 0x0024;
  29. int B = 0x0020;
  30. int C = 0x001E;
  31. int D = 0x001B;
  32. int E = 0;
  33. int F = 0;
  34. int G = 0x0028;
  35. int rest = 0;
  36. int qrt=60;
  37.  
  38. int main(void)
  39. {
  40. DDRD = 0xFF;
  41. PORTD = 0xFF;
  42. PORTA = 0xFF;
  43. DDRA = 0x00;
  44. DDRE |= (1<<SPK);
  45. USART_Init(9600);
  46. menu();
  47. }
  48. void menu()
  49. {
  50. unsigned char value;
  51. Serial_String("Group N's Microcontrollers Project 2!\n");
  52. Serial_String("Select the mode you wish to go to:\n");
  53. Serial_String("0. Menu\n");
  54. Serial_String("1. Keyboard With Switches\n");
  55. Serial_String("2. Jukebox\n");
  56. Serial_String("3. Keyboard using Serial\n");
  57. while(1)
  58. {
  59. value = USART_RxChar();
  60. switch(value){
  61. case '0':
  62. menu();
  63. break;
  64. case '1':
  65. keyboard();
  66. break;
  67. case '2':
  68. jukebox();
  69. break;
  70. case '3':
  71. Serialkeyboard();
  72. break;
  73. }
  74. }
  75. }
  76. void jukebox()
  77. {
  78.  
  79. int jinglebells[26][2] = {
  80. {B,qrt},{B,qrt},{B,qrt*2},{B,qrt},{B,qrt},
  81. {B,qrt*2},{B,qrt},{D,qrt},{G,qrt*1.5},{A,qrt/2},
  82. {B,qrt*3},{C,qrt},{C,qrt},{C,qrt*3/2},{C,qrt/2},
  83. {C,qrt},{B,qrt},{B,qrt},{B,qrt/2},{B,qrt/2},
  84. {D,qrt},{D,qrt},{C,qrt},{A,qrt},{G,qrt*3},
  85. {0,0}
  86. };
  87. Play_music(jinglebells);
  88. unsigned char value;
  89. value = USART_RxChar();
  90. switch(value){
  91. case '0':
  92. menu();
  93. break;
  94. case '1':
  95. keyboard();
  96. break;
  97. case '2':
  98. jukebox();
  99. break;
  100. case '3':
  101. Serialkeyboard();
  102. break;
  103. }
  104. }
  105. void keyboard()
  106. {
  107. while (1)
  108. {
  109. if((~PINA & (1<<PINA0)) == (1<<PINA0)) //Checks to see if SW1 is pressed
  110. {
  111. PORTD &= ~(1 << PORTD0);
  112. Play_sound(A,1);
  113. }
  114.  
  115. if((~PINA & (1<<PINA1)) == (1<<PINA1)) //Checks to see if SW 7 is pressed
  116. {
  117. PORTD &= ~(1 << PORTD1);
  118. Play_sound(B,1); //outputs the updated time
  119. }
  120.  
  121. if((~PINA & (1<<PINA2)) == (1<<PINA2)) //checks to see if SW2 is pressed
  122. {
  123. PORTD &= ~(1 << PORTD2);
  124. Play_sound(C,1);
  125. }
  126.  
  127. if((~PINA & (1<<PINA3)) == (1<<PINA3)) //Checks to see if SW3 is pressed
  128. {
  129. PORTD &= ~(1 << PORTD3);
  130. Play_sound(D,1); //outputs the updated time
  131. }
  132.  
  133. if((~PINA & (1<<PINA5)) == (1<<PINA5)) //checks to see if SW9 is pressed
  134. {
  135. PORTD &= ~(1 << PORTD5);
  136. Play_sound(E,1); //outputs the updated time
  137. }
  138. if((~PINA & (1<<PINA6)) == (1<<PINA6)) //checks to see if SW9 is pressed
  139. {
  140. PORTD &= ~(1 << PORTD6);
  141. Play_sound(F,1); //outputs the updated time
  142. }
  143.  
  144. if((~PINA & (1<<PINA7)) == (1<<PINA7)) //checks to see if SW9 is pressed
  145. {
  146.  
  147. PORTD &= ~(1 << PORTD7);
  148. Play_sound(G,1); //outputs the updated time
  149. }
  150.  
  151. PORTD = 0xFF;
  152. unsigned char value;
  153. value = USART_RxChar();
  154. switch(value){
  155. case '0':
  156. menu();
  157. break;
  158. case '1':
  159. keyboard();
  160. break;
  161. case '2':
  162. jukebox();
  163. break;
  164. case '3':
  165. Serialkeyboard();
  166. break;
  167. }
  168. }
  169.  
  170. }
  171.  
  172. void Serialkeyboard(){
  173. while(1)
  174. {
  175. unsigned char value;
  176. value = USART_RxChar();
  177. switch(value){
  178. case 'a':
  179. Play_sound(A,qrt);
  180. break;
  181. case 'b':
  182. Play_sound(B,qrt);
  183. break;
  184. case 'c':
  185. Play_sound(C,qrt);
  186. break;
  187. case 'd':
  188. Play_sound(D,qrt);
  189. break;
  190. case '0':
  191. main();
  192. break;
  193. }
  194. }
  195.  
  196.  
  197. }
  198.  
  199.  
  200.  
  201. //Initializes USART function
  202. //Initializes USART1
  203. //Pre: Given an unsigned long for the baudrate
  204. //Post: Enables USART transmitter and receiver for the desired baud rate
  205. void USART_Init(unsigned long BAUDRATE)
  206. {
  207. // Enable USART transmitter and receiver
  208. UCSR1B = (1<<RXEN) | (1<<TXEN); //Sends a 1 to RXEN and TXEN
  209. // Write USCRC for 8 bit data and 1 stop bit
  210. UCSR1C |= (1<<UCSZ1) | (1<<UCSZ0); //Sends a 3 (11) to USCZ1
  211.  
  212. //Defines prescale value
  213. //stores prescale value in an integer
  214. int prescale =(F_CPU/(16*BAUDRATE))-1;
  215. // Sets UBRR1 register for desired baud rate
  216. UBRR1H = (unsigned char)(prescale>>8);
  217. UBRR1L = (unsigned char)prescale;
  218. }
  219.  
  220. // Data receiving function
  221. char USART_RxChar()
  222. {
  223. // checks to see if there is a character to receive
  224. // if not, it returns a null
  225. while( !(UCSR1A & (1<<RXC)) ) //Check to see if register is updated
  226. return;
  227. // if so, it returns it
  228. return UDR1;
  229. }
  230.  
  231. void USART_TxChar(char data)
  232. {
  233. UDR1 = data; //Write data to be transmit in UDR
  234.  
  235. // Wait until data transmit and buffer get empty
  236. while( !(UCSR1A & (1<<UDRE)) );
  237. }
  238.  
  239. void Serial_String(char cstring[])
  240. {
  241. for(int i=0;cstring[i]!='\0';i++)
  242. {
  243. USART_TxChar(cstring[i]);
  244. }
  245. }
  246.  
  247.  
  248. void Play_music(int music[][2])
  249. {
  250. for(int i=0;music[i][0]!=0;i++)
  251. {
  252. Play_sound(music[i][0], music[i][1]);
  253. my_delay(0xAF);
  254. }
  255. }
  256.  
  257. void Play_sound(int x, int length)
  258. {
  259. for(int i=0; i<length; i++)
  260. {
  261. PORTE |= (1<<PORTE4);
  262. my_delay(x);
  263. PORTE &= ~(1<<PORTE4);
  264. my_delay(x);
  265. }
  266. }
  267.  
  268. void my_delay(int x)
  269. {
  270. TCCR1A = 0x00;
  271. TCCR1B = 0b00000101;
  272. TCNT1= 0xFFFF - x;
  273. while((TIFR1 & (1<<TOV1)) == 0x00){}
  274. TIFR1 |= (1<<TOV1);
  275. TCCR1A = 0;
  276. TCCR1B = 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement