The Godfather talking
You may crack software. How about me?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 29, 2024, 03:11:24 15:11


Login with username, password and session length


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

Posts: 113

Thank You
-Given: 53
-Receive: 390


« on: September 17, 2013, 07:30:41 07: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, 07:33:09 07:33 by micropcb » Logged
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #1 on: September 17, 2013, 09:10:05 09: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
Active Member
***
 Muted
Offline Offline

Posts: 113

Thank You
-Given: 53
-Receive: 390


« Reply #2 on: September 17, 2013, 09:24:13 09: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, 10:38:16 10: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: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


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

http://www.rocketnumbernine.com/2009/04/26/using-spi-on-an-avr-1
Logged
micropcb
Active Member
***
 Muted
Offline Offline

Posts: 113

Thank You
-Given: 53
-Receive: 390


« Reply #5 on: September 17, 2013, 01:13:22 13: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
Active Member
***
 Muted
Offline Offline

Posts: 113

Thank You
-Given: 53
-Receive: 390


« Reply #6 on: September 17, 2013, 02:36:54 14: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