Hallo Zusammen,

ich habe eine Schaltung aufgebaut, in der ein ATmega8 über I2C mit einem SRF02 Ultraschallmodul redet. Ich benutze im Code die I2C-Master Implementierung von peter fleury.

Ich hatte jetzt schon öfter den Effekt, dass die Kommunikation im i2c_readAck() einfriert, was vermutlich darauf zurückzuführen ist, dass vom Slave (SRF02) keine Antwort zurück kommt.

Ich wollte nun also versuchen, das Ganze interrupt-basiert anzugehen. Leider bekomme ich es nicht mal auf die Reihe, einen Interrupt für Aktivitäten auf dem I2C-Bus zu aktivieren. Ich habe folgenden Code:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "delay.h"
#include "lcd.h"
#include "i2cmaster.h"

#ifndef SIGNAL
#include <avr/signal.h>
#endif /* SIGNAL */

ISR (TWI_vect)
{
	PORTB &= ~(1 << PB0);
	delayms(500);
	PORTB |= (1 << PB0);
}

unsigned int read_cm(unsigned char address)
{
	unsigned char lowbyte,highbyte;
	unsigned int distance;

	i2c_start_wait(address+I2C_WRITE);

	while (i2c_write(0));
	while (i2c_write(81));
	i2c_stop();

	i2c_start_wait(address+I2C_WRITE);
	while (i2c_write(2));
	i2c_stop();
	
	delayms(65);
	
	i2c_start_wait(address+I2C_READ);
	highbyte = i2c_readAck();
	lowbyte = i2c_readNak();
	distance = (highbyte<<8)+lowbyte;
	i2c_stop();
	if(distance > 150) {
		PORTC = 0x00;
	}
	return distance;
}

int main(void)
{
	...
	i2c_init();
	...
	TWCR |= (1<<TWIE) | (1<<TWINT);
	sei();

	for ( ;; ) {
		dist = read_cm(SRF02_ADDRESS);
		if (dist >= HEIGHT) {
			fill_percent = 0;
		} else {
			fill_percent = 100 - ((dist * 100) / HEIGHT);
		}
		delayms(3000);
	}
}
Was mache ich beim Aktivieren des Interrupts falsch? Es passiert einfach nichts ...

Wie ist das eigentlich, muss ich als Implementierung nun twimaster.c oder i2cmaster.S verwenden?

AVR-GCC Version 4.3.0

Gruss,
mefiX