Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem with spi in atmega 32

Status
Not open for further replies.

sefat

Newbie level 4
Newbie level 4
Joined
Nov 17, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,317
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();
    }
}
 
Last edited by a moderator:

I am not a programmer?
Try to see the settings on both sides are correct. like CPHA,CPOL,MSB first or LSB first and basic features like Clock freq.
 

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();
    }
}

hi...
what sort of an o/p are u getting...?
hope u have taken into consideration that CPHA and CPOL of both SPI'S.
cant really figure out the reason for which u are ORing the data from SPI register with data on port D...?
if you only had to send the data to port d u could have straight away flushed it there.....


cheers...
Vijay
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top