AVR

SPI_Slave

작성자 임베디드코리아 작성일17-11-15 16:25 조회2,523회 댓글0건
//1. baud rate를 선택
 #define USART_BAUDRATE 9600
 
 //2.시스템 클록과 원하는 baud rate를 이용하여 UBRR 값을 계산한다.
 #define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
 
#include <avr/io.h>
#include <avr/interrupt.h>
 
 
void usartInit()
{
    //3. UBRR0은 16비트 레지스터이기 때문에 8비트씩 나누어서 넣어야 한다.
    UBRR0H = (uint8_t)(UBRR_VALUE>>8);
    UBRR0L = (uint8_t) UBRR_VALUE;
   
    //4. USART 설정
    UCSR0C |= (1<<UCSZ00)|(1<<UCSZ01); //Charecter size : 8비트
    UCSR0C &= ~(1<<USBS0); //stop  bit : 1비트
    UCSR0C &= ~((1<<UPM01)|(1<<UPM00)); // no parity mode
   
    //5. 송수신을 가능하게 한다.
    UCSR0B=(1<<RXEN0)|(1<<TXEN0);
}
 
void transmitByte(uint8_t data) {
   
    //이전 전송이 끝나기를 기다림
    while(!(UCSR0A&(1<<UDRE0))){};
   
    UDR0 = data;                                            /* send data */
}
 
uint8_t receiveByte(void) {
    // 수신 되기를 기다림
    while(!(UCSR0A&(1<<RXC0))){};
    return UDR0;                                /* return register value */
}
 
 
void printString(volatile unsigned char str[]) {
    uint8_t i = 0;
    while (str[i]!='\0') {
        transmitByte(str[i]);
        i++;
    }
}
 
void readString(char str[], uint8_t maxLength) {
    char response;
    uint8_t i;
    i = 0;
    while (i < (maxLength - 1)) {                  /* prevent over-runs */
        response = receiveByte();
        transmitByte(response);                                    /* echo */
        if (response == '\r') {                    /* enter marks the end */
            break;
        }
        else {
            str[i] = response;                      /* add in a letter */
            i++;
        }
    }
    str[i] = 0;                          /* terminal NULL character */
}
 
void  init_SPI( ) {
   
    DDRB &= ~((1<<PB1)|(1<<PB2)|(1<<PB0)); // SCK, MOSI, SS --> inputs
    DDRB |= (1<<PB3);                  // MISO --> output
   
    //slave mode
    SPCR &= ~(1<<MSTR);
   
    //16000KHz / 16
    SPCR  |= (1<<SPR0);
   
    //enable SPI
    SPCR |= ((1<<SPE)|(1<<SPIE));
 
}
 
 
 
 
unsigned char SPI_write(unsigned char cData) {
    SPDR = cData;
    while ( !(SPSR & (1<<SPIF))) ;
    return SPDR;
}
 
unsigned char SPI_read( void ) {
    SPDR = 0x0;
    while ( !(SPSR & (1<<SPIF))) ;
    return SPDR;
}
 
 
 
volatile unsigned char str[100];
 
 
ISR(SPI_STC_vect)
{
    static uint8_t idx=0;
    unsigned char ch = SPDR;
 
    if ( ch == '\0' )
    {
        str[idx]='\0';
        printString(str);
        idx=0;
    }
    else {
            str[idx]=ch;
            idx++;
    }   
 
 
}
 
int main (void)
{
    init_SPI();
    usartInit();
    sei();
   
    while(1)
    {
 
 
    }
}