SPI

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

Hi!
How can I get two AVR8515 to talk to eashother. I´ve tried with SPI but I don´t now how to implement it. I´ve tried many diffrent way but nothing seems to work.

I´m using CodeVision compilier maybe there are some limits in this?

admin's test signature
 

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

Hi,

Here's some code a friend of mine once wrote. There are two files. One for master 8515, and another for slave 8515.

Master 8515:
*****************************************************************************************

/* code example: SPI master
compiled using IAR Embedded Workbench 1.51C
*/

#include "iom163.h"
#include "ina90.h"

#define MOSI DDB5
#define MISO DDB6
#define SS DDB4
#define SCK DDB7

unsigned char data = 0;

interrupt [SPI_STC_vect] void ISR_SPI(void)
{
SPDR = data++; //send the value of data and increment data
}

void main(void)
{
// Data direction of SPI lines
DDRB |= 1<<MOSI; //Output
DDRB |= 0<<MISO; //Input (Automaticly set as input in master mode)
DDRB |= 0<<SS; //Input (Should be output on multi-master bus)
DDRB |= 1<<SCK; //Output

//SPI mode
SPCR |= 1<<MSTR; //select SPI master mode
SPCR |= 1<<SPIE; //Enable SPI interrupt
SPCR |= (1<<SPR1)|(1<<SPR0); //setting SPI speed fck/128
SPCR |= 1<<SPE; //Enable SPI

_SEI(); //Enable global interrupts

SPDR = data; //Transmit the value of data trough SPI
//and increment data
while(1); //loop for a while
}

Slave 8515:
************************************************************************************************

/* code example: SPI slave
compiled using IAR Embedded Workbench 1.51C
*/

#include "iom163.h"
#include "ina90.h"

#define MOSI DDB5
#define MISO DDB6
#define SS DDB4
#define SCK DDB7

unsigned char data = 0;

interrupt [SPI_STC_vect] void ISR_SPI(void)
{
PORTD = ~SPDR; //Place the received value on PORTD
}

void main(void)
{
//Data direction on PORTD
DDRD = 0xff; //All pins used for output

// Data direction of SPI lines
DDRB |= 0<<MOSI; //Input (Automaticly set as input in slave mode)
DDRB |= 1<<MISO; //Output
DDRB |= 0<<SS; //Input (Automaticly set as input in slave mode)
DDRB |= 0<<SCK; //Input (Automaticly set as input in slave mode)

//SPI mode
SPCR |= 0<<MSTR; //select SPI master mode
SPCR |= 1<<SPIE; //Enable SPI interrupt
SPCR |= 1<<SPE; //Enable SPI

_SEI(); //Enable global interrupts

while(1); //loop for a while
}

Does this do the trick? If you're using assembly, it's pretty straightforward conversion...

Corinthian

admin's test signature
 

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

Hi!
Thanks!
We wonder were we can find the headerfiles that are included in the code.

Lars

admin's test signature
 

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

Hi,

Those are included in the IAR compiler he's using. Other compilers will use other header files. What compiler are you using? If it's made for AVR, there should be a corresponding file. I could mail them to you if I've got the right one, provided I get your e-mail address :)

The iom163 file contains definitions such as DDRB and SPE, e.g. links between these names and the appropriate memory addresses. The ina90 contains intrinsic commands such as SEI(); Look for files containing these definitions.

Corinthian

admin's test signature
 

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

Hi!
We are using the CodeVision compiler but we also have the IAR compiler but we can´t find the iom163.h file. It would be nice if you could mail it to us. My emailadress is:peha1377@student.uu.se

Per Hammer

admin's test signature
 

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

Hi,

I've sent the ones I found to you. They're IAR compiler files, but you'll probably be able to port them for such simple code.

Corinthian

admin's test signature