#include <avr/io.h>
#include <util/delay.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define RF_PORT PORTB // SPI Port
#define RF_DDR DDRB
#define RF_PIN PINB
#define SDI 3 // MOSI Pinnummer
#define SCK 5 // SCK Pinnummer
#define CS 2 // CS Pinnummer
#define SDO 4 // MISO Pinnummer
void spi_init_master (void)
{
//RF_PORT=(1<<CS);
RF_DDR&=~((1<<SDO));
RF_DDR|=(1<<SDI)|(1<<SCK)|(1<<CS);
SPCR=(1<<SPE)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA)|(1<<DORD);
// SPSR |= (1<<SPI2X);
}
unsigned char spi_tranceiver (unsigned char data)
{
cbi(RF_PORT, CS);
SPDR = data; //Load data into the buffer
while(!(SPSR & (1<<SPIF)));
sbi(RF_PORT, CS);
return(SPDR); //Return received data
}
int main(void)
{
unsigned char a=5;
unsigned char b;
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPS0);
CLKPR=(0<<CLKPS1);
CLKPR=(1<<CLKPS2);
CLKPR=(0<<CLKPS3);
DDRD=0xFF;
DDRC=0xFF;
spi_init_master();
asm("NOP");asm("NOP");
while(1)
{
SETBIT(PORTC,0);
_delay_ms(500);
CLEARBIT(PORTC,0);
_delay_ms(500);
b=spi_tranceiver(a);
if (b==6)
{
PORTD=0xFF;
_delay_ms(500);
PORTD=0x00;_delay_ms(500);
}
}
}