Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project 2.c
- *
- * Created: 12/5/2019 1:52:18 PM
- * Author : hdm9yt
- */
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/delay.h>
- #define SPK PORTE4 //Defines portE4 for the speaker
- //function defining
- void USART_Init(unsigned long);
- char USART_RxChar();
- void USART_TxChar(char);
- void Serial_String(char cstring[]);
- void Play_music(int music[][2]);
- void Play_sound(int length, int x);
- void my_delay();
- void menu();
- void jukebox();
- void keyboard();
- void Serialkeyboard();
- //Global variables to store note values.
- int A = 0x0024;
- int B = 0x0020;
- int C = 0x001E;
- int D = 0x001B;
- int E = 0;
- int F = 0;
- int G = 0x0028;
- int rest = 0;
- int qrt=60;
- int main(void)
- {
- DDRD = 0xFF;
- PORTD = 0xFF;
- PORTA = 0xFF;
- DDRA = 0x00;
- DDRE |= (1<<SPK);
- USART_Init(9600);
- menu();
- }
- void menu()
- {
- unsigned char value;
- Serial_String("Group N's Microcontrollers Project 2!\n");
- Serial_String("Select the mode you wish to go to:\n");
- Serial_String("0. Menu\n");
- Serial_String("1. Keyboard With Switches\n");
- Serial_String("2. Jukebox\n");
- Serial_String("3. Keyboard using Serial\n");
- while(1)
- {
- value = USART_RxChar();
- switch(value){
- case '0':
- menu();
- break;
- case '1':
- keyboard();
- break;
- case '2':
- jukebox();
- break;
- case '3':
- Serialkeyboard();
- break;
- }
- }
- }
- void jukebox()
- {
- int jinglebells[26][2] = {
- {B,qrt},{B,qrt},{B,qrt*2},{B,qrt},{B,qrt},
- {B,qrt*2},{B,qrt},{D,qrt},{G,qrt*1.5},{A,qrt/2},
- {B,qrt*3},{C,qrt},{C,qrt},{C,qrt*3/2},{C,qrt/2},
- {C,qrt},{B,qrt},{B,qrt},{B,qrt/2},{B,qrt/2},
- {D,qrt},{D,qrt},{C,qrt},{A,qrt},{G,qrt*3},
- {0,0}
- };
- Play_music(jinglebells);
- unsigned char value;
- value = USART_RxChar();
- switch(value){
- case '0':
- menu();
- break;
- case '1':
- keyboard();
- break;
- case '2':
- jukebox();
- break;
- case '3':
- Serialkeyboard();
- break;
- }
- }
- void keyboard()
- {
- while (1)
- {
- if((~PINA & (1<<PINA0)) == (1<<PINA0)) //Checks to see if SW1 is pressed
- {
- PORTD &= ~(1 << PORTD0);
- Play_sound(A,1);
- }
- if((~PINA & (1<<PINA1)) == (1<<PINA1)) //Checks to see if SW 7 is pressed
- {
- PORTD &= ~(1 << PORTD1);
- Play_sound(B,1); //outputs the updated time
- }
- if((~PINA & (1<<PINA2)) == (1<<PINA2)) //checks to see if SW2 is pressed
- {
- PORTD &= ~(1 << PORTD2);
- Play_sound(C,1);
- }
- if((~PINA & (1<<PINA3)) == (1<<PINA3)) //Checks to see if SW3 is pressed
- {
- PORTD &= ~(1 << PORTD3);
- Play_sound(D,1); //outputs the updated time
- }
- if((~PINA & (1<<PINA5)) == (1<<PINA5)) //checks to see if SW9 is pressed
- {
- PORTD &= ~(1 << PORTD5);
- Play_sound(E,1); //outputs the updated time
- }
- if((~PINA & (1<<PINA6)) == (1<<PINA6)) //checks to see if SW9 is pressed
- {
- PORTD &= ~(1 << PORTD6);
- Play_sound(F,1); //outputs the updated time
- }
- if((~PINA & (1<<PINA7)) == (1<<PINA7)) //checks to see if SW9 is pressed
- {
- PORTD &= ~(1 << PORTD7);
- Play_sound(G,1); //outputs the updated time
- }
- PORTD = 0xFF;
- unsigned char value;
- value = USART_RxChar();
- switch(value){
- case '0':
- menu();
- break;
- case '1':
- keyboard();
- break;
- case '2':
- jukebox();
- break;
- case '3':
- Serialkeyboard();
- break;
- }
- }
- }
- void Serialkeyboard(){
- while(1)
- {
- unsigned char value;
- value = USART_RxChar();
- switch(value){
- case 'a':
- Play_sound(A,qrt);
- break;
- case 'b':
- Play_sound(B,qrt);
- break;
- case 'c':
- Play_sound(C,qrt);
- break;
- case 'd':
- Play_sound(D,qrt);
- break;
- case '0':
- main();
- break;
- }
- }
- }
- //Initializes USART function
- //Initializes USART1
- //Pre: Given an unsigned long for the baudrate
- //Post: Enables USART transmitter and receiver for the desired baud rate
- void USART_Init(unsigned long BAUDRATE)
- {
- // Enable USART transmitter and receiver
- UCSR1B = (1<<RXEN) | (1<<TXEN); //Sends a 1 to RXEN and TXEN
- // Write USCRC for 8 bit data and 1 stop bit
- UCSR1C |= (1<<UCSZ1) | (1<<UCSZ0); //Sends a 3 (11) to USCZ1
- //Defines prescale value
- //stores prescale value in an integer
- int prescale =(F_CPU/(16*BAUDRATE))-1;
- // Sets UBRR1 register for desired baud rate
- UBRR1H = (unsigned char)(prescale>>8);
- UBRR1L = (unsigned char)prescale;
- }
- // Data receiving function
- char USART_RxChar()
- {
- // checks to see if there is a character to receive
- // if not, it returns a null
- while( !(UCSR1A & (1<<RXC)) ) //Check to see if register is updated
- return;
- // if so, it returns it
- return UDR1;
- }
- void USART_TxChar(char data)
- {
- UDR1 = data; //Write data to be transmit in UDR
- // Wait until data transmit and buffer get empty
- while( !(UCSR1A & (1<<UDRE)) );
- }
- void Serial_String(char cstring[])
- {
- for(int i=0;cstring[i]!='\0';i++)
- {
- USART_TxChar(cstring[i]);
- }
- }
- void Play_music(int music[][2])
- {
- for(int i=0;music[i][0]!=0;i++)
- {
- Play_sound(music[i][0], music[i][1]);
- my_delay(0xAF);
- }
- }
- void Play_sound(int x, int length)
- {
- for(int i=0; i<length; i++)
- {
- PORTE |= (1<<PORTE4);
- my_delay(x);
- PORTE &= ~(1<<PORTE4);
- my_delay(x);
- }
- }
- void my_delay(int x)
- {
- TCCR1A = 0x00;
- TCCR1B = 0b00000101;
- TCNT1= 0xFFFF - x;
- while((TIFR1 & (1<<TOV1)) == 0x00){}
- TIFR1 |= (1<<TOV1);
- TCCR1A = 0;
- TCCR1B = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement