Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.22 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.         if((~PINA & (1<<PINA7)) == (1<<PINA7))  //checks to see if SW9 is pressed
  166.         {
  167.             PORTD &= ~(1 << PORTD7);
  168.             Serial_String("G, ");
  169.             while((~PINA & (1<<PINA4)) ==(1<<PINA4)){
  170.                 Play_sound(G,1);
  171.             }
  172.         }
  173.        
  174.         PORTD = 0xFF;
  175.         unsigned char value;
  176.         value = USART_RxChar();
  177.         switch(value){
  178.             case '0':
  179.             menu();
  180.             break;
  181.             case '1':
  182.             keyboard();
  183.             break;
  184.             case '2':
  185.             jukebox();
  186.             break;
  187.             case '3':
  188.             Serialkeyboard();
  189.             break;
  190.         }
  191.     }
  192.    
  193. }
  194.  
  195. void Octave_Jump(int sw){
  196.     if(sw == 0)
  197.     {
  198.         A*=2;
  199.         B*=2;
  200.         C*=2;
  201.         D*=2;
  202.         E*=2;
  203.         F*=2;
  204.         G*=2;
  205.         qrt/=2;
  206.     }
  207.     if(sw == 1)
  208.     {
  209.         A/=2;
  210.         B/=2;
  211.         C/=2;
  212.         D/=2;
  213.         E/=2;
  214.         F/=2;
  215.         G/=2;
  216.         qrt*=2;
  217.     }
  218. }
  219.  
  220. void Serialkeyboard(){
  221.     while(1)
  222.     {
  223.         unsigned char value;
  224.         value = USART_RxChar();
  225.         switch(value){
  226.             case 'a':
  227.             Play_sound(A,qrt);
  228.             break;
  229.             case 'b':
  230.             Play_sound(B,qrt);
  231.             break;
  232.             case 'c':
  233.             Play_sound(C,qrt);
  234.             break;
  235.             case 'd':
  236.             Play_sound(D,qrt);
  237.             break;
  238.             case 'e':
  239.             Play_sound(E,qrt);
  240.             break;
  241.             case 'f':
  242.             Play_sound(F,qrt);
  243.             break;
  244.             case 'g':
  245.             Play_sound(G,qrt);
  246.             break;
  247.             case '0':
  248.             main();
  249.             break;
  250.         }
  251.     }
  252. }
  253.  
  254.  
  255.  
  256. //Initializes USART function
  257. //Initializes USART1
  258. //Pre: Given an unsigned long for the baudrate
  259. //Post: Enables USART transmitter and receiver for the desired baud rate
  260. void USART_Init(unsigned long BAUDRATE)
  261. {
  262.     // Enable USART transmitter and receiver
  263.     UCSR1B = (1<<RXEN) | (1<<TXEN); //Sends a 1 to RXEN and TXEN
  264.     // Write USCRC for 8 bit data and 1 stop bit
  265.     UCSR1C |= (1<<UCSZ1) | (1<<UCSZ0);  //Sends a 3 (11) to USCZ1
  266.    
  267.     //Defines prescale value
  268.     //stores prescale value in an integer
  269.     int prescale =(F_CPU/(16*BAUDRATE))-1;
  270.     // Sets UBRR1 register for desired baud rate
  271.     UBRR1H = (unsigned char)(prescale>>8);
  272.     UBRR1L = (unsigned char)prescale;
  273. }
  274.  
  275. // Data receiving function
  276. char USART_RxChar()
  277. {
  278.     // checks to see if there is a character to receive
  279.     // if not, it returns a null
  280.     while( !(UCSR1A & (1<<RXC)) ) //Check to see if register is updated
  281.     return;
  282.     // if so, it returns it
  283.     return UDR1;
  284. }
  285.  
  286. void USART_TxChar(char data)
  287. {
  288.     UDR1 = data; //Write data to be transmit in UDR
  289.    
  290.     // Wait until data transmit and buffer get empty
  291.     while( !(UCSR1A & (1<<UDRE)) );
  292. }
  293.  
  294. void Serial_String(char cstring[])
  295. {
  296.     for(int i=0;cstring[i]!='\0';i++)
  297.     {
  298.         USART_TxChar(cstring[i]);
  299.     }
  300. }
  301.  
  302. void Play_music(int music[][2])
  303. {
  304.     for(int i=0;music[i][0]!=0;i++)
  305.     {
  306.         Play_sound(music[i][0], music[i][1]);
  307.         my_delay(0xAF);
  308.     }
  309. }
  310.  
  311. void Play_sound(int x, int length)
  312. {
  313.     for(int i=0; i<length; i++)
  314.     {
  315.         PORTE |= (1<<PORTE4);
  316.         my_delay(x);
  317.         PORTE &= ~(1<<PORTE4);
  318.         my_delay(x);
  319.     }  
  320. }
  321.  
  322. void my_delay(int x)
  323. {
  324.     TCCR1A = 0x00;
  325.     TCCR1B = 0b00000101;
  326.     TCNT1= 0xFFFF - x;
  327.     while((TIFR1 & (1<<TOV1)) == 0x00){}
  328.     TIFR1 |= (1<<TOV1);
  329.     TCCR1A = 0;
  330.     TCCR1B = 0;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement