Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * David Welch and Joe Weber (DJ Spooky)
- * Professor Holcomb
- * ECE 353: Computer Systems Lab I
- * Project 2
- * MIDI Controller
- *
- */
- #include <io.h>
- #include <avr/interrupt.h>
- #include <avr/delay.h>
- #include <avr/eeprom.h>
- void initializeTimer()
- {
- OCR1A = 0x07A1; // COMPA Interrupt value (Should be 0x07A1, but is off by factor of 4)
- TCCR1B |= (1 << CS10); // Prescaler = 1024
- TCCR1B |= (1 << CS12);
- TIMSK |= (1 << OCIE1A); // Enable interrupt when Timer1 = OCR1A
- sei(); // Enable global interrupts
- }
- // Sets up different things as different stuff
- void setRegisters()
- {
- // I/O enables
- DDRA = 0x00; // Set PINA as input
- DDRB = 0xFF; // Set PORTB as output
- DDRD = 0x40; // Set the one pin on PORTD to output
- // Timer sets & enables
- UBRRH = 0x00; UBRRL = 0x07; // Set Universal Baud Rate Register to 7
- UCSRB |= (1 << RXEN); // Enable Receiver
- UCSRB |= (1 << TXEN); // Enable Transmitter
- // Analong to Digital Converter enables
- ADMUX |= (1 << MUX2); // Analog Channel & Gain Selection||Single Ended Input ADC7
- ADMUX |= (1 << MUX1); // ||
- ADMUX |= (1 << MUX0); // ||
- ADCSRA |= (1 << ADEN); // ADC Enable
- }
- // Called when TCNT1 = OCR1A
- ISR(TIMER1_COMPA_vect)
- {
- PORTB = 0x00; // Turn off the LED... Its been 500ms
- }
- int main(void)
- {
- initializeTimer(); // Initialize the timer
- setRegisters(); // Sets up different things as different stuff
- uint16_t light; // The digital light value (0 - 1024)
- uint16_t t; // TCNT1
- uint16_t gahbage; // Enables TCNT1H and TCNT1L to be read
- uint8_t* eepromAddress; // EEPROM Address (Byte addressable)
- uint8_t* eA; // EEPROM Address (Byte addressable)
- uint8_t status, data1, data2; // Define our 3 bytes of data
- uint8_t t1, t2; // First and 2nd timer bytes
- uint8_t nothing; // Filters out junk in the write UDR
- uint8_t flag = 0; // Determines if we were in record mode or not the previous loop
- uint8_t mod = 1; // Modulates the signal based on the light sensor data
- // Loop continuously
- while(1){
- // Mode 1: Recording
- // Mode 2: Playback
- // Mode 3: Playback & Modify (Mode 2 modifier)
- // RECORDING - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- if(PINA & (1 << 0)){
- // If we're coming into record from another function
- if(!flag){
- flag = 1; // Set flag
- eepromAddress = 1; // Reset stored MIDI data
- TCNT1 = 0; // Reset clock
- }
- if(UCSRA & (1 << RXC)){ // When there's data
- // We've got the first byte of data!
- status = UDR; // UDR = USART Data Register (RXB)
- // Wait for the 2nd byte
- while(!(UCSRA & (1 << RXC)));
- data1 = UDR; // Read the note
- // If the note is being played (as opposed to stopped)
- if (status != 0x80)
- PORTB = data1; // Make the LED bar correspond to the note
- // Wait for the 3rd byte
- while(!(UCSRA & (1 << RXC)));
- data2 = UDR; // Velocity data
- gahbage = TCNT1;
- t1 = TCNT1H; // Record the clock!
- t2 = TCNT1L;
- TCNT1 = 0; // Start the clock!
- // Write our data to EEPROM
- eeprom_write_byte(eepromAddress, status); // Write data
- eepromAddress++; // Increment address
- eeprom_write_byte(eepromAddress, data1);
- eepromAddress++;
- eeprom_write_byte(eepromAddress, data2);
- eepromAddress++;
- eeprom_write_byte(eepromAddress, t1);
- eepromAddress++;
- eeprom_write_byte(eepromAddress, t2);
- eepromAddress++;
- eeprom_write_byte(0, eepromAddress);
- }
- }
- // PLAYBACK - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- else if(PINA & (1 << 1)){
- mod = 1; // Assume regular light
- eA = 1; // Start from the first data address (eepromAddress is in slot1)
- // Loop through the stored data when the switch is on & there's more data
- while((eepromAddress - eA >= 4) && (PINA & (1 << 1))){
- // Read from MIDI;
- status = eeprom_read_byte(eA); // Read data
- eA++; // Incrament address
- data1 = eeprom_read_byte(eA);
- eA++;
- data2 = eeprom_read_byte(eA);
- eA++;
- t1 = eeprom_read_byte(eA);
- eA++;
- t2 = eeprom_read_byte(eA);
- eA++;
- eepromAddress = eeprom_read_byte(0); // Read the max address
- // Deduce time from 2 bytes to 1
- t = ((uint16_t)t1);
- t = (t << 8);
- t = t | ((uint16_t)t2);
- // If its our first time in the playback, don't wait for the first note
- if (eA == 6)
- t = 0;
- // PLAYBACK & MODIFY - - - - - - - - - - - - - - - - - - - - - - - - - -
- if(PINA & (1 << 2)){
- ADCSRA |= (1 << ADSC); // Ready to read from the ADC
- light = ADC; // Read from the ADC
- if(light <= (1 << 6)) // Bright light
- mod = 2;
- else if((light > (1 << 6)) && (light < (1 << 9))) // Medium light
- mod = 1;
- else // if(light >= (1 << 9)) // Dim light
- mod = 0;
- }
- // If there's bright light, half the time intervals
- if(mod == 2){
- t = (t >> 1);
- }
- // Wait for the time between notes or note duration
- TCNT1 = 0;
- while((TCNT1 != t) && (PINA & (1 << 1)));
- // If there's dim light, double the time intervals
- if(mod == 0){
- TCNT1 = 0x07A1; // Trigger the interrupt
- TCNT1 = 0;
- while((TCNT1 != t) && (PINA & (1 << 1)));
- }
- //Write to MIDI
- while(((UCSRA & (1 << UDRE)) == 0) && (PINA & (1 << 1))); // Wait for the computer to be ready to be written to
- UDR = status; // Send the status
- while(((UCSRA & (1 << UDRE)) == 0) && (PINA & (1 << 1)));
- UDR = data1;
- while(((UCSRA & (1 << UDRE)) == 0) && (PINA & (1 << 1)));
- UDR = data2;
- // If the note is being played (as opposed to stopped)
- if (status != 0x80)
- PORTB = data1; // Make the LED bar correspond to the note
- }
- // Wait before looping again immediately (500ms - 2s depending on mod)
- TCNT1 = 0;
- while((TCNT1 != 0x07A2) && (PINA & (1 << 1)));
- if(mod != 2){
- TCNT1 = 0;
- while((TCNT1 != 0x07A2) && (PINA & (1 << 1)));
- }
- if(mod == 0){
- TCNT1 = 0;
- while((TCNT1 != 0x07A2) && (PINA & (1 << 1)));
- TCNT1 = 0;
- while((TCNT1 != 0x07A2) && (PINA & (1 << 1)));
- }
- nothing = UDR; // Prevents accidentally writing into record
- flag = 0; // We're not in record
- }
- // NOTHING - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- else{
- nothing = UDR; // Prevents accidentally writing into record
- flag = 0; // We're not in record
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement