Problem beim Starten von TWI

OP #4419623
Lesenswert?

Hallo Leute,

ich beschäftige mich gerade mit dem TWI bzw. I2C eines Atmega32, 
Minimalbeispiel steht unten. Der Atmega32 wird mit 4MHz versorgt und SCL 
und SDA hängen jeweils mit 3,3k Widerständen an VCC (5V). Einziger Slave 
ist derzeit ein PCF8574.

Nach dem
1
     
2
I2C_Init();
3
PORTA  = TWCR;
zeigt PORTA das was ich erwarte (0b00000100).

Nach dem
1
     
2
I2C_Start();
3
PORTD  = TWCR;
zeigt PORTD (0b10100100). Wenn TWINT (erste Bit) an dieser Stelle noch 
gesetzt ist, hätte er dann nicht bei
1
 
2
while ((TWCR & (1<<TWINT)) == 0);
hängen bleiben müssen? Sieht für mich nämlich so aus, als würde er START 
nicht senden - verstehe dann aber nicht, warum er PORTD anspricht. Habe 
mir TWSR auch mal ausgeben lassen, ist komplett leer...


Kompletter Code:
1
#include <avr/io.h>
2

3
int main (void){
4
DDRA   = 0xFF;
5
DDRD    = 0xFF;
6
  
7
I2C_Init();
8
PORTA  = TWCR;
9
I2C_Start();
10
PORTD  = TWCR;
11

12
while(1){
13
}
14
return 0;
15
}
16

17
void I2C_Init(void)
18
{
19
//set SCL to 100kHz @ 4MHz
20
TWSR = 0x00;  // Prescaler = 1
21
TWBR = 0x02;  // 
22
//enable TWI
23
TWCR = (1<<TWEN);  // Enable
24
}
25

26
// start signal
27
void I2C_Start(void)
28
{
29
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);  // Clear Interrupt, send START, Enable I2C
30
while ((TWCR & (1<<TWINT)) == 0);    // Wait until finished
31
}
OP #4419778
Lesenswert?

Das stimmt laut Datenblatt so:

1. The first step in a TWI transmission is to transmit a START 
condition. This is done by writing a specific value into TWCR, 
instructing the TWI hardware to transmit a START condition. Which value 
to write is described later on. However, it is important that the TWINT 
bit is set in the value written. Writing a one to TWINT clears the Flag. 
The TWI will not start any operation as long as the TWINT bit in TWCR is 
set. Immediately after
the application has cleared TWINT, the TWI will initiate transmission of 
the START condition.
2. When the START condition has been transmitted, the TWINT Flag in TWCR 
is set, and TWSR is updated with a status code indicating that the START 
condition has successfully been sent.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren