i'm using two atmega 32 for spi.
master will send 0 to 100 to slave and the slave will show it on PORTD.
i'm not getting the expected result.
whats the prblm.plz help.
here is the code.
for master:
Code:
#include <avr/io.h>
#include <avr/delay.h>
#define DDR_SPI DDRB
#define DD_MOSI PB5
#define DD_SCK PA7
#define DD_SS PA4
void SPI_MasterInit(void)
{
DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS); /* Set MOSI and SCK output, all others input */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); /* Enable SPI, Master, set clock rate fck/16 */
}
void SPI_MasterTransmit(char cData)
{
SPDR = cData; /* Start transmission */
while(!(SPSR & (1<<SPIF))); /* Wait for transmission complete */
}
int main(void)
{ unsigned char i;
SPI_MasterInit();
while(1)
{ unsigned int ch=0;
for(i=0;i<=100;i++){
SPI_MasterTransmit(ch);
_delay_ms(1000);
ch++;}
}
}
for slave:
Code:
#include <avr/io.h>
#define DDR_SPI DDRB
#define DD_MISO PB7
void SPI_SlaveInit(void)
{ DDRB=0x00;
/* Set MISO output, all others input */
//DDR_SPI = (1<<DD_MISO);
/* Enable SPI */
SPCR = (1<<SPE);
}
char SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return data register */
return SPDR;
}
int main(void)
{ DDRD=0xFF;
PORTD=0;
DDRB=0x00;
SPI_SlaveInit();
while(1)
{
PORTD|=SPI_SlaveReceive();
}
}