Rainbow-electronics ATmega8515L Instrukcja Użytkownika Strona 125

  • Pobierz
  • Dodaj do moich podręczników
  • Drukuj
  • Strona
    / 223
  • Spis treści
  • BOOKMARKI
  • Oceniono. / 5. Na podstawie oceny klientów
Przeglądanie stron 124
125
ATmega8515(L)
2512A–AVR–04/02
The following codeexamplesshowhow to initializethe SPI as a Master andhow to per-
formasimpletransmission. DDR_SPI in theexamples must be replacedbytheactual
Data Direction Registercontrolling the SPIpins. DD_MOSI, DD_MISO andDD_SCK
must be replacedbytheactualdata direction bitsfor these pins. For example, ifMOSIis
placed on pin PB5,replace DD_MOSI with DDB5andDDR_SPIwithDDRB.
Note: 1. Theexample codeassumes that the part specificheaderfileis included.
Assembly Code Example
(1)
SPI_MasterInit:
; Set MOSI and SCK output, all others input
ldi r17,(1<<DD_MOSI)|(1<<DD_SCK)
out DDR_SPI,r17
; Enable SPI, Master, set clock rate fck/16
ldi r17,(1<<SPE)|(1<<MSTR)|(1<<SPR0)
out SPCR,r17
ret
SPI_MasterTransmit:
; Start transmission of data (r16)
out SPDR,r16
Wait_Transmit:
; Wait for transmission complete
sbis SPSR,SPIF
rjmp Wait_Transmit
ret
CCode Example
(1)
void SPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void SPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
}
Przeglądanie stron 124
1 2 ... 120 121 122 123 124 125 126 127 128 129 130 ... 222 223

Komentarze do niniejszej Instrukcji

Brak uwag