Forum: Mikrocontroller und Digitale Elektronik I²C Adressen prüfen


von gustav (Gast)


Lesenswert?

Hallo,

Ich möchte das mein mega8 I2C Master beim einschalten guckt welche 
Slaves eingeschaltet sind. Dazu soll er einfach nur alle Adressen von 
0x10 bis 0x1F prüfen.

Mein Problem ist das wenn ich folgenden Code ausführe fängt ein Slave an 
zu spinnen und hängt sich bei jedem zweiten mal auf auf (Zieht dabei 
beide Signalleitungen auf low).
1
uint8_t i;
2
for(i = 0x10; i <= 0x1F; ++i)
3
{
4
  temp_buffer[0] = (i<<TWI_ADR_BITS)|(0<<TWI_READ_BIT);
5
  TWI_Start_Transceiver_With_Data(temp_buffer, 1);
6
  while(TWI_Transceiver_Busy());
7
  _delay_us(50);
8
}

Das ist noch minimal Code, er hängt sich direkt beim ersten Durchlauf 
bei TWI_Transceiver_Busy() auf weil die Leitungen auf low sind.

Wenn ich den Code mitten im Betrieb laufen lassen (taster/uart) 
funktioniert alles wunderbar.

Der Slave der den Fehler verursacht hat TWAR = 0x10 (Addrese).

Ich verwende den Code aus App note AVR311 und AVR315.

von Wolfgang Horn (Gast)


Lesenswert?

Hi, Gustav,


mein Verdächtiger: "TWI_Start_Transceiver_With_Data()" - was passiert da 
drin?

Ciao
Wolfgang Horn

von gustav (Gast)


Lesenswert?

1
/****************************************************************************
2
Call this function to send a prepared message. The first byte must contain the slave address and the
3
read/write bit. Consecutive bytes contain the data to be sent, or empty locations for data to be read
4
from the slave. Also include how many bytes that should be sent/read including the address byte.
5
The function will hold execution (loop) until the TWI_ISR has completed with the previous operation,
6
then initialize the next operation and return.
7
****************************************************************************/
8
void TWI_Start_Transceiver_With_Data( unsigned char *msg, unsigned char msgSize )
9
{
10
  unsigned char temp;
11
12
  while ( TWI_Transceiver_Busy() );             // Wait until TWI is ready for next transmission.
13
14
  TWI_msgSize = msgSize;                        // Number of data to transmit.
15
  TWI_buf[0]  = msg[0];                         // Store slave address with R/W setting.
16
  if (!( msg[0] & (TRUE<<TWI_READ_BIT) ))       // If it is a write operation, then also copy data.
17
  {
18
    for ( temp = 1; temp < msgSize; temp++ )
19
      TWI_buf[ temp ] = msg[ temp ];
20
  }
21
  TWI_statusReg.all = 0;      
22
  TWI_state         = TWI_NO_STATE ;
23
  TWCR = (1<<TWEN)|                             // TWI Interface enabled.
24
         (1<<TWIE)|(1<<TWINT)|                  // Enable TWI Interupt and clear the flag.
25
         (0<<TWEA)|(1<<TWSTA)|(0<<TWSTO)|       // Initiate a START condition.
26
         (0<<TWWC);                             //
27
}

von Joerg X. (Gast)


Lesenswert?

Nur so aus Neigierde:
- du benutzt auch den IAR-Compiler (für den ist das Code-Beispiel)?
- du hast auch die Interrupts aktiviert? (der Code arbeitet 
Interruptbasiert, und du "pfuschst" da irgendwie dazwischen)
- warum fragst du diese Adresen ab, ohne irgendeine Auswertung, d.h. 
ohne zu kontrollieren OB ein slave antwortet? (da gibt's eine "union..." 
mit dem aktuellen TWI-Status....)

also meine Tipps sind RTFM! sowohl vom AVR als auch von Compiler und 
App-Note

von Jörg B. (manos)


Lesenswert?

Was sind das für Slaves? µC's (Software) oder 
Porterweiterungen/Sensoren/etc (fertige Hardware).

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.