Forum: Mikrocontroller und Digitale Elektronik Problem mit der SPI Master Programmierung (Atmega8)


von Markus H. (rf-bug)


Lesenswert?

Hallo *,

ich habe evtl. ein kleines Problem. Ich simuliere mit mit dem AVR Studio 
den angehängten Code. SPI als Master

Ich meine, das die Daten "nur einmal" geschickt werden.

Es scheint, als ob das     SPIF: SPI Interrupt Flag
nur einmal (also bei ersten Mal senden) gesetzt ist. Beim 2ten Durchlauf 
ist es bei der Datenübergabe an SPDR = cData; das Flag sofort auf Null 
wird und der Prozessor hier
1
while(!(SPSR & (1<<SPIF)))
"hängen" bleibt.

Kann mir einer sagen, was ich da noch falsch mache?

Danke!

Gruß
1
#include <avr/io.h>
2
#include <inttypes.h>
3
 
4
#define DDR_SPI     DDRB
5
#define DD_MISO     PB4
6
#define DD_MOSI     PB3
7
#define DD_SCK     PB5
8
#define DD_SS     PB2
9
10
void SPI_MasterInit(void)
11
{  /* Set MOSI and SCK output, all others input */
12
  DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK);
13
  /* Enable SPI, Master, set clock rate fck/16 */
14
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
15
  // Internal Pullup for MISO to avoid floating Signal States
16
  PORTB |= (1<<DD_MISO);
17
}
18
19
void SPI_MasterTransmit(int cData)
20
{  /* Start transmission */
21
  SPDR = cData;
22
  /* Wait for transmission complete */
23
  while(!(SPSR & (1<<SPIF)))
24
  {;}
25
}
26
27
28
// Mainprog  
29
int main (void) 
30
{
31
  SPI_MasterInit();
32
  PORTB |= (1<<DD_SS);
33
34
  while (1) 
35
  {
36
  if bit_is_set (PINB, 0)
37
  {
38
  SPI_MasterTransmit (0xB2);
39
  }
40
  SPCR &= ~(1<<SPIF);  
41
  
42
  } //while (1) Schleife
43
  return 0;
44
}

von Falk B. (falk)


Lesenswert?

@ Markus H. (rf-bug)

>Kann mir einer sagen, was ich da noch falsch mache?

Du musst SS als Ausgang schalten.

AVR-Tutorial: Schieberegister

MfG
Falk

von Markus H. (rf-bug)


Lesenswert?

Danke Falk!

Das war es!

Gruß
Markus

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.