Gast
#1302059
Hallo Leute, ich bin noch relativ neu im Thema µC, aber ich habe mich auf die "Forschung" von I2C Bus gestürzt. Im der Codesammlung bin ich auf den folgenden Beitrag gestoßen: Beitrag "AVR TWI Master und Slave Funtionen in C" Diesen Code habe ich runter gezogen und versuche es jetzt zu verstehen, leider gibt es paar Stellen wo ich nicht weiter komme. Könnte mir jemand auf die Sprünge helfen? >>>Das ist die Slave Routine: >while (1) > { >/* >** Check wether something is to do for the TWI slave interface >*/ > if (TWIS_ResonseRequired (&TWIS_ResonseType)) > { > switch (TWIS_ResonseType) > { >/* >** Slave is requests to read bytes from the master. >** It is expliciltely assumed, that the master sends 8 bytes >*/ > case TWIS_ReadBytes: > for (i=0;i<7;i++) > { > byte[i] = TWIS_ReadAck (); > printf ("Byte read: >%d\n",byte[i]); > } > byte[7] = TWIS_ReadNack (); > printf ("Byte read: %d\n",byte[7]); > TWIS_Stop (); > break; >/* >** Slave is requested to send bytes to the master. >** It is expliciltely assumed, that the master expects 8 bytes >*/ > case TWIS_WriteBytes: > for (i=0;i<8;i++) > { > TWIS_Write (j++); > printf ("Byte sent: %d\n", >j-1); > } > TWIS_Stop (); > break; > } > } >/* >** Do something else, e.g. >*/ > i++; > } Die erste If Schleife macht mir Sorgen -->if (TWIS_ResonseRequired (&TWIS_ResonseType))<-- Wenn ich es richtig verstehe wird hier die Funktion "TWIS_ResonseRequired" aufgerufen und dem Pointer in der Funktion die Variable -->(&TWIS_ResonseType)<-- übergeben. Wenn ich mir jetzt aber die Funktion anschaue verstehe ich das ganze nicht so ganz. >uint8_t TWIS_ResonseRequired (uint8_t *TWI_ResonseType) > { > *TWI_ResonseType = TWSR; > return TWCR & (1<<TWINT); > } Die Variable TWIS_ResonseType wird im Kopfteil deklariert, es wird aber kein Wert zugewiesen also hat die Variable den Wert Null. Also steht hier (uint8_t *TWI_ResonseType) eine Null. Und jetzt kommt das was ich nicht verstehe, wieso wird jetzt dem Pointer den Statuswert zugewiesen? Anschließend wird der Funktion der Status von TWINT zurück übergeben. >>>Jetzt kommen wir zu der Master Routine: >while (1) > { >/* >** Read byte(s) from the slave. >** It is implicitely assumed, that the slave will send >** 8 bytes >*/ > if (!TWIM_Start (SlaveAddress, TWIM_READ)) //Lesen=1 > { > TWIM_Stop (); > // printf ("Could not start TWI Bus for READ\n"); > } > else > { > for (i=0;i<7;i++) > { > Data[i] = TWIM_ReadAck (); > // printf ("Reading Byte %d: %d\n", i, >Data[i]); > } > Data[7] = TWIM_ReadNack (); > //printf ("Reading Byte %d: %d\n", i, Data[7]); > TWIM_Stop (); > Delay_ms (1000); > } >/* >** Write byte(s) to the slave. >** It is implicitely assumed, that the slave will >** accepts 8 bytes >*/ > if (!TWIM_Start (SlaveAddress, TWIM_WRITE)) //Schreiben=0 > { > TWIM_Stop (); > //printf ("Could not start TWI Bus for WRITE\n"); > } > else > { > for (i=0;i<8;i++) > { > TWIM_Write (j++); > //printf ("Byte %d sent: %d\n", i, j); > } > TWIM_Stop (); > Delay_ms (1000); > } >/* >** Do something else >*/ > i++; > } Hier sehen wir eine Immerschleife wo gleichzeitig eine Sende und eine Empfangsroutine aufgerufen werden. Warum wird es gleichzeitig aufgerufen? Ich hoffe mir kann jemand helfen.