Also es ist mittlerweile so weit gediehen, dass immer wieder übertragen wird. Manchmal scheint das ganze aber zu hängen, sprich der Master sendet nach einer Übertragung kein Stoppsignal mehr. Auf welche Fehlerquellen könnte das deuten?
Hier nun die Codes für Master:
Und für den Slave:Code:#include <avr/io.h> #include <avr/interrupt.h> #include "i2cmaster.h" #include <compat/twi.h> void timer_init(void){ TCCR0 = (1 << CS02); //Frequenzteilung durch 256 } void main(void){ DDRD = 0x00; //PORTD als Eingang PORTD = 0xFF; // Pull-Up Widerstände an Port D aktiviert -> Schalter als Active-Low ausführen DDRB = 0xFF; PORTB = 0x00; //hier hängen LEDs dran, um eventuell beim Fehlersuchen zu helfen unsigned int overflow_counter = 0; unsigned char goal1[2]; goal1[0] = 0; goal1[1] = 0; timer_init(); i2c_init(); PORTB = (1 <<PB0); for(;;){ PORTB = 0x00; if ( TIFR & (1<<TOV0) ) {//wenn die timer/counter0 overflow Fahne '1' ist, dass ist immer wenn 1/152 sec erreicht sind (10Mhz/(256*256)) TIFR |= (1<<TOV0) ; //Overflow Bit wieder auf null setzen overflow_counter++; //overflow counter inkrementieren } if (!(PIND & (1 << PD7)) && (overflow_counter > 100)){ PORTB = (1<<PB0); goal[1]++; i2c_start_wait(0x02+I2C_WRITE); // set device address and write mode i2c_write(0x02); // write address = 2 i2c_write(goal1[1]); // write value goal[i] to Slave i2c_stop(); i++; overflow_counter = 0; } } } //end of bedienfeld.cCode://clockspeed 4Mhz -> /* Dies ist das C-File für die Anzeigensteuerung, das I2C wird über einen Interrupt ausgelöst*/ #include <avr/io.h> #include "show_digits.h" //File für die Umwandlung von Zehner- und Einserdezimal in BCD #include <compat/twi.h> #include <avr/interrupt.h> #include "i2cnew/twislave.h" #include <util/twi.h> volatile unsigned char goal1 = 0; volatile unsigned char goal2 = 0; void timer_init(void){ TCCR0 = (1 << CS00); //Timer / Counter Control Register is set to devide by 8 } void main(void){ int overflow_counter = 0; //overflow counter zählt die counter bit overflows int pin_counter = 0; DDRB = 0xFF; PORTB = 0x00; DDRD = 0xFF; //Alle PortD Pins auf Ausgang gesetzt (PD0-3 für BCD, PD4-7 für Ziffer 1-4) PORTD = 0x00; //Alles auf Low, Anzeige aus timer_init(); //timer wird gestartet int i = 0; init_twi_slave(0x02); //Alles für TWI Übertragung fertig machen rxbuffer[6] = 78; rxbuffer[2] = 00; for(;;){ PORTB = 0x00; goal1 = rxbuffer[2]; if ( TIFR & (1<<TOV0) ) {//wenn die timer/counter0 overflow Fahne '1' ist, dass ist immer wenn 1/195 sec erreicht sind (4Mhz/(8*256)) TIFR |= (1<<TOV0) ; //Overflow Bit wieder auf null setzen overflow_counter++; //overflow counter inkrementieren } if (overflow_counter==30) {//Nach 10x256x8 = 20480 overflows oder 50ms PORTD = 0x00; //Alle Ausgänge ausschalten PORTD |= (1 << PD4); //Tor 1 Zehner anschalten show_ten(goal1); } else if (overflow_counter==60) { PORTD = 0x00; //Alle Ausgänge ausschalten PORTD |= (1 << PD5); //Tor 1 Einser anschalten show_one(goal1); } else if (overflow_counter==90) { PORTD = 0x00; //Alle Ausgänge ausschalten PORTD |= (1 << PD6); //Tor 2 Zehner anschalten show_ten(goal2); } else if (overflow_counter==120) { PORTD = 0x00; //Alle Ausgänge ausschalten PORTD |= (1 << PD7); //Tor 2 Einser anschalten show_one(goal2); overflow_counter=0; pin_counter++; } if (pin_counter > 60){ i++; pin_counter = 0; } if (i == 7){ i=0; } } }







Zitieren

Lesezeichen