I have been trying to accomplish connecting ATmega32 and ATmega328 using SPI
Hardware connections:
ATmega328 ATmega32
master slave
------------------
MISO -> MISO
MOSI -> MOSI
SCK -> SCK
SS -> SS
I have added some code to blink LEDs after each step to recognize if a particular step is completed.
When connected, the LED connected to master blinks once while LED connected to the slave doesn't blink at all.
What have I done wrong?
Thanks for any advice.
Code for atmega328 master
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
//Initialize SPI Master Device
void spi_init_master (void)
{
DDRB = (1<<PORTB5)|(1<<PORTB3)|(1<<PORTB2); // Set MOSI, SCK , SS as Output
SPCR = (1<<SPE)|(1<<MSTR); // Enable SPI, Set as Master
}
//Function to send and receive data
unsigned char spi_tranceiver (unsigned char data)
{
PORTB &= ~(1<<PINB2); //sending low signal to activate slave
SPDR = data; // Load data into the buffer
while(!((SPSR)&(1<<SPIF))); // wait till transmission complete
PORTC |= (1<<PINC4);
_delay_ms(1000);
PORTC &= ~(1<<PINC4);
_delay_ms(1000);
return(SPDR); // Return received data
}
//Main
int main(void)
{
DDRC |= (1<<PINC4);
spi_init_master();
unsigned char t = spi_tranceiver(6);
for(unsigned char i=1;i<=t;i++){
PORTC = (1<<PINC4);
_delay_ms(500);
PORTC &= ~(1<<PINC4);
_delay_ms(500);
}
}
Code for ATmega32 slave
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
void spi_init_slave (void);
unsigned char spi_tranceiver (unsigned char data);
unsigned char lo;
int main(void)
{
DDRD |= (1<<PD7);
spi_init_slave();
while (1)
{
lo = spi_tranceiver(3);
for(unsigned char i=1;i<=lo;i++) {
PORTD |= (1<<PD7);
_delay_ms(100);
PORTD &= ~(1<<PD7);
_delay_ms(100);
}
}
}
// Initialize SPI Slave Device
void spi_init_slave (void)
{
DDRB &= ~(1<<PINB5); // Set MOSI as input
DDRB |= (1<<PINB6); // Set MISO as output
DDRB &= ~(1<<PINB4); // SS as input
SPCR = (1<<SPE); // Enable SPI
}
unsigned char spi_tranceiver (unsigned char data)
{
// Load data into the buffer
SPDR = data;
//Wait until transmission complete
while(!(SPSR & (1<<SPIF) ));
PORTD |= (1<<PD7);
_delay_ms(100);
PORTD &= ~(1<<PD7);
_delay_ms(100);
// Return received data
return(SPDR);
}