Mediul de dezvoltare:
Functia setup : Initializeaza pinii digitali si pe cei de intrerupere:
Serial.begin(9600); // SETEAZA PINUL 5 CA OUTPUT (BUZZER) DDRD |= (1 << DDD5); // SETEAZA PINUL 4 CA OUTPUT (LED) DDRD |= (1 << DDD4); // SETEAZA PINUL 6 SI 7 CA INPUS DDRD &= ~((1 << DDD6) | (1 << DDD7)); PORTD |= ((1 << PORTD6) | (1 << PORTD7)); // SETEAZA PINUL 8-13 CA INPUS DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2) | (1 << DDB3) | (1 << DDB4) | (1 << DDB5)); PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2) | (1 << PORTB3) | (1 << PORTB4) | (1 << PORTB5)); // SETEAZA PINUL 2 SI 3 CA INPUT PENTRU INTRERUPERI DDRD &= ~((1 << DDD2) | (1 << DDD3)); PORTD |= ((1 << PORTD2) | (1 << PORTD3)); // CONFIGURARE INTRERUPERI EICRA |= (1 << ISC01); // PD2 EICRA |= (1 << ISC11); // PD3 EIMSK |= (1 << INT0) | (1 << INT1); //ACTIVAM INTRERUPERILE sei();
Functia loop : Verifica daca s-a apasat vreo clapa, si face reprezentarea unei octave, in cazul schimbarii acesteia
if (octave >= 0 && octave <= 2) { if (!(PINB & (1 << PINB5))) { tone(5, f0[octave * 8], 100); // PINUL 13 Serial.println("btn1 apasat."); _delay_ms(100); } if (!(PINB & (1 << PINB4))) { tone(5, f0[octave * 8 + 1], 100); // PINUL 12 Serial.println("btn2 apasat."); _delay_ms(100); }
if (!(PINB & (1 << PINB3))) { tone(5, f0[octave * 8 + 2], 100); // PINUL 11 Serial.println("btn3 apasat."); _delay_ms(100); } if (!(PINB & (1 << PINB2))) { tone(5, f0[octave * 8 + 3], 100); // PINUL 10 Serial.println("btn4 apasat."); _delay_ms(100); } if (!(PINB & (1 << PINB1))) { tone(5, f0[octave * 8 + 4], 100); // PINUL 9 Serial.println("btn5 apasat."); _delay_ms(100); } if (!(PINB & (1 << PINB0))) { tone(5, f0[octave * 8 + 5], 100); // PINUL 8 Serial.println("btn6 apasat."); _delay_ms(100); } if (!(PIND & (1 << PIND7))) { tone(5, f0[octave * 8 + 6], 100); // PINUL 7 Serial.println("btn7 apasat."); _delay_ms(100); } if (!(PIND & (1 << PIND6))) { tone(5, f0[octave * 8 + 7], 100); // PINUL 6 Serial.println("btn8 apasat."); _delay_ms(100); } //REPREZENTARE OCTAVA if(state == true){ if(octave == 0){ PORTD |= (1 << PORTD4); //APRINDEM LEDUL _delay_ms(100); PORTD &= ~(1 << PORTD4); //STINGEM LEDUL } else if (octave == 1){ PORTD |= (1 << PORTD4); _delay_ms(100); PORTD &= ~(1 << PORTD4); _delay_ms(100); PORTD |= (1 << PORTD4); _delay_ms(100); PORTD &= ~(1 << PORTD4); } else if (octave == 2){ PORTD |= (1 << PORTD4); _delay_ms(100); PORTD &= ~(1 << PORTD4); _delay_ms(100); PORTD |= (1 << PORTD4); _delay_ms(100); PORTD &= ~(1 << PORTD4); _delay_ms(100); PORTD |= (1 << PORTD4); _delay_ms(100); PORTD &= ~(1 << PORTD4); } Serial.println(octave); state = false; } } _delay_ms(10);
Intreruperile : Acestea sunt realizate pe butoanele ce schimba octavele, avand rolul de a creste performanta codului.
ISR(INT0_vect) { unsigned long currentTime = millis(); if (currentTime - lastDebounceTimeUp > debounceDelay) { octaveUp(); lastDebounceTimeUp = currentTime; } } ISR(INT1_vect) { unsigned long currentTime = millis(); if (currentTime - lastDebounceTimeDown > debounceDelay) { octaveDown(); lastDebounceTimeDown = currentTime; } } void octaveDown(void) { Serial.println("Interrupt for Octave Down triggered"); octave = max(0, octave - 1); Serial.println("OCT1 apasat."); state = true; _delay_ms(200); } void octaveUp(void) { Serial.println("Interrupt for Octave Up triggered"); octave = min(2, octave + 1); Serial.println("OCT2 apasat."); state = true; _delay_ms(200); }
Fişierele se încarcă pe wiki folosind facilitatea Add Images or other files. Namespace-ul în care se încarcă fişierele este de tipul :pm:prj20??:c? sau :pm:prj20??:c?:nume_student (dacă este cazul). Exemplu: Dumitru Alin, 331CC → :pm:prj2009:cc:dumitru_alin.