Forum: Compiler & IDEs SPI in Gang bringen / überoptimiert?


von CAnfänger (Gast)


Lesenswert?

Hallo Leute, ich wollte jetzt mal die SPI-SChnittstelle in Gang bringen 
und habe ein kleines Testprogramm aus meinem Projekt extrahiert. Es soll 
einfach permanent ein Byte (0x95) per SPI senden. Der Code compiliert 
aber zu 0 Byte - warum?

//Bibliotheken
#include <avr/io.h>
#include <stdint.h> // damit man uint8_t verwenden kann

//für SPI:
#define DDR_SPI  DDRB
#define PORT_SPI PORTB
#define MOSI   2
#define MISO   3
#define SCK     1
#define SS     4 // eigentlich 0, aber ich will es selbst steuern

//Variable
static uint8_t dummy;

//Funktionen
int main(void);
static uint8_t SPI_Transfer(uint8_t sByte);
static void SPI_MasterInit(void);

//Hauptprogramm
int main(void)
{
  SPI_MasterInit();

  while(1)
  {
    dummy=SPI_Transfer(0x95);
  }
}

void SPI_MasterInit(void)
{
  // Set MOSI, SCK and SS output, MISO Input
  DDR_SPI |= (1<<MOSI)|(1<<SCK)|(1<<SS);
  DDR_SPI &= ~(1<<MISO);
  //from datasheet
         SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}

uint8_t SPI_Transfer(uint8_t sByte)
{
  SPDR = sByte;
  while(!(SPSR & (1<<SPIF)));
  return SPDR;
}

von Frank L. (franklink)


Lesenswert?

Hallo,
ich habe den Code mal compiliert. (ATMEGA8 und AVR-Studio)

das Ergebnis:
1
Program:     120 bytes (1.5% Full)
2
(.text + .data + .bootloader)
3
4
Data:          0 bytes (0.0% Full)
5
(.data + .bss + .noinit)
6
7
Build succeeded with 0 Warnings...

Also alles vollkommen in Ordnung.

Gruß
Frank

von CAnfänger (Gast)


Lesenswert?

Ich schäme mich! Ich hab nur bei data.. geguckt, weil mein Fenster so 
eingestellt war..

Aber mein eigentliches Problem: An PB1 (SCK) müßte ich doch jetzt mit 
dem Oszi was sehen, oder? Es tut sich nichts.. (permanent LOW, sieht 
aus, als wär es gar nicht als Ausgang beschaltet..)

von Johann L. (gjlayde) Benutzerseite


Lesenswert?

Das war doch was mit SS? Es muss entweder OUT sein oder immer HIGH, 
ansonsten flutscht der SPI in Slave-Mode.

Johann

von CAnfänger (Gast)


Lesenswert?

Ich verstehe es zwar nicht (ich aktiviere die SPI ja als Master), aber 
auch wenn ich SS als Ausgang definiere, ändert sich nichts.

Also ich hab folgende Zeile ergänzt

#define SSPLUS   0

und in der SPI_MasterInit geschrieben

DDR_SPI |= (1<<MOSI)|(1<<SCK)|(1<<SS)|(1<<SSPLUS);


es clockt einfach nicht..


Hat evtl. jemand ein funktionierendes Mini-Programm?

von CAnfänger (Gast)


Lesenswert?

arrrrgh! Jetzt geht es. Es lag wirklich an diesem Pin (SS muß Ausgang 
sein) Ich hab außerdem wiedermal vergessen, daß man trotz "neues 
Projekt" immer noch den alten Hexfile zum Programmieren drinstehen hat 
bei AVR Studio.. *das nervt..*

Aber wo steht das mit dem SS Pin? (nur mal interessehalber)..

von Stefan E. (sternst)


Lesenswert?

Datenbatt -> SS Pin Functionality -> Master Mode:
1
When the SPI is configured as a master (MSTR in SPCR is set), the user
2
can determine the direction of the SS pin.
3
4
If SS is configured as an output, the pin is a general output pin which does
5
not affect the SPI system. Typically, the pin will be driving the SS pin of
6
the SPI slave.
7
8
If SS is configured as an input, it must be held high to ensure Master SPI
9
operation. If the SS pin is driven low by peripheral circuitry when the SPI
10
is configured as a master with the SS pin defined as an input, the SPI
11
system interprets this as another master selecting the SPI as a slave and
12
starting to send data to it.

von CAnfänger (Gast)


Lesenswert?

Ah ja.. danke! Da hatte ich wohl bisher zufällig immer Glück.. :-)

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.