Thanks for all the replies.
I was able to implement Slave receive interrupt, Master is transmitting data via polling.
Code C++ - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| // this program enables SPI communication and
// Sets the AVR into Master mode
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
char data = 'c';
//initUSART();
//printString("MASTER\r\n");
DDRB |= (1<<2)|(1<<3)|(1<<5); // SCK, MOSI and SS as outputs
DDRB &= ~(1<<4); // MISO as input
SPCR |= (1<<MSTR); // Set as Master
SPCR |= (1<<SPR0)|(1<<SPR1); // divided clock by 128
SPCR |= (1<<SPE); // Enable SPI
while(1)
{
SPDR = data; // send the data
while(!(SPSR & (1<<SPIF))); // wait until transmission is complete
//transmitByte(SPDR);
//data = SPDR;
_delay_ms(100);
// if you have multiple slaves, this is where you want to switch
}
} |
Code C++ - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| // this program enables SPI communication and
// Sets the AVR into Slave mode
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "USART.h"
volatile uint8_t data;
volatile uint8_t data;
void spi_init_slave (void)
{
DDRB=(1<<PINB4); //MISO as OUTPUT
SPCR=(1<<SPE)|(1<<SPIE); //Enable SPI && interrupt enable bit
SPDR=0;
}
ISR(SPI_STC_vect)
{
uint8_t y = SPDR;
PORTC = y;
transmitByte(y);
}
int main(void)
{
initUSART();
printString("Slave\r\n");
DDRC = 0XFF;
spi_init_slave(); //Initialize slave SPI
sei();
while(1)
{
}
} |
This one works.
How can I make master to transmit data using interrupt??
and slave to transmit data to master using interrupt??
use the /SS signals. They usually enable the slave's MISO output drivers, and they are used to synchronize "bit to byte"
Code C++ - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| DDRB |= (1<<2)|(1<<3)|(1<<5); // SCK, MOSI and SS as outputs
DDRB &= ~(1<<4); // MISO as input
DDRC |= (1 << PC1); // To select SS
PORTC |= (1 << PC1);
SPCR |= (1<<MSTR); // Set as Master
SPCR |= (1<<SPR0)|(1<<SPR1); // divided clock by 128
SPCR |= (1<<SPE); // Enable SPI
while(1)
{
PORTC &= ~(1 << PC1); // Select slave
SPDR = data; // send the data
while(!(SPSR & (1<<SPIF))); // wait until transmission is complete
//transmitByte(SPDR);
//data = SPDR;
PORTC |= (1 << PC1); // UnSelect slave
_delay_ms(500);
// if you have multiple slaves, this is where you want to switch
} |
You mean this way???
Example: Maybe you want to send some commands with parameter via SPI. Such a command will have a maximum length in bytes.
--> use software byte buffer with appropriate size. During communication just store the received bytes in the buffer. After transmission the master should inactivate the SS signal to show the slave the End_of_frame ... and the slave may start to parse the complete frame.
is there any example code ?
I have one general doubt about SPI communication.
Say I will send a command to Slave from Master. So when clock pulses are complete Slave Data Register will have data transmitter by Master and Master Data Register will have data transmitted from Slave. It could be some junk data. The Slave then process this data to check whether it is a read or write command. If it is a valid command it will send an ACK back to receiver. To read this ACK from Slave, Master will send some junk data to Slave and once transmission completes Master will have ACK data and Slave will have Junk data. I hope I'm clear up to this point.
My doubt is, before Master transmit Junk data to receive ACK data from Slave, Slave should read it's data received before(Here it is Read/Write command) process this info and if it's valid, place ACK in Data register, so that when master sends junk data to read ACK. SO for this duration of time, Should MATER wait in it's loop????