Forum: Mikrocontroller und Digitale Elektronik SPI Initialisierung in AVR Assembler ATmega88


von Jan S. (jschirrmacher)


Lesenswert?

Hi liebe Mitstreiter,

möchte mit meinem ATmega88 über SPI kommunizieren und initialisiere ihn 
mit folgendem Code:

.equ SPIDDR     = DDRB
.equ SPIMOSI    = PORTB3
.equ SPIMISO    = PINB4
.equ SPISCK     = PORTB5

SPI_MASTERINIT:
    sbi SPIDDR,SPIMOSI // Set MOSI and SCK output
    sbi SPIDDR,SPISCK
    cbi SPIDDR,SPIMISO
    ldi r17,(1<<SPE)|(1<<MSTR)|(1<<SPR0) // Enable SPI, Master, set 
clock rate fck/16
    out SPCR,r17
    in r17,SPSR // Clear status register
    ret

Nun teste ich diesen Code im Simulator und stelle fest, dass das 
SPCR-Register beim out-Befehl erst richtig besetzt ist, die Flags beim 
nächsten Befehl aber plötzlich anders sind, nämlich ist plötzlich das 
SPI Interrupt Flag gesetzt und das Master/Slave Select ist wieder 
gelöscht. SPI Enable bleibt gesetzt.

Ist nun der Simulator falsch oder was geht hier vor sich?

Im realen Gerät scheinen alle Flags 0 zu bleiben was ich allerdings nur 
über eine Ausgabe über den UART feststellen kann.

Hat jemand eine Idee?

von ich (Gast)


Lesenswert?

Hallo Jan Schirrmacher

Setze den slave select (SS) Pin auf AUSGANG bzw. sorge dafür, wenn auf 
Eingang gesetzt, das er immer high ist (Pullup).

Denn...Egal was du in der Initialisierung machst zb. Master oder Slave 
wählst, wird der µC wieder zum Slave gesetzt, wenn das SPI modul ein low 
Puls am SS Pin erkennt und der pin ein Eingang ist.

18.3.2 Master Mode

When the SPI is configured as a Master (MSTR in SPCR is set), the user 
can determine the
direction of the SS pin.
If SS is configured as an output, the pin is a general output pin which 
does not affect the SPI
system. Typically, the pin will be driving the SS pin of the SPI Slave.
If SS is configured as an input, it must be held high to ensure Master 
SPI operation. If the SS pin
is driven low by peripheral circuitry when the SPI is configured as a 
Master with the SS pin
defined as an input, the SPI system interprets this as another master 
selecting the SPI as a
slave and starting to send data to it. To avoid bus contention, the SPI 
system takes the following
actions:
1. The MSTR bit in SPCR is cleared and the SPI system becomes a Slave. 
As a result of
the SPI becoming a Slave, the MOSI and SCK pins become inputs.
2. The SPIF Flag in SPSR is set, and if the SPI interrupt is enabled, 
and the I-bit in SREG is
set, the interrupt routine will be executed.
Thus, when interrupt-driven SPI transmission is used in Master mode, and 
there exists a possibility
that SS is driven low, the interrupt should always check that the MSTR 
bit is still set. If the
MSTR bit has been cleared by a slave select, it must be set by the user 
to re-enable SPI Master
mode.

Quelle: Datenblatt REV: 8025K–AVR–10/09

MfG

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.