Skip to content

Instantly share code, notes, and snippets.

@avr-programmierung
Created May 16, 2019 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avr-programmierung/63c9c6f1f26a746b2d50c2236aabdd56 to your computer and use it in GitHub Desktop.
Save avr-programmierung/63c9c6f1f26a746b2d50c2236aabdd56 to your computer and use it in GitHub Desktop.
ATmega88 @ 8MHz SPI-Slave - Daten am MOSI empfangen und am PORTD ausgeben
/* * spi_receive_01.c
* SPI-Slave - Daten am MOSI empfangen und am PORTD ausgeben
* Controller: ATmega88 @ 8MHz
*/
#include <avr/io.h>
#include <util/delay.h>
void SPI_Slave_Init(void)
{
SPCR &= ~(1<<MSTR); // Controller = Slave
SPCR &= ~(1<<CPOL); // CLK positiv phase
SPCR |= (1<<CPHA); // Sampling data @ rising edge
DDRB |= (1<<PB4); // Set MISO output
DDRB &= ~(1<<PB3); // Set MOSI input
DDRB &= ~(1<<PB5); // Set SCK input
DDRB &= ~(1<<PB2); // Set SS input
SPCR |= (1<<SPE); // SPI enable
}
uint8_t SPI_Slave_Receive(void)
{
while (!(SPSR & (1<<SPIF))); // Wait for transmission complete
return SPDR; // return spi data register
}
int main(void)
{
DDRD = 0xFF;
SPI_Slave_Init(); // Init SPI Interface as Slave
while(1)
{
PORTD = SPI_Slave_Receive();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment