SPI-USI connection using ATMEGA8 and ATTINY2313

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

I'm having problem connecting an ATMEGA8 and ATTINY2313 over the SPI-USI link.

I'm using ATMEGA8 as master and ATTINY2313 as slave.

The code for the master is

#include 
#include 
#include

ISR(SIG_SPI)
{
PORTD=SPDR;
_delay_ms(100);
SPDR=0x03;


}

int main()
{
     DDRD=0xff;
     DDRB=(1<<PB5)|(1<<PB3);
     PORTB=(1<<PB4);
     SPCR=(1<<SPIE)|(1<<SPE)|(1<<MSTR);

     SPSR&=(1<<SPIF);
     sei();

     SPDR=0x01;
     while(1);
     return 0;
}

The code in the ATTINY slave is:

#include 
#include
#include

uint8_t _index=0;
char m[]="123";



ISR(USI_OVERFLOW_vect)
{
    PORTD=USIDR;
    USIDR=m[_index];
    if(_index<3)
	_index++;
    else
	_index=0;
    
    USISR |= (1 << USIOIF);
    _delay_ms(10);
}


int main()
{
DDRB=(1<<PB6);
PORTB=(1<<PB7)|(1<<PB5);
DDRD=0xff;
USICR=(1<<USIOIE)|(1<<USIWM0)|(1<<USICS1);
USISR |= (1 << USIOIF);

sei();
while(1);
return 0;
}

The problem is that the above code works great in Protues, but when I implement this on a breadboard, The program doesn't work.

Can anybody help? THanks in advance

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So you are running the master at FULL speed, can the slave keep up provided everything is programmed correctly?

I don't see any attempt at using a slave select signal.

Why are you using interrupts to send data via SPI the way you do? Would not a loop do just as well?

By the way you want SPI_STC_vect not SIG_SPI, should not be the cause of your problems however.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your reply...

I'm using Internal 1MHz Osc. for both ATMEGA8 and ATTINY2313. Is there a problem in this?

Yes I have used interrupts in both master and slave because this is the requirement of my project. Can you please tell me what's wrong in this code?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not really sure as to "what's wrong in this code". I would usually connect a debugger on the slave (and possibly one on the master) and see what's coming in\out.

However I have never used the USI for SPI. But for normal slave operation the data sheet says:

Quote:
When the SPI is configured as Slave, the SPI is only guaranteed to work at fosc/4 or lower.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it necessary to keep the same frequency for both the slave and master AVR?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is it necessary to keep the same frequency for both the slave and master AVR?

You have a basic misunderstanding about SPI. the master provides the clock, and sets the frequency. So I guess technically they would be at the same frequency--that which the master sends.

Quote:

I have used interrupts in both master and slave because this is the requirement of my project.

Tell more about that "requirement". Yes, it is quite common for slave SPI to be interrupt-driven. For master SPI on an AVR, however, there are very few cases where it would have an advantage, and indeed is quote a bit slower for common applications with small "messages".

Do yourself a favour and force PB2 - /SS to be an output so you don't jump into slave mode on the master.

Quote:

SPSR&=(1<<SPIF);

What do you think that line does? What does the datasheet say about clearing SPIF?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much the SPI-USI worked when I set the SS pin to be output!!!

Quote:

SPSR&=(1<<SPIF);

I wrote this to make sure that the SPI Interrupt is cleared when the program starts.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you must WRITE a 1 to SPIF to clear it, anding doesn't work with flags.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But you must WRITE a 1 to SPIF to clear it, anding doesn't work with flags.

js--
Quote:

What does the datasheet say about clearing SPIF?

:twisted:

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.