The Godfather talking
You think I am funny guy huh?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 10, 2016, 03:35:33 15:35


Login with username, password and session length


Pages: [1]
Print
Author Topic: Need help on SPI code for Atmega16  (Read 2577 times)
0 Members and 1 Guest are viewing this topic.
micropcb
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 29
-Receive: 150


« on: September 17, 2013, 08:30:41 08:30 »

Hi everyone,

I am trying to establish communication between two Atmega16s using SPI
One Atmega16 is configured as master and the other as slave.
The connections are as follows :

Master                  Slave
SCK          <--->       SCK
MOSI        <--->     MOSI
MISO        <--->     MISO
SS            <--->        SS

I am expecting to get the binary 1,2,3....etc on the master's portA (common anode LEDs) every one second.

But it does not work. I am using SS from the master to synchronize .
I shall be obliged if somebody can help me find the bug.
Codes attached.

Thanks in advance.


    
« Last Edit: September 17, 2013, 08:33:09 08:33 by micropcb » Logged
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #1 on: September 17, 2013, 10:10:05 10:10 »

The SPIF would clear after read both SPSR and SPDR. So the slave code may need to read out SPDR in order to clear the SPIF flag.

Try this code out on slave to see if it works,
Code:
while(1)
{
  SPDR = ++i;
  while ( !(SPSR & (1<<SPIF)));
  PORTA = ~SPDR;
}
Logged
micropcb
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 29
-Receive: 150


« Reply #2 on: September 17, 2013, 10:24:13 10:24 »

I tried that robotai but no response.
Attached proteus simulation and AVR studio files
Logged
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #3 on: September 17, 2013, 11:38:16 11:38 »

Well, it seems you have a mistyping on register flag. ((1<SPE))

correction on master.c
Code:
//SPCR = (1<SPE) | (1<<MSTR) | (1<<SPR0)|(1<<SPR1);
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0)|(1<<SPR1);

correction on slave.c
Code:
//SPCR = (1<SPE) | (1<<SPR0) |(1<<SPR1);
SPCR = (1<<SPE)| (1<<SPR0) |(1<<SPR1);
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2018

Thank You
-Given: 514
-Receive: 477


Top Topic Starter


« Reply #4 on: September 17, 2013, 01:26:11 13:26 »

http://www.rocketnumbernine.com/2009/04/26/using-spi-on-an-avr-1
Logged

nothing to say..
micropcb
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 29
-Receive: 150


« Reply #5 on: September 17, 2013, 02:13:22 14:13 »

Well, it seems you have a mistyping on register flag. ((1<SPE))

correction on master.c
Code:
//SPCR = (1<SPE) | (1<<MSTR) | (1<<SPR0)|(1<<SPR1);
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0)|(1<<SPR1);

correction on slave.c
Code:
//SPCR = (1<SPE) | (1<<SPR0) |(1<<SPR1);
SPCR = (1<<SPE)| (1<<SPR0) |(1<<SPR1);

How silly of me not to have noticed that !!
Now it works like a charm!!
Logged
micropcb
Junior Member
**
Offline Offline

Posts: 63

Thank You
-Given: 29
-Receive: 150


« Reply #6 on: September 17, 2013, 03:36:54 15:36 »

Here are the files of the working project for the use of anybody who may be interested.

Cheers! Smiley
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC