Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

problem with spi in atmega 32

Status
Not open for further replies.

sefat

Newbie level 4
Joined
Nov 17, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
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:

malli_1729

Full Member level 5
Joined
Feb 13, 2007
Messages
248
Helped
47
Reputation
94
Reaction score
46
Trophy points
1,308
Location
INDIA
Activity points
3,187
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.
 

vijayjadhav.595

Member level 1
Joined
Sep 22, 2012
Messages
40
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Mumbai,India
Activity points
1,537
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
 

dean_winchester

Advanced Member level 4
Joined
Oct 19, 2010
Messages
111
Helped
22
Reputation
44
Reaction score
21
Trophy points
1,298
Location
India
Activity points
1,826
I will say.. check the connections.. MISO to MISO and MOSI to MOSI
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top