Go Down

Topic: SPI interface with other board_AVR_ESK1100 or among Arduino itself (Read 10 times) previous topic - next topic

Graynomad

That wasn't a stand-alone program, what happened to all your setup() code (most or which you don't need anyway I think).

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

ranjeetray

#86
Nov 17, 2012, 06:32 am Last Edit: Nov 17, 2012, 06:44 am by ranjeetray Reason: 1
Code: [Select]
[code][quote author=Nick Gammon link=topic=120454.msg999023#msg999023 date=1353098493]
[quote author=ranjeetray link=topic=120454.msg998331#msg998331 date=1353063009]
Can we do like this, after receiving data can we check(if condition) data and then send back data to master like this.
[/quote]

I really don't know what you are trying to do. You are doing an SPI.transfer in the middle of the ISR?

You send back data to the master [b]by assigning to SPDR in the interrupt service routine[/b]. No other way. What are you thinking?
[/quote]

Hi...!!

This way(SPDR = dat[0]) also it is not able to send back data to Master, and I am not able to give delay properly and synchronize with master. Slave is not getting chance to send back data, I think so.


[code]

#include "pins_arduino.h"
#include <SPI.h>
#define SS 10
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
byte c;
void ss_falling()
{
 c = 0;
}

void setup (void)
{

 // have to send on master in, *slave out*
 pinMode(MISO, OUTPUT);
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode (SPI_MODE2);
 SPI.setClockDivider(SPI_CLOCK_DIV64) ;

 // turn on SPI in slave mode
 SPCR |= _BV(SPE);

 // turn on interrupts
 SPCR |= _BV(SPIE);
 
// disable timer interrupts
 TIMSK0 = 0;
 // interrupt for SS falling edge
 attachInterrupt (0, ss_falling, FALLING);
  // disable timer interrupts
 TIMSK0 = 0;
 
}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
 c = SPDR;
 
 if(c == 0x04)
 {
   
   SPDR = dat[0];
 
}

  else if(c == 0x06)
 {
   SPDR = dat[1];
 
}
 else
SPDR = 0x00;    // what to return to the master



}  // end of interrupt service routine (ISR) SPI_STC_vect

void loop (void)
{

}  // end of loop

[/code][/code]

ranjeetray


That wasn't a stand-alone program, what happened to all your setup() code (most or which you don't need anyway I think).

______
Rob


Hi..!!

This code give following results, which have been attached here.

Code: [Select]



#include "pins_arduino.h"
#include <SPI.h>

#define MAX_BYTES 4  // is there 4 bytes? if not change this and the arrays.

byte cmd_04_data[MAX_BYTES] = {1,2,3,4}; // you fill in the numbers
byte cmd_06_data[MAX_BYTES] = {5,6,7,8};
byte * data_ptr;
int byte_count = 0;
void setup (void)
{
 
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode (SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);
 
// disable timer interrupts
  TIMSK0 = 0;
  // interrupt for SS falling edge
  //attachInterrupt (0, ss_falling, FALLING);
   // disable timer interrupts
  TIMSK0 = 0;
}
ISR (SPI_STC_vect) {
  byte c = SPDR;
  if (byte_count == 0) {
      // set a pointer to one or other array based on the byte just received
      data_ptr = (c == 0x04) ? cmd_04_data : cmd_06_data;
  }
  SPDR = *(data_ptr + byte_count);
  byte_count++;   
  if (byte_count = MAX_BYTES) byte_count = 0;
}
void loop (void)
{

}  // end of loop

Graynomad

I can't make out anything from the scope screen shots, there's obviously something happening though.

Tell you what I'll do, against my better judgement I'll try to write you some code for both ends based on what I think you want to do.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

ranjeetray


That wasn't a stand-alone program, what happened to all your setup() code (most or which you don't need anyway I think).

______
Rob


Hi..!!

With this code , attached result is coming.

Code: [Select]


#include "pins_arduino.h"
#include <SPI.h>

#define size_data 4  // is there 4 bytes? if not change this and the arrays.

byte send_04_data[size_data] = {1,2,3,7}; // you fill in the numbers
byte send_06_data[size_data] = {3,8,7,9};
byte * data_ptr;
int byte_count = 0;
void setup (void)
{
 
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode (SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);
 
// disable timer interrupts
  TIMSK0 = 0;
  // interrupt for SS falling edge
  //attachInterrupt (0, ss_falling, FALLING);
   // disable timer interrupts
  TIMSK0 = 0;
}
ISR (SPI_STC_vect) {
  byte c = SPDR;
  if (byte_count == 0)
  {
     
      data_ptr = (c == 0x04) ? send_04_data : send_06_data;
 
  SPDR = *(data_ptr + byte_count);
  byte_count++; } 
  else
 
  {
    SPDR = 0;      //******** Added
 
  }
  if (byte_count = size_data)
  byte_count = 0;
}
void loop (void)
{

}  // end of loop

Go Up