After a while spent trying to find out about the SPI capability of AVRs, I decided the best way to learn was to create my own library (an approach I have found works very well for beginners). I found an excellent example at https://sites.google.com/site/qe... and quickly turned it into a header file for easy reuse, adding a little more functionality for my own purposes. Since I am new to coding in raw C (I have escaped from the clutches of the Arduino IDE!), I thought it would be a good idea to see what the community here thinks of it, and if they have any advice to improve it. I also don't have the means to test it at the moment (that I am aware of), so I would appreciate it if anyone could try it out, following a successful build in ATMEL Studio 6. SPI.h is as follows:
/* * SPI.c * * Created: 20/12/2014 14:40:18 * Author: Benedict */ #ifndef SPI #define SPI #include <avr/io.h> #ifndef csPORT # warning "Define csPORT (default PORTB)" #define csPort PORTB #endif #ifndef cs1 # warning "Define cs1 if needed" #define cs1 0 #endif #ifndef cs2 # warning "Define cs2 if needed" #define cs2 0 #endif #ifndef cs3 # warning "Define cs3 if needed" #define cs3 0 #endif char dataIn; void spiInitMaster(void){ DDRB |= (1<<2)|(1<<3)|(1<<5); // SCK, MOSI and SS as outputs DDRB &= ~(1<<4); // MISO as input csPort |= (1<<cs1) | (1<<cs2) | (1<<cs3);//Set slaves high SPCR |= (1<<MSTR); // Set as Master SPCR |= (1<<SPR0)|(1<<SPR1); // divided clock by 128 SPCR |= (1<<SPE); // Enable SPI } void spiInitSlave(void){ DDRB &= ~((1<<2)|(1<<3)|(1<<5)); // SCK, MOSI and SS as inputs DDRB |= (1<<4); // MISO as output SPCR &= ~(1<<MSTR); // Set as slave SPCR |= (1<<SPR0)|(1<<SPR1); // divide clock by 128 SPCR |= (1<<SPE); } int spiReceive(void){//Call in while(1) while(!(SPSR & (1<<SPIF))); // wait until all data is received dataIn = SPDR; return dataIn; } void spiTransmit(char dataOut, int slavePin){//Call in while(1) csPort &= ~ 1<<slavePin; SPDR = dataOut; // send the data while(!(SPSR & (1<<SPIF))); } #endif
Thank you to anyone who takes the time to read this!