I'm trying communicate between two Arduino to Spi .. I can not send to more than 8 bits at a time I have to make a delay. then I have an arduino the Master and one Slave when the Slave responds Master can not receive anything.
I want somebody who could ever help..
code Master
#include <avr/io.h>
#include <util/delay.h> // delay eg _delay_ms(6);
#include <stdio.h>
void setup()
{
Serial.begin(9600);
/* SETTING CLOCK PRESCALER TO 1 */
CLKPR = 0x80; // Enable change to prescaler
CLKPR = 0x00; // Set clock prescaler to 1
/* SETTING UP PORT DIRECTIONS */
DDRB = 0b11101111; // All output except MISO (PB4)
/* SETTING UP SPI */
SPCR = 0x50; // Enable SPI in MSB-first mode, disable SPI interrupt and set SPI clock prescaler to 4
// SPSR |= 0x01; // Enable double speed (f_spi-clk = f_clk/2)
SPSR &= 0xFE; // Disable double speed
}
void SPI_MasterTransmit(unsigned char data_byte)
{
SPDR = data_byte;
PORTB &= 0xFB; // Pull SS low to enable the slave
while(!(SPSR & (1<<SPIF)));
PORTB |= 0x04; // Pull SS high to disable the slave
}
void loop()
{
int data;
//setup(); // Set registers for interrupts to run etc
delay(5000);
SPI_MasterTransmit('l');
delay(10);
SPI_MasterTransmit('c');
SPI_MasterTransmit('d');
data= SPI_SlaveReceive();
Serial.println(data);
}
unsigned char SPI_SlaveReceive()
{
while(!(SPSR & (1<<SPIF)));
return SPDR;
}
code Slave
#include <stdio.h>
#include <avr/io.h>
#include <SPI.h>
#define MOSI 11
#define MISO 12
#define SCK 13
#define SS 10
int array [3];
int array2[] = {108,99,100};
unsigned char data = 2;
unsigned char scrap;
int counter = 0;
void setup()
{
int i=0;
pinMode(MOSI, INPUT);
pinMode(MISO, OUTPUT);
pinMode(SCK,INPUT);
pinMode(SS,INPUT);
SPI.begin();
Serial.begin(9600);
Serial.println("Enabling SPI in slave mode");
SPCR = 0x40; // Enable SPI in slave mode
delay(500);
Serial.println("Initialisation complete.");
for(i=0;i<3;i++){
array*= SPI_SlaveReceive();*
_ Serial.println(array*);*_
* //Serial.println(data);*
* }*
}
int i;
void loop()
{
int da;
_ /i++;_
_ data=0;_
_ Serial.print("loop");*_
* Serial.println(i);*
* data = SPI_SlaveReceive();
_ Serial.println(data);*_
for(i=0;i<11;i++){
da=array*;*
Serial.println(data,DEC);
delay(1000);
}
_ /
//delay(1000);
if (array_cmp(array, array2, 3, 3) == true){
// do this if they are equal*
* da=5;*_
* digitalWrite(SS,LOW);*
* SPI.transfer(da);*
* digitalWrite(SS,HIGH);*
* Serial.println("Entrou7777777");*
* Serial.println(da,HEX);*
* }else{*
* // do this if they are different*
* }*
}
unsigned char SPI_SlaveReceive()
{
* while(!(SPSR & (1<<SPIF)));*
* return SPDR;*
}
void SPI_SlaveTransmit(unsigned char data_byte)
{
* SPDR = data_byte;
_ PORTB &= 0xFB; // Pull SS low to enable the slave*
* while(!(SPSR & (1<<SPIF)));
PORTB |= 0x04; // Pull SS high to disable the slave*
}
boolean array_cmp(int *a, int *b, int len_a, int len_b){
* int n;
Serial.println("Entrou");
// if their lengths are different, return false*
* if (len_a != len_b) return false;*
* // test each element to be the same. if not, return false*
* for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;*
* //ok, if we have not returned yet, they are equal
return true;
}*_