/*
* spi_send_01.c
* SPI-Master - Datenübertragung
* Übertragung zweier 8-Bit Daten (11010010 und 10000001)
* Controller: ATmega88 @ 1MHz
*/
#include <avr/io.h>
#include <util/delay.h>

void SPI_Master_Init(void)
{
	SPCR &= ~(1<<DORD);		// Send MSB first
	SPCR |= (1<<MSTR);		// Controller = Master
	SPCR &= ~(1<<CPOL);		// CLK positiv phase
	SPCR |= (1<<CPHA);		// Sampling data @ rising edge
	SPCR |= (1<<SPR1);		// CLK / 64 = 125kHz
	DDRB |= (1<<PB3);		// Set MOSI output
	DDRB &= ~(1<<PB4);		// Set MISO input
	DDRB |= (1<<PB5);		// Set SCK output
	DDRB |= (1<<PB2);		// Set SS output
	SPCR |= (1<<SPE);		// SPI enable
}

void SPI_Master_Transmit(char cData)
{
	PORTB &= ~(1<<PB2);				// SS = low (Slave = enabled)
	SPDR = cData;					// Start transmission
	while (!(SPSR & (1<<SPIF)));	// Wait for transmission complete
	PORTB |= (1<<PB2);				// SS = high (Slave = disabled)
}

int main(void)
{
	SPI_Master_Init();
	
	while(1)
	{
		SPI_Master_Transmit(0b11010010);
		_delay_ms(500);		// delaytime between next transmission
		SPI_Master_Transmit(0b10000001);
		_delay_ms(500);		// delaytime between next transmission
	}
}