Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / SPI between Arduino and AVR on: April 02, 2013, 02:38:42 pm
Hi everyone,

I am doing a project which involves using an Arduino Uno as a master device with SPI communication to AVR Atmega32 devices as slaves. I have experimented a lot with SPI and can get it working perfectly between two AVRs or two Arduinos, however, when I try to get them to communicate between an Arduino and AVR I run into problems. The first ten or so transmissions immediately after hitting "Reset" on the Arduino transmit fine (the resets are tied also), but after that I read mostly 0xFF and occasionally a random byte from the master side (read through Serial Monitor). If anyone has any idea why it is doing this PLEASE HELP!

Thank you so much!!!

The code is shown below:

Master Code:
Code:
//==================================================================
//Arduino Uno (Master)
//==================================================================

#include <SPI.h>

int SLAVESELECT=10;
int DD_MOSI=11;
int DD_MISO=12;
int DD_SCK=13;
byte buffer[6]={0x01,0x02,0x03,0x04,0x05,0x06};

void setup()
{
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
Serial.begin(9600);
pinMode(SLAVESELECT,OUTPUT);
pinMode(DD_MOSI,OUTPUT);
pinMode(DD_MISO,INPUT);
pinMode(DD_SCK,OUTPUT);
digitalWrite(SLAVESELECT,HIGH);
}

void loop(void)
{
digitalWrite(SLAVESELECT,LOW);  //enable Slave Select
  for(int i=0;i<6;i++)
  {
   byte x = SPI.transfer(buffer[i]);
   Serial.print("master:");
   Serial.println(buffer[i], HEX);
   Serial.println(x, HEX);
   delay(200);  
  }
digitalWrite(SS,HIGH);
}

Slave Code
Code:
//=====================================================================
//ATmega32 (Slave)
//=====================================================================
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <stdlib.h>

#define DD_SS    PINB4  //0
#define DD_MOSI    PINB5 //0
#define DD_MISO    PINB6 //1
#define DD_SCK     PINB7 //0
#define DDR_SPI    DDRB  //0100 0000

void SPI_SlaveInit(void)
{
DDRB = 0x40; // set MOSI as input pin, MISO output
PORTB = 0xB0;           //1011 0000
SPCR=(1<<SPE); // Enable SPI
SPSR=0;
}

char SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)));
/* Return data register */
return SPDR;
}

int main(void){
SPI_SlaveInit();
while(1)
{
PORTD =  SPI_SlaveReceive(); // move SPDR value to POTRD
_delay_ms(1000);
}
}
2  Using Arduino / Networking, Protocols, and Devices / [SPI] Many master SS pins on Due? on: March 14, 2013, 08:14:36 pm
Hi everyone smiley-surprise

My name is Melissa, I'm new here and hope someone can help me with this.

I am using an Arduino Due and I want to receive a bitstream from many AVR microcontrollers (about 45). I am using the Due as the master device. However the Due's SPI page says it only supports pins 4, 10, and 52 for the master SS line from the Due...I have no idea why this is because AVRs can have almost any I/O pin be an SS line for the master so I decided to try using other pins other than 4, 10 and 52 as master SS lines.

I currently have many other pins acting as SS lines from the Due and it compiled fine but I'm only receiving "0xFFFFFF" on all lines  smiley-confuse, even 4, 10 and 52. I was wondering if anyone knew if the Arduino really does restrict SS lines to 4/10/52 or if its probably some other error. Wanted to rule this out before I went crazy trying to figure out why it wasn't working. I looked all over the internet but couldn't find any information on this. I guess no one has tried to tie 45 AVRs to an Arduino before.

Thanks! I hope this question was clear...

-Melissa
Pages: [1]