/*****************************************************
Project : ATmega2560 MCU°£ÀÇ SPIÅë½Å Å×½ºÆ®
Version : LK-AVR2560D V01
Date    : 2011-07-28
Author  : Kyung Nam Lee
Company : LKEMBEDDED 
Compiler: CodevisionAVR+AVRStudio
Comment: Master MCU(LK-AVR2560DÈ®ÀåÇü °³¹ßº¸µå)¿¡¼­ SW1, SW2¸¦ ÀÔ·ÂÇÏ¸é Slave MCU(LK-AVR2560DÈ®ÀåÇü °³¹ßº¸µå)
        ¿¡¼­ ºÎÀú¿Í LED°¡ ON/OFF µÇ¸ç ¸¶½ºÅÍ, ½½·¹ÀÌºê º¸µå¿¡¼­ ¼­·Î ±³È¯ÇÑ µ¥ÀÌÅÍ¸¦ RS232Æ÷Æ®¿¡ Ãâ·Â ÇÏ´Â ÇÁ·Î±×·¥ÀÌ´Ù. 
        ÀÌ¶§ÀÇ LK-AVR2560DÈ®ÀåÇü °³¹ßº¸µå°£ÀÇ ³»Àå µÈ  10ÇÉ ¹Ú½ºÇì´õ Ä¿³ØÅÍ(PORTB)´Â 10PÇÃ·§ÄÉÀÌºí ÀÌ¿ëÇÏ¿© ¹Ýµå½Ã ¼­·Î ¿¬°á µÇ¾î ÀÖ¾î¾ß ÇÑ´Ù. 
*****************************************************/

#include <mega2560.h>
#include <delay.h>
#include <stdio.h>

#define Master 
//#define Slave   //LK-AVR2560DÈ®ÀåÇü °³¹ßº¸µå Master or Slaveµ¿ÀÛ ¸ðµå¸¦ ¼³Á¤!


#ifdef Master
unsigned char SPI_send(unsigned char data)//
{ 
    unsigned char data3=0;    
    SPDR = data;            /* Start transmission */
    while(!(SPSR&0x80));    /* Wait for transmission complete */ 
    return SPDR; 
    /*return SPDR; Àü¼Û(write)ÇÔ¼ö¿Í ÀÐ±â ÇÔ¼ö¸¦ ±¸ºÐ Áþ±â À§ÇØ¼­ ÁÖ¼®Ã³¸®¸¦ ÇÑ´Ù.
    ÇÊÀÚ´Â ±»ÀÌ ÇÔ¼ö¸¦ µû·Î ¸¸µé ÇÊ¿ä¼ºÀ» ¸ø ´À³¤´Ù.  
    Áß¿äÇÑ °ÍÀº SPIÅë½Å½Ã¿¡ Master¿¡¼­ SPDR·¹Áö½ºÅÍ¿¡ ±â·ÏÇÒ¶§¸¸ SPI ClockÀÌ Ãâ·ÂµÈ´Ù´Â Á¡ÀÌ´Ù.
    Áï, ½½·¹ÀÌºê¿¡¼­´Â SPDR·¹Áö½ºÅÍ¿¡ °ªÀ» ±â·ÏÇØ¼­ ´ë±âÇÏ°í ÀÖÀ¸¸ç, Master¿¡¼­ SPDR·¹Áö½ºÅÍ¿¡ °ªÀ» 
    ±â·Ï(Ãâ·ÂÇÏ¸é) Feedback µÇ¾î¼­ Master¿¡ ÀÔ·ÂµÈ´Ù´Â Á¡ÀÔ´Ï´Ù.
    */  
}    
char SPI_read()//dummy°ªÀ¸·Îµµ ÀÐ¾î ¿Ã¼ö ÀÖÁö¸¸ À§ SPI_sendÇÔ¼öÃ³·³ ¹Ù·Î ÀÐ¾î µé¾î ¿Íµµ ¹«°üÇÑ´Ù. 
{ 
    unsigned char data3=0;    
    SPDR = 0x00; //Dummy Data
    while(!(SPSR&0x80));          
    return SPDR;  
}     
void Master_init()
{
    PORTB=0x00;
    DDRB=0x07; /* Set MOSI and SCK output, all others input */
    SPCR=0x50; /* Enable SPI, Master, set clock rate fck/16 */
    SPSR=0x00;
}
#endif 

#ifdef Slave 
/*½½·¹ÀÌºêÃø¿¡¼­ ¸¶½ºÅÍ·Î µ¥ÀÌÅÍ¸¦ º¸³¾ ½Ã¿¡´Â µ¥ÀÌÅÍ¸¦ SPDR·¹Áö½ºÅÍ¿¡ ÀúÀå¸¸ ÇÏ¸éµË´Ï´Ù.
ÀÌ¶§ÀÇ ¸¶½ºÅÍ°¡ ½½·¹ÀÌºê·Î µ¥ÀÌÅÍ¸¦ º¸³¾ ¶§ ½½·¹ÀÌºêÀÇ µ¥ÀÌÅÍ´Â ÀÚµ¿ÀûÀ¸·Î ¸¶½ºÅÍ·Î Àü¼ÛµË´Ï´Ù.
°á±¹ ¸¶½ºÅÍ¿¡¼­ ¹ß»ýµÇ´Â SPIÅ¬·° ¾øÀÌ´Â ½½·¹ÀÌºê¿¡¼­ ´Üµ¶À¸·Î ¸¶½ºÅÍ·Î µ¥ÀÌÅÍ¸¦ º¸³¾¼ö´Â ¾øÀ¸¸ç,
AVRÄ¨ÀÇ SPIÅë½ÅÀº ¸¶½ºÅÍ¿Í ½½·¹ÀÌºê°¡ ¼­·Î ±³Â÷¿¬°áµÈ ¿øÇü(LOOP)±¸Á¶·Î µ¥ÀÌÅÍ¸¦ ±³È¯ÇÏ°í ÀÖ´Ù.*/
char SPI_receive() //slave
{   
    
    unsigned char data2=0;  
    while(!(SPSR&0x80)); /* Wait for reception complete */
    data2= SPDR;         /* Return Data Register */  
    if(data2==0xfe)
    SPDR = 1;         /* Start transmission */
    else SPDR = 2; 
    printf("SLAVE LK-AVR2560D:receive data from MASTER:%d \r\n",data2); 
    return data2;
    
}
void Slave_init()
{
   
    PORTB=0x00;
    DDRB=0x08;  /* Set MISO output, all others input */   
    SPCR=0x40;  /* Enable SPI */
    SPSR=0x00;
}
#endif
 
void main(void)
{
    UCSR0A=0x00;
    UCSR0B=0x08;
    UCSR0C=0x06;
    UBRR0H=0x00;
    UBRR0L=0x67;  
      
    DDRG=0X13;  //LED, BUZZ OUTPUT SET
    PORTG=0X13; //LED, BUZZ INITIALIZE

    #ifdef Master     //master? 
    Master_init(); 
    #endif
    #ifdef Slave      //master? 
    Slave_init();
    #endif         
while (1)
    {           
         #ifdef Master     //master? 
            static unsigned char data1=0;
            if((PING&0X04)==0)  
            {    
                delay_ms(20); //8mS Áö¿¬½Ã°£ÀÌ È®º¸°¡ µÇ¾ß¸¸ ¼Û½Å°ª°ú ¼ö½Å°ªÀÌ °ãÃÄ¼­ printf()Ãâ·Â µÇÁö ¾Ê´Â´Ù.          
                data1=SPI_send(0XFF); //Slave '0xff' Transmitter!   
                printf("MASTER  LK-AVR2560D: receive data from SLAVE:%d \r\n",data1); 
            }  
            else if((PING&0X08)==0)  
            {      
                delay_ms(20); 
                data1=SPI_send(0XFE); //Slave '0xfe' Transmitter! 
                printf("MASTER LK-AVR2560D: receive data from SLAVE:%d \r\n",data1);      
    
            } 
        #endif 
        #ifdef Slave      //master? 
            static unsigned char data=0;
            data=SPI_receive(); //Receive Function 
            if(data==0xff)
            {
            PORTG=0X00;   //Buzz, LED ON   
            }
            if(data==0xfe)
            {
            PORTG=0X13;  //Buzz, LED OFF
            }                    
        #endif 
    }
      
}