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.

Atmega16 SPI communication

Status
Not open for further replies.

sanatdutta

Newbie level 3
Joined
May 23, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
IIT Madras Chennai
Activity points
1,305
hi i am new to programming, i had a doubt
in SPI while writing a char we just copy char to SPDR resistor

while reading we again copy char to SPDR then again copy it to some variable.

Please help:cry:
 

void WriteByteSPI(unsigned char byte)
{

SPDR = byte; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete

}

char ReadByteSPI(char addr)
{
SPDR = addr; //Load byte to Data register
while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
addr=SPDR;
return addr;
}




in UART communication for sending data we wait till UDRE resistor is set then we copy variable to UDR by this way it sends the data, while receiving we wait till RXC is set then we copy UDR to variable.

while in SPI receiving is different
 

In UART we have 2 registers for transmission and reception.

While in SPI the received data will also be stored in SPDR itself.

Nandhu
 

sanatdutta said:
in UART communication for sending data we wait till UDRE resistor is set then we copy variable to UDR by this way it sends the data, while receiving we wait till RXC is set then we copy UDR to variable.
while in SPI receiving is different

Yes in SPI, instead of the UDRE or RXC flags you will wait for the SPIF flag to go high which indicates that a transfer is complete.

If you do not set up the clock phase & clock polarity correctly then you will most likely have trouble making it work. Is it configured as a master or slave? What about the slave? How is it configured? What about the clock rate?

If people would learn assembly first and understand at least the basics of most microcontrollers before programming in c there would be a billion less posts in these forums asking what to do.

Doesn't anyone read the datasheet anymore?
 

don't forget! in SPI while it's sending a byte, it reads a byte...

so you could do bidirectional communication by sending a byte and wait until it's transmited and read the recieved byte on the same register...(that's your ReadSPI subroutine)!

if you first send a byte (your code will work!) and AFTER that, you should give pulses to read a byte; you can make a "dummy write" and read the spi register after that.. (like sending a read=ReadSPI(0xFF) ).
 

Kurenai_ryu said:
AFTER that, you should give pulses to read a byte; you can make a "dummy write" and read the spi register after that.. (like sending a read=ReadSPI(0xFF) ).
When using the hardware spi, you do not need to send pulses - the hardware does that for you.

You can find the SPI subroutine in both C and asm right in the datasheet (pages 138-139).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top