#include <avr/io.h>
#include "SPI_slave.h"
#include "global.h"
//================================ Slave ========================================
void Slave_Init()
{
// sbi(SREG,7); // SREG = (1 << 7); SREG = (1 << I);은 에러 코드 !
DDRE = (1 << PE5); // slave 측 LED
DDRB = (1 << PB3) | (0 << PB2) | (0 << PB1) | (0 << PB0); // MISO, MOSI, SCK, SS
SPCR = (0 << SPIE) | (1 << SPE) | (0 << MSTR) | (0 << SPR0); // slave에서는 분주가 의미가 없다.
SPSR = 0x00;
}
char Slave_Receive()
{
while(!(SPSR & (1 << SPIF))); // Slave Mode에서 동작 시, 데이터 수신 완료 후 Flag up!
return SPDR;
}
/* SPI Slave */
void SPI_slave()
{
char Receive_data=0x00;
Slave_Init();
sbi(PORTE,5); // LED OFF
while(1)
{
Receive_data = Slave_Receive();
if(Receive_data == 0x55) // 01010101
cbi(PORTE,5);
else if(Receive_data == 0xaa) // 10101010
cbi(PORTE,5);
else
sbi(PORTE,5);
}
}