SPI klappt nicht

Gast #1798430
Lesenswert?

Hallo,

ich habe im Moment ein Problem mit eienr SPI Kommunikation.
Ich hatte einen ATmega644 mit einem IC verschaltet. Habe ich erst alles 
direkt verlötet, hat auch alles super geklappt. Dann wollte ich die 
SPI-Schnittstelle via i-Coupler von Analog Devices galvanisch trennen. 
Dazu habe ich den IC auf einer anderen Platine aufgebaut. Dann ging 
nichts mehr.

Aus Verzweiflung habe ich den IC wieder auf die Platine mit dem 
Mikrocontroller zusammengeschaltet - jetzt geht immer noch gar nichts. 
Habe den IC schon ausgetauscht, alle Leitungen 100 mal überprüft.

Fakt ist, er bleibt immer an dieser while-Schlife hängen:
1
{
2
  int k;
3
  unsigned long temp;
4

5
  // SS low
6
  PORTB &= ~(1<<DDB4);
7
  for (k=0;k<5;k++){;}    
8

9
  //Start transmission 
10
  SPDR = address;
11
  //Wait for transmission complete 
12
  while(!(SPSR & (1<<SPIF)));     // HIER BLEIBT ER IMMER STEHEN
13
  for (k=0;k<50;k++){;}  
14

15
  // Byte empfangen
16
        // Dummy senden
17
  SPDR = 0x00;
18
  // Wait for dummy-transmission complete
19
  while(!(SPSR & (1<<SPIF)));  
20
  // Byte in temp reinschreiben
21
  temp = SPDR;
22
  for (k=0;k<5;k++){;}  
23

24
  // SS high
25
  PORTB |= (1<<DDB4);
26
  for (k=0;k<100;k++){;}
27
  return temp;
28
}
Gast #1798479
Lesenswert?

Hi!

Okay, du hattest recht. Ich war so dumm, und habe direkt unter main eine 
while(1)-Schleife eingefügt, um das SPI zu testen. Die Initialisierung 
kam erst unter dieser Schleife.

Aber du hast mich erst auf den richtigen Pfad gebracht. Also danke!
Moderator (Firma: Titel) Persönliche Seite #1798524
Lesenswert?

Das kann man so pauschal nicht sagen.
Bei einer Komponente, die nach einem programmgesteuerten Start 
selbständig auf dem uC läuft (SPI, RS232 senden...), mache ich sowas 
auch. Warum auch nicht? Ich weiß ja, dass dieser Prozess garantiert 
irgendwann fertig wird. Und wenn nicht, dann merke ich das schon in der 
Entwicklung (siehe oben).

Niemals sollte man aber auf externe Ereignisse (RS232-Empfang, 
Schalter...) auf diese Art warten.
#1798649
Lesenswert?

Datenblatt sagt:
1
When configured as a Master, the SPI interface has no automatic control of
2
the SS line. This must be handled by user software before communication can
3
start. When this is done, writing a byte to the SPI Data Register starts
4
the SPI clock generator
5
[...]
6
The Master may continue to shift the next byte by writing it into SPDR, or
7
signal the end of packet by pulling high the Slave Select, SS line. The
8
last incoming byte will be kept in the Buffer Register for later use.
und außerdem:
1
Master Mode
2
When the SPI is configured as a Master (MSTR in SPCR is set), the user can
3
determine the direction of the SS pin.
4
If SS is configured as an output, the pin is a general output pin which
5
does not affect the SPI system. Typically, the pin will be driving the SS
6
pin of the SPI Slave.
7
If SS is configured as an input, it must be held high to ensure Master SPI
8
operation. If the SS pin is driven low by peripheral circuitry when the
9
SPI is configured as a Master with the SS pin defined as an input, the SPI
10
system interprets this as another master selecting the SPI as a slave and
11
starting to send data to it. To avoid bus contention, the SPI system takes
12
the following actions:
13
1. The MSTR bit in SPCR is cleared and the SPI system becomes a Slave.
14
As a result of the SPI becoming a Slave, the MOSI and SCK pins become
15
inputs.
16
2. The SPIF Flag in SPSR is set, and if the SPI interrupt is enabled, and
17
the I-bit in SREG is set, the interrupt routine will be executed.

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