Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.23 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 = 0x0018;
  33. int F = 0x0016;
  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.  
  49. void menu()
  50. {
  51.     unsigned char value;
  52.     Serial_String("Group 32's Microcontrollers Project 2!\n");
  53.     Serial_String("Select the mode you wish to go to:\n");
  54.     Serial_String("0. Menu\n");
  55.     Serial_String("1. Keyboard With Switches\n");
  56.     Serial_String("2. Jukebox\n");
  57.     Serial_String("3. Keyboard using Serial\n");
  58.     while(1)
  59.     {
  60.         value = USART_RxChar();
  61.         switch(value){
  62.         case '0':
  63.         menu();
  64.         break;
  65.         case '1':
  66.         keyboard();
  67.         break;
  68.         case '2':
  69.         jukebox();
  70.         break;
  71.         case '3':
  72.         Serialkeyboard();
  73.         break;
  74.         }
  75.     }
  76. }
  77.  
  78. void jukebox()
  79. {
  80.    
  81.     int jinglebells[26][2] = {
  82.         {B,qrt},{B,qrt},{B,qrt*2},{B,qrt},{B,qrt},
  83.         {B,qrt*2},{B,qrt},{D,qrt},{G,qrt*1.5},{A,qrt/2},
  84.         {B,qrt*3},{C,qrt},{C,qrt},{C,qrt*3/2},{C,qrt/2},
  85.         {C,qrt},{B,qrt},{B,qrt},{B,qrt/2},{B,qrt/2},
  86.         {D,qrt},{D,qrt},{C,qrt},{A,qrt},{G,qrt*3},
  87.         {0,0}
  88.     };
  89.     Play_music(jinglebells);
  90.     unsigned char value;
  91.     value = USART_RxChar();
  92.     switch(value){
  93.         case '0':
  94.         menu();
  95.         break;
  96.         case '1':
  97.         keyboard();
  98.         break;
  99.         case '2':
  100.         jukebox();
  101.         break;
  102.         case '3':
  103.         Serialkeyboard();
  104.         break;
  105.     }
  106. }
  107.  
  108. void keyboard()
  109. {
  110.     Serial_String("Welcome to the serial keyboard.\nEnter a letter between a-g to play that note\n");
  111.     while (1)
  112.     {
  113.         if((~PINA & (1<<PINA0)) == (1<<PINA0)) //Checks to see if SW1 is pressed
  114.         {
  115.             PORTD &= ~(1 << PORTD0);
  116.             Serial_String("A, ");
  117.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  118.                 Play_sound(A,1);
  119.             }
  120.         }
  121.        
  122.         if((~PINA & (1<<PINA1)) == (1<<PINA1))  //Checks to see if SW2 is pressed
  123.         {
  124.             PORTD &= ~(1 << PORTD1);
  125.             Serial_String("B, ");
  126.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  127.                 Play_sound(B,1);
  128.             }
  129.         }
  130.        
  131.         if((~PINA & (1<<PINA2)) == (1<<PINA2))  //checks to see if SW3 is pressed
  132.         {
  133.             PORTD &= ~(1 << PORTD2);
  134.             Serial_String("C, ");
  135.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  136.                 Play_sound(C,1);
  137.             }
  138.         }
  139.        
  140.         if((~PINA & (1<<PINA4)) == (1<<PINA4)) //Checks to see if SW4 is pressed
  141.         {
  142.             PORTD &= ~(1 << PORTD4);
  143.             Serial_String("D, ");
  144.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  145.                 Play_sound(D,1);
  146.             }
  147.         }
  148.        
  149.         if((~PINA & (1<<PINA5)) == (1<<PINA5))  //checks to see if SW7 is pressed
  150.         {
  151.             PORTD &= ~(1 << PORTD5);
  152.             Serial_String("E, ");
  153.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  154.                 Play_sound(E,1);  
  155.             }
  156.         }
  157.         if((~PINA & (1<<PINA6)) == (1<<PINA6))  //checks to see if SW8 is pressed
  158.         {
  159.             PORTD &= ~(1 << PORTD6);
  160.             Serial_String("F, ");
  161.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  162.                 Play_sound(F,1);
  163.             }
  164.         }
  165.        
  166.         if((~PINA & (1<<PINA7)) == (1<<PINA7))  //checks to see if SW9 is pressed
  167.         {
  168.             PORTD &= ~(1 << PORTD7);
  169.             Serial_String("G, ");
  170.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  171.                 Play_sound(G,1);
  172.             }
  173.         }
  174.        
  175.         PORTD = 0xFF;
  176.         unsigned char value;
  177.         value = USART_RxChar();
  178.         switch(value){
  179.             case '0':
  180.             menu();
  181.             break;
  182.             case '1':
  183.             keyboard();
  184.             break;
  185.             case '2':
  186.             jukebox();
  187.             break;
  188.             case '3':
  189.             Serialkeyboard();
  190.             break;
  191.         }
  192.     }
  193.    
  194. }
  195.  
  196. void Octave_Jump(int sw){
  197.     if(sw == 0)
  198.     {
  199.         A*=2;
  200.         B*=2;
  201.         C*=2;
  202.         D*=2;
  203.         E*=2;
  204.         F*=2;
  205.         G*=2;
  206.         qrt/=2;
  207.     }
  208.     if(sw == 1)
  209.     {
  210.         A/=2;
  211.         B/=2;
  212.         C/=2;
  213.         D/=2;
  214.         E/=2;
  215.         F/=2;
  216.         G/=2;
  217.         qrt*=2;
  218.     }
  219. }
  220.  
  221. void Serialkeyboard(){
  222.     while(1)
  223.     {
  224.         unsigned char value;
  225.         value = USART_RxChar();
  226.         switch(value){
  227.             case 'a':
  228.             Play_sound(A,qrt);
  229.             break;
  230.             case 'b':
  231.             Play_sound(B,qrt);
  232.             break;
  233.             case 'c':
  234.             Play_sound(C,qrt);
  235.             break;
  236.             case 'd':
  237.             Play_sound(D,qrt);
  238.             break;
  239.             case 'e':
  240.             Play_sound(E,qrt);
  241.             break;
  242.             case 'f':
  243.             Play_sound(F,qrt);
  244.             break;
  245.             case 'g':
  246.             Play_sound(G,qrt);
  247.             break;
  248.             case '0':
  249.             main();
  250.             break;
  251.         }
  252.     }
  253. }
  254.  
  255. //Initializes USART function
  256. //Initializes USART1
  257. //Pre: Given an unsigned long for the baudrate
  258. //Post: Enables USART transmitter and receiver for the desired baud rate
  259. void USART_Init(unsigned long BAUDRATE)
  260. {
  261.     // Enable USART transmitter and receiver
  262.     UCSR1B = (1<<RXEN) | (1<<TXEN); //Sends a 1 to RXEN and TXEN
  263.     // Write USCRC for 8 bit data and 1 stop bit
  264.     UCSR1C |= (1<<UCSZ1) | (1<<UCSZ0);  //Sends a 3 (11) to USCZ1
  265.    
  266.     //Defines prescale value
  267.     //stores prescale value in an integer
  268.     int prescale =(F_CPU/(16*BAUDRATE))-1;
  269.     // Sets UBRR1 register for desired baud rate
  270.     UBRR1H = (unsigned char)(prescale>>8);
  271.     UBRR1L = (unsigned char)prescale;
  272. }
  273.  
  274. // Data receiving function
  275. char USART_RxChar()
  276. {
  277.     // checks to see if there is a character to receive
  278.     // if not, it returns a null
  279.     while( !(UCSR1A & (1<<RXC)) ) //Check to see if register is updated
  280.     return;
  281.     // if so, it returns it
  282.     return UDR1;
  283. }
  284.  
  285. void USART_TxChar(char data)
  286. {
  287.     UDR1 = data; //Write data to be transmit in UDR
  288.    
  289.     // Wait until data transmit and buffer get empty
  290.     while( !(UCSR1A & (1<<UDRE)) );
  291. }
  292.  
  293. void Serial_String(char cstring[])
  294. {
  295.     for(int i=0;cstring[i]!='\0';i++)
  296.     {
  297.         USART_TxChar(cstring[i]);
  298.     }
  299. }
  300.  
  301. void Play_music(int music[][2])
  302. {
  303.     for(int i=0;music[i][0]!=0;i++)
  304.     {
  305.         Play_sound(music[i][0], music[i][1]);
  306.         my_delay(0xAF);
  307.     }
  308. }
  309.  
  310. void Play_sound(int x, int length)
  311. {
  312.     for(int i=0; i<length; i++)
  313.     {
  314.         PORTE |= (1<<PORTE4);
  315.         my_delay(x);
  316.         PORTE &= ~(1<<PORTE4);
  317.         my_delay(x);
  318.     }  
  319. }
  320.  
  321. void my_delay(int x)
  322. {
  323.     TCCR1A = 0x00;
  324.     TCCR1B = 0b00000101;
  325.     TCNT1= 0xFFFF - x;
  326.     while((TIFR1 & (1<<TOV1)) == 0x00){}
  327.     TIFR1 |= (1<<TOV1);
  328.     TCCR1A = 0;
  329.     TCCR1B = 0;
  330.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement