SPI in Gang bringen / überoptimiert?

Gast #1156753
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;
}
Gast #1156793
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..)
Gast #1156855
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?
Gast #1156879
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)..
#1156884
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.

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