Forum: Compiler & IDEs TWI interface


von Mario S. (freak75)


Lesenswert?

Hallo,

ich verwende die TWI Library von Peter Fleury. Dabei ist mir folgendes 
aufgefallen, bei Verwendung des Orginalcodes bleibt der Controller in 
Zeile 20 hängen (Beispiel 1).Wenn ich den Code folgendermassen abändere 
lauft das Programm fehlerfrei weiter (beispiel 2). Kann mir jemand 
dieses Verhalten erklären ?

Controller: ATMEGA664P
win-AVR 20100110


Beispiel 1:

    /* send stop condition */
10  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);

  // wait until stop condition is executed and bus released
20  while(TWCR & (1<<TWSTO));

Beispiel 2:

    /* send stop condition */
  //TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  TWCR =   (1<<TWEN) | (1<<TWSTO);
  TWCR = (1<<TWINT);
  // wait until stop condition is executed and bus released
  while(TWCR & (1<<TWSTO));

Danke

freak75

von sebastians (Gast)


Lesenswert?

Hab mal ins Datenblatt (ATmega16, hatte ich halt gerade zur Hand) 
geschaut...

Writing the TWSTO bit to one in Master mode will generate a STOP 
condition on the Two-wire Serial Bus. When the STOP condition is 
executed on the bus, the TWSTO bit is cleared automatically. In Slave 
mode, setting the TWSTO bit can be used to recover from an error 
condition. This will not generate a STOP condition, but the TWI returns 
to a well-defined unaddressed Slave mode and releases the SCL and SDA 
lines to a high impedance state.

Da du in deinem Beispiel 2 das Bit hier löschst:
1
TWCR = (1<<TWINT);
wartet das wohl einfach nicht bis die STOP condition gesendet wurde.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.