Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #90 on: November 17, 2012, 01:17:32 am » |
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
Thank You. It will be really a great help for me.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6806
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #91 on: November 17, 2012, 01:31:50 am » |
Here's some code. I took the razor to what you had and removed everything not required to prove the concept. Hopefully I didn't go too far. The master could still be a lot smaller. Master //Master sending data #include <SPI.h>// include the SPI library: #define SS_PIN 10
int command_array[] = {0x04, 0x06, 0x08, 0x0A}; int returned_data[4];
void setup() { pinMode (SS_PIN, OUTPUT);// set the spi_data_pin as an output:
Serial.begin(115200); SPI.begin(); SPI.setDataMode(SPI_MODE2); SPI.setClockDivider(SPI_CLOCK_DIV64) ; SPI.setBitOrder(LSBFIRST); }
void loop() { digitalWrite(SS_PIN,LOW); SPI.transfer(command_array[0]); // send command delay(1); // give the slave some time returned_data[0] = SPI.transfer(0); // get response digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[1]); returned_data[1] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[2]); returned_data[2] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[3]); returned_data[3] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
Serial.print (returned_data[0]); Serial.print (','); Serial.print (returned_data[1]); Serial.print (','); Serial.print (returned_data[2]); Serial.print (','); Serial.println (returned_data[3]); } Slave byte response_data[] = {-1,-1,-1,-1,'4',-1,'6',-1,'8',-1,'A'};
void setup (void) { pinMode(MISO, OUTPUT); // have to send on master in, *slave out* SPCR |= _BV(SPE); // turn on SPI in slave mode SPCR |= _BV(SPIE); // turn on interrupts }
void loop () {}
ISR (SPI_STC_vect) { byte command = SPDR;
SPDR = response_data[command];
}
See if that does anything useful. ______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #92 on: November 17, 2012, 02:04:53 am » |
Hi...!! I am reading http://www.gammon.com.au/spi again and again and trying to grasp SPI, but not getting expected results. Can we write the codes like this, are these correct codes for mater and slave ... Slave receiving data from master then sending the data back to master, is it right. ...
// SPI interrupt routine ISR (SPI_STC_vect) { c = SPDR; if(c == 0x04) { digitalWrite(SS, LOW); SPI.transfer(dat[0]); SPI.transfer(dat[1]); digitalWrite(SS, HIGH); digitalWrite(SS, LOW); SPI.transfer(dat[2]); SPI.transfer(dat[3]); digitalWrite(SS, HIGH); digitalWrite(SS, LOW); SPI.transfer(dat[4]); SPI.transfer(dat[5]); digitalWrite(SS, HIGH); } else if(c == 0x06) { digitalWrite(SS, LOW); SPI.transfer(dat[6]); SPI.transfer(dat[7]); digitalWrite(SS, HIGH); digitalWrite(SS, LOW); SPI.transfer(dat[8]); SPI.transfer(dat[9]); digitalWrite(SS, HIGH); digitalWrite(SS, LOW); SPI.transfer(dat[10]); SPI.transfer(dat[11]); digitalWrite(SS, HIGH); } else SPDR = 0x00; // what to return to the master
} // end of interrupt service routine (ISR) SPI_STC_vect ...
Look, ranjeetray, you are just ignoring everything I am saying. I said this: Can we do like this, after receiving data can we check(if condition) data and then send back data to master like this.
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 by assigning to SPDR in the interrupt service routine. No other way. What are you thinking? You do not use SPI.transfer in the ISR. Do you have trouble understanding that? You are ignoring me, you are just making stuff up, and then taking screen shots of the scope output. It won't work. Graynomad has offered to write an example but that won't help if you don't make an attempt to understand things. Don't just copy and paste and hope for the best.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #93 on: November 17, 2012, 02:35:15 am » |
Here's some code. I took the razor to what you had and removed everything not required to prove the concept. Hopefully I didn't go too far. The master could still be a lot smaller. Master //Master sending data #include <SPI.h>// include the SPI library: #define SS_PIN 10
int command_array[] = {0x04, 0x06, 0x08, 0x0A}; int returned_data[4];
void setup() { pinMode (SS_PIN, OUTPUT);// set the spi_data_pin as an output:
Serial.begin(115200); SPI.begin(); SPI.setDataMode(SPI_MODE2); SPI.setClockDivider(SPI_CLOCK_DIV64) ; SPI.setBitOrder(LSBFIRST); }
void loop() { digitalWrite(SS_PIN,LOW); SPI.transfer(command_array[0]); // send command delay(1); // give the slave some time returned_data[0] = SPI.transfer(0); // get response digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[1]); returned_data[1] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[2]); returned_data[2] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[3]); returned_data[3] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
Serial.print (returned_data[0]); Serial.print (','); Serial.print (returned_data[1]); Serial.print (','); Serial.print (returned_data[2]); Serial.print (','); Serial.println (returned_data[3]); } Slave byte response_data[] = {-1,-1,-1,-1,'4',-1,'6',-1,'8',-1,'A'};
void setup (void) { pinMode(MISO, OUTPUT); // have to send on master in, *slave out* SPCR |= _BV(SPE); // turn on SPI in slave mode SPCR |= _BV(SPIE); // turn on interrupts }
void loop () {}
ISR (SPI_STC_vect) { byte command = SPDR;
SPDR = response_data[command];
}
See if that does anything useful. ______ Rob //Master sending data #include <SPI.h>// include the SPI library: #define SS_PIN 10
int command_array[] = {0x04, 0x06, 0x08, 0x0A}; int returned_data[4];
void setup() { pinMode (SS_PIN, OUTPUT);// set the spi_data_pin as an output:
Serial.begin(115200); SPI.begin(); SPI.setDataMode(SPI_MODE2); SPI.setClockDivider(SPI_CLOCK_DIV64) ; SPI.setBitOrder(LSBFIRST); }
void loop() { digitalWrite(SS_PIN,LOW); SPI.transfer(command_array[0]); // send command delay(1); // give the slave some time returned_data[0] = SPI.transfer(0); // get response digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[1]); returned_data[1] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[2]); returned_data[2] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); delay(1); SPI.transfer(command_array[3]); returned_data[3] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
Serial.print (returned_data[0]); Serial.print (','); Serial.print (returned_data[1]); Serial.print (','); Serial.print (returned_data[2]); Serial.print (','); Serial.println (returned_data[3]); }
With the following code, attached image 2 and TEK0002 are the output at Master Serial Monitor and on Oscilloscope: #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; } 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; } if (byte_count = size_data) byte_count = 0; } void loop (void) { } // end of loop
With the following code image 3 and TEK0006 are output: #include "pins_arduino.h" #include <SPI.h> byte response_data[] = {1,2,-1,3,'4',-1,'6',-1,'8',-1,'A'}; 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; }
void loop (void) { } // end of loop
ISR (SPI_STC_vect) {
byte command = SPDR;
SPDR = response_data[command];
}
But Slave is not sending data back properly.
|
|
|
|
« Last Edit: November 17, 2012, 02:47:08 am by ranjeetray »
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 71
Posts: 6806
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #94 on: November 17, 2012, 03:21:14 am » |
Do you deliberately not use the code provided to wind us up or what? Why did you change the array and put all the SPI setup code back in?
Use the slave code I posted. It may not be correct but it's a lot better than what you have I think.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #95 on: November 17, 2012, 03:31:17 am » |
if (byte_count = size_data) byte_count = 0; I think it's a troll.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #96 on: November 17, 2012, 04:09:36 am » |
I've worn a groove in my tablet's screen scrolling through this thread. Nick, you deserve a medal.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #97 on: November 17, 2012, 05:29:45 am » |
I've worn a groove in my tablet's screen scrolling through this thread. Nick, you deserve a medal.
I appreciate for your effort.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #98 on: November 19, 2012, 01:39:11 am » |
I've worn a groove in my tablet's screen scrolling through this thread. Nick, you deserve a medal.
Hi...!!! I have tried all the possible combinations , Slave is not able to send back data to master for each if conditions. #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;
int m, n ;
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;
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
c = SPDR;
if(c == 0x00)
{
SPDR = 0x01;
}
else if(c == 0x06)
{
SPDR = 0x02;
}
else if(c == 0x08)
{
SPDR = 0x03;
}
else if(c == 0x0A)
{
SPDR = 0x07;
}
else
SPDR = 0; // what to return to the master
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
{
} // end of loop
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #99 on: November 19, 2012, 01:42:38 am » |
What is the master code? And it might be quicker to just index into an array.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #100 on: November 19, 2012, 01:43:56 am » |
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
What is the point of this?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #101 on: November 19, 2012, 01:46:11 am » |
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
What is the point of this? Instead of sending SPDR = 0x01 or, data I was trying to send SPDR = dat[0]; but that also dint work.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #102 on: November 19, 2012, 01:55:22 am » |
[quote author=Nick Gammon link=topic=120454.msg1001647#msg1001647 date=1353307358] What is the master code? And it might be quicker to just index into an array. [/quote] Master code on AVR:
[code] /** * \file
* Include header files for all drivers that have been imported from * AVR Software Framework (ASF). */ #include <asf.h> #include <avr32/io.h> #include "pm.h" #include "delay.h"
int16_t *readData,i; unsigned int timeout = 1500; int16_t cmdAddr[30] = {0x0400,0x0600,0x0800,0x0A00,0x0C00,0x0E00,0x1000,0x1200,0x1400}; //int16_t cmdAddr[30] = {0x04, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08,0x00,0x00, 0x00, 0x0A,0x00, 0x00, 0x00,0x0C, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00}; int main (void) { pm_switch_to_osc0(&AVR32_PM,12000000,2048); // Switch the main clock to the external oscillator 0 delay_init(12000000); // delay_ms(20); // busy wait for 20ms
AVR32_GPIO.port[0].gperc = 1 << 10; // A peripheral function controls the corresponding pin AVR32_GPIO.port[0].gperc = 1 << 11; // A peripheral function controls the corresponding pin AVR32_GPIO.port[0].gperc = 1 << 12; // A peripheral function controls the corresponding pin AVR32_GPIO.port[0].gperc = 1 << 13; // A peripheral function controls the corresponding pin AVR32_GPIO.port[0].pmr0c = 1 << 10; // To select the peripheral function A AVR32_GPIO.port[0].pmr1c = 1 << 10; // To select the peripheral function A AVR32_GPIO.port[0].pmr0c = 1 << 11; // To select the peripheral function A AVR32_GPIO.port[0].pmr1c = 1 << 11; // To select the peripheral function A AVR32_GPIO.port[0].pmr0c = 1 << 12; // To select the peripheral function A AVR32_GPIO.port[0].pmr1c = 1 << 12; // To select the peripheral function A AVR32_GPIO.port[0].pmr0c = 1 << 13; // To select the peripheral function A AVR32_GPIO.port[0].pmr1c = 1 << 13; // To select the peripheral function A AVR32_SPI0.MR.mstr = 1; // SPI operates in MASTER mode AVR32_SPI0.MR.modfdis = 0; // Mode Fault Detection disabled AVR32_SPI0.MR.llb = 0; // Local loop path disabled AVR32_SPI0.MR.ps = 0; // Fixed peripheral is selected AVR32_SPI0.MR.pcsdec = 0 ; // The chip selects are directly connected to a peripheral device AVR32_SPI0.MR.dlybcs = 0; // Delay between chip selects AVR32_SPI0.MR.pcs = 0xF; // Peripheral chip select disable AVR32_SPI0.CSR0.scbr = 0x28; // Serial Clock Baud Rate initialized to 40 AVR32_SPI0.CSR0.dlybs = 0x0a; // Delay before serial clock signal (SPCK) AVR32_SPI0.CSR0.dlybct = 0x12; // Delay between consecutive transfers AVR32_SPI0.CSR0.csaat = 1; // Chip Select Active After Transfer AVR32_SPI0.CSR0.cpol = 1 ; // Clock polarity AVR32_SPI0.CSR0.ncpha = 0 ; // Clock phase AVR32_SPI0.CSR0.bits =0x8; // Number of data bits transferred. while(1) { readData = 0x500; i = 0; AVR32_SPI0.CR.spien = 1; // Enables the SPI to transfer and receive data while(i!=9) { AVR32_SPI0.MR.pcs = 0xE; // Peripheral Chip Select (CSR0) enable while (AVR32_SPI0.SR.tdre != 1); // Transmit Data Register Empty AVR32_SPI0.TDR.td = cmdAddr[i]; // send command address to TDR register while(AVR32_SPI0.SR.tdre!=1); // Transmit data ready empty status AVR32_SPI0.TDR.td = 0x0000; while(AVR32_SPI0.SR.rdrf!=1); // Read data register full status readData[i] = AVR32_SPI0.RDR.rd; // Store read data into an array i++; AVR32_SPI0.MR.pcs = 0xF; // Disable the peripheral chip select (CSR0) delay_ms(2); } AVR32_SPI0.CR.spidis = 1; // Disables the SPI, it finishes its transfer delay_ms(20); // busy wait for 20ms } while(1); }
And Master AVR is reading just FFFFFFFFFFFFFFFFF , what could be the reason. Master code on Arduino //Master sending data #include <SPI.h>// include the SPI library: #define SS_PIN 10
int command_array[] = {0x04, 0x06, 0x08, 0x0A}; int returned_data[4];
void setup() { pinMode (SS_PIN, OUTPUT);// set the spi_data_pin as an output:
Serial.begin(115200); SPI.begin(); SPI.setDataMode(SPI_MODE2); SPI.setClockDivider(SPI_CLOCK_DIV64) ; SPI.setBitOrder(LSBFIRST); }
void loop() { digitalWrite(SS_PIN,LOW); SPI.transfer(command_array[0]); // send command //delay(1); // give the slave some time returned_data[0] = SPI.transfer(0); // get response digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); //delay(1); SPI.transfer(command_array[1]); returned_data[1] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); //delay(1); SPI.transfer(command_array[2]); returned_data[2] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW); //delay(1); SPI.transfer(command_array[3]); returned_data[3] = SPI.transfer(0); digitalWrite(SS_PIN,HIGH);
Serial.print (returned_data[0]); Serial.print (','); Serial.print (returned_data[1]); Serial.print (','); Serial.print (returned_data[2]); Serial.print (','); Serial.println (returned_data[3]); }
Master code on Arduino Again //Master Program #include <SPI.h>// include the SPI library: #define SCK_PIN 13 #define MISO_PIN 12 #define MOSI_PIN 11
const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot: int array[30] = {0x04, 0x00, 0x00, 0x00, 0x06, 0x00,0x00, 0x00, 0x08,0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00}; int chr;
void setup() { pinMode (spidata, OUTPUT);// set the spi_data_pin as an output: SPI.begin();// initialize SPI: Serial.begin(115200); SPI.setDataMode(SPI_MODE2); SPI.setClockDivider(SPI_CLOCK_DIV64) ; SPI.setBitOrder(LSBFIRST); digitalWrite(spidata,LOW); }
void loop() { int data[10]; char ch;
//delay(14); digitalWrite(spidata,LOW); SPI.transfer(array[0]); //delay(1); SPI.transfer(array[1]); // delay(1); digitalWrite(spidata,HIGH); //delay(14); digitalWrite(spidata,LOW); SPI.transfer(array[2]); //delay(1); SPI.transfer(array[3]); // delay(1); digitalWrite(spidata,HIGH); // delay(14); digitalWrite(spidata,LOW); SPI.transfer(array[4]); //delay(1); SPI.transfer(array[5]); //delay(1); digitalWrite(spidata,HIGH); //delay(1); digitalWrite(spidata,LOW); SPI.transfer(array[6]); //delay(1); SPI.transfer(array[7]); // delay(1); digitalWrite(spidata,HIGH); digitalWrite(spidata,LOW); SPI.transfer(array[8]); // delay(1); SPI.transfer(array[9]); //delay(1); digitalWrite(spidata,HIGH); digitalWrite(spidata,LOW); SPI.transfer(array[10]); // delay(1); SPI.transfer(array[11]); // delay(1); digitalWrite(spidata,HIGH); //delay(1); digitalWrite(spidata,LOW); SPI.transfer(array[12]); //delay(1); SPI.transfer(array[13]); // delay(1); digitalWrite(spidata,HIGH); // delay(14); digitalWrite(spidata,LOW); SPI.transfer(array[14]); //delay(1); SPI.transfer(array[15]); // delay(1); digitalWrite(spidata,HIGH); // delay(14); digitalWrite(spidata,LOW); SPI.transfer(array[16]); //delay(1); SPI.transfer(array[17]); // delay(1); digitalWrite(spidata,HIGH); //delay(14); digitalWrite(spidata,LOW); SPI.transfer(array[18]); //delay(1); SPI.transfer(array[19]); //delay(1); digitalWrite(spidata,HIGH); digitalWrite(spidata,LOW); SPI.transfer(array[20]); //delay(1); SPI.transfer(array[21]); // delay(1); digitalWrite(spidata,HIGH); digitalWrite(spidata,LOW); SPI.transfer(array[22]); // delay(1); SPI.transfer(array[23]); // delay(1); digitalWrite(spidata,HIGH); // delay(14); }
Slave code on Arduino //Slave 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; int m, n ; 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; } // end of setup // SPI interrupt routine ISR (SPI_STC_vect) { c = SPDR; if(c == 0x04) { SPDR = 0x01; } else if(c == 0x06) { SPDR = 0x02; } else if(c == 0x08) { SPDR = 0x03; } else if(c == 0x0A) { SPDR = 0x07; } //else //SPDR = 0; // what to return to the master } // end of interrupt service routine (ISR) SPI_STC_vect void loop (void) { } // end of loop
Exceeding the 9500 charecters. [/code]
|
|
|
|
« Last Edit: November 19, 2012, 02:09:35 am by ranjeetray »
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 0
Posts: 108
|
 |
« Reply #103 on: November 19, 2012, 02:01:12 am » |
What is the master code? And it might be quicker to just index into an array.
Other Slave codes are: #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; } 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
byte response_data[] = {-1,-1,-1,-1,'4',-1,'6',-1,'8',-1,'A'};
void setup (void) { pinMode(MISO, OUTPUT); // have to send on master in, *slave out* SPCR |= _BV(SPE); // turn on SPI in slave mode SPCR |= _BV(SPIE); // turn on interrupts }
void loop () {}
ISR (SPI_STC_vect) { byte command = SPDR;
SPDR = response_data[command];
}
|
|
|
|
« Last Edit: November 19, 2012, 02:03:53 am by ranjeetray »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #104 on: November 19, 2012, 02:59:22 am » |
Master code on Arduino ... Master code on Arduino Again ... Other Slave codes are:
What?
|
|
|
|
|
Logged
|
|
|
|
|
|