Sending Data as a Host SPI Device
Full Code Example
#include <avr/io.h>
void SPI0_init(void);
void clientSelect(void);
void clientDeselect(void);
uint8_t SPI0_exchangeData(uint8_t data);
void SPI0_init(void)
{
PORTA.DIR |= PIN4_bm;
PORTA.DIR &= ~PIN5_bm;
PORTA.DIR |= PIN6_bm;
PORTA.DIR |= PIN7_bm;
SPI0.CTRLA = SPI_CLK2X_bm
| SPI_DORD_bm
| SPI_ENABLE_bm
| SPI_MASTER_bm
| SPI_PRESC_DIV16_gc;
}
uint8_t SPI0_exchangeData(uint8_t data)
{
SPI0.DATA = data;
while (!(SPI0.INTFLAGS & SPI_IF_bm))
{
;
}
return SPI0.DATA;
}
void clientSelect(void)
{
PORTA.OUT &= ~PIN7_bm;
}
void clientDeselect(void)
{
PORTA.OUT |= PIN7_bm;
}
int main(void)
{
uint8_t data = 0;
SPI0_init();
while (1)
{
clientSelect();
SPI0_exchangeData(data);
clientDeselect();
}
}
Receiving Data as a Client SPI
Device Full Code Example
#include <avr/io.h>
#include <avr/interrupt.h>
void SPI0_init(void);
volatile uint8_t receiveData = 0;
volatile uint8_t writeData = 0;
void SPI0_init(void)
{
PORTA.DIR &= ~PIN4_bm;
PORTA.DIR |= PIN5_bm;
PORTA.DIR &= ~PIN6_bm;
PORTA.DIR &= ~PIN7_bm;
SPI0.CTRLA = SPI_DORD_bm
| SPI_ENABLE_bm
& (~SPI_MASTER_bm);
SPI0.INTCTRL = SPI_IE_bm;
}
ISR(SPI0_INT_vect)
{
receiveData = SPI0.DATA;
SPI0.DATA = writeData;
SPI0.INTFLAGS = SPI_IF_bm;
}
int main(void)
{
SPI0_init();
sei();
while (1)
{
;
}
}
Changing Data Type Full Code
Example
#include <avr/io.h>
void SPI0_init(void);
void clientSelect(void);
void clientDeselect(void);
uint8_t SPI0_exchangeData(uint8_t data);
void SPI0_init(void)
{
PORTA.DIR |= PIN4_bm;
PORTA.DIR &= ~PIN5_bm;
PORTA.DIR |= PIN6_bm;
PORTA.DIR |= PIN7_bm;
SPI0.CTRLA = SPI_CLK2X_bm
| SPI_DORD_bm
| SPI_ENABLE_bm
| SPI_MASTER_bm
| SPI_PRESC_DIV16_gc;
SPI0.CTRLB |= SPI_MODE_3_gc;
}
uint8_t SPI0_exchangeData(uint8_t data)
{
SPI0.DATA = data;
while (!(SPI0.INTFLAGS & SPI_IF_bm))
{
;
}
return SPI0.DATA;
}
void clientSelect(void)
{
PORTA.OUT &= ~PIN7_bm;
}
void clientDeselect(void)
{
PORTA.OUT |= PIN7_bm;
}
int main(void)
{
uint8_t data = 0;
SPI0_init();
while (1)
{
clientSelect();
SPI0_exchangeData(data);
clientDeselect();
}
}