Created
June 10, 2016 12:27
-
-
Save vnaskos/3145d1464b89531536165d5a372fc3dd to your computer and use it in GitHub Desktop.
Simple demonstration of SPI for atmega16 as slave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef F_CPU | |
#define F_CPU 1000000UL | |
#endif | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <util/delay.h> | |
#define ACK 0xF8 | |
void spi_init_slave(void); | |
uint8_t spi_tranceiver(uint8_t data); | |
void spi_init_slave(void) | |
{ | |
DDRB = (1<<PB6); //MISO as OUTPUT | |
SPCR = (1<<SPE); //Enable SPI | |
} | |
//Function to send and receive data | |
uint8_t spi_tranceiver(uint8_t data) | |
{ | |
SPDR = data; //Load data into buffer | |
while(!(SPSR & (1<<SPIF) )); //Wait until transmission complete | |
return(SPDR); //Return received data | |
} | |
int main(void) | |
{ | |
uint8_t data; | |
spi_init_slave(); //Initialize slave SPI | |
DDRA = 0xFF; //Initialize PORTA as OUTPUT | |
while(1) | |
{ | |
data = spi_tranceiver(ACK); //Receive data, send ACK | |
PORTA = data; //Display data on PORTA | |
_delay_ms(20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment