#define F_CPU 16000000 #include #include #include #include volatile unsigned long timer0_overflow_count; int32_t c; unsigned long ausgabe1; unsigned long ausgabe2; unsigned long ausgabe3; unsigned long i; char buffer[20]; // String der Laenge 20 Zeichen deklarieren. void sende (char buffer[]) // Funktionsaufruf zum Uebermitteln einer Zeichenfolge { for (int c=0; buffer[c]!=0; c++) // for-Schleife: Abbruch wenn Zeichenfolge { // zu Ende ist, sonst "c" inkrementieren while (bit_is_clear(UCSRA,5)) // Solange Zeichen noch nicht vollstaendig Uebermittelt. { ; // tue nichts! (warte) } UDR=buffer[c]; // Danach: Sende aktuelles ("c"-tes) Zeichen der Zeichenkette. } while (bit_is_clear(UCSRA,5)) // Solange Zeichen noch nicht vollstaendig Uebermittelt. { ; // tue nichts! (warte) } } ISR (TIMER0_OVF_vect) //Overflow Interrupt { timer0_overflow_count ++; //Overflow Zähler } void startTimer() { timer0_overflow_count = 0l; TCNT0 = 0; i = 0l; } void Zeitcheck() { timer0_overflow_count = timer0_overflow_count << 8; timer0_overflow_count = timer0_overflow_count + TCNT0; timer0_overflow_count = timer0_overflow_count << 2; i = timer0_overflow_count; } long time1(int maske) { unsigned long count = 0l; unsigned long total = 0l; while(i < 40000) Zeitcheck(); { DDRB = 0b11111001; PORTB = 0b11111000; DDRB = 0b11111000; PORTB = 0b11111000; while(!(PINB & maske)) count++; total++; } startTimer(); count = count << 8; count = count / total; return count; } long time2(int maske) { unsigned long count = 0l; unsigned long total = 0l; while(i < 40000) Zeitcheck(); { DDRB = 0b11111010; PORTB = 0b11111000; DDRB = 0b11111000; PORTB = 0b11111000; while(!(PINB & maske)) count++; total++; } startTimer(); count = count << 8; count = count / total; return count; } long time3(int maske) { unsigned long count = 0l; unsigned long total = 0l; while(i < 40000) Zeitcheck(); { DDRB = 0b11111100; PORTB = 0b11111000; DDRB = 0b11111000; PORTB = 0b11111000; while(!(PINB & maske)) count++; total++; } startTimer(); count = count << 8; count = count / total; return count; } int main(void) { UBRRL = 8; //Quarz: 3.6864MHz, Baudrate 9600 UCSRB = 0b10011000; //Empfänger, Sender ein UCR = UCSRB DDRB = 0b11111000; //PinB 0-2 als Eingang 3-7 als Ausgang PORTB = 0b00000000; TIMSK = 0b00000001; //Interrupt OVF 0 enable TCCR0 = 0b00000001; //Prescaler 1024 Timer Start sei () ; //Interrupts erlauben startTimer(); //Springe zu UP while(1) { ausgabe1 = time1(1<