Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #define SS 4
- #define MOSI 5
- #define MISO 6
- #define SCK 7
- #define SS_Enable PORTB &= ~(1<<SS)
- #define SS_Disable PORTB |= (1<<SS)
- void SPI_Master_Init()
- {
- DDRB |= (1<<MOSI)|(1<<SCK)|(1<<SS);
- DDRB &= ~(1<<MISO);
- SS_Disable;
- SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
- SPSR &= ~(1<<SPI2X);
- }
- void SPI_Slave_Init()
- {
- DDRB &= ~((1<<MOSI)|(1<<SCK)|(1<<SS));
- DDRB |= (1<<MISO);
- SPCR = (1<<SPE);
- }
- void SPI_Master_Write(char data)
- {
- char flush_buffer;
- SPDR = data;
- while(!(SPSR & (1<<SPIF)));
- flush_buffer = SPDR;
- }
- char SPI_Slave_Transmit(char data)
- {
- SPDR = data;
- while(!(SPSR & (1<<SPIF)));
- return(SPDR);
- }
- char SPI_Master_Read()
- {
- SPDR = 0xFF;
- while(!(SPSR & (1<<SPIF)));
- return(SPDR);
- }
- char SPI_Slave_Receive()
- {
- while(!(SPSR & (1<<SPIF)));
- return(SPDR);
- }
Advertisement
Add Comment
Please, Sign In to add comment