#include <avr/io.h>
void SPI0_init(void);
void slaveSelect(void);
void slaveDeselect(void);
uint8_t SPI0_exchangeData(uint8_t data);
void SPI0_init(void)
{
PORTA.DIR |= PIN4_bm; /* Set MOSI pin direction to output */
PORTA.DIR &= ~PIN5_bm; /* Set MISO pin direction to input */
PORTA.DIR |= PIN6_bm; /* Set SCK pin direction to output */
PORTA.DIR |= PIN7_bm; /* Set SS pin direction to output */
SPI0.CTRLA = SPI_CLK2X_bm /* Enable double-speed */
| SPI_DORD_bm /* LSB is transmitted first */
| SPI_ENABLE_bm /* Enable module */
| SPI_MASTER_bm /* SPI module in Master mode */
| SPI_PRESC_DIV16_gc; /* System Clock divided by 16 */
SPI0.CTRLB |= SPI_MODE_3_gc; /* Data Mode 3 */
}
uint8_t SPI0_exchangeData(uint8_t data)
{
SPI0.DATA = data;
while (!(SPI0.INTFLAGS & SPI_IF_bm)) /* waits until data is exchanged*/
{
;
}
return SPI0.DATA;
}
void slaveSelect(void)
{
PORTA.OUT &= ~PIN7_bm; // Set SS pin value to LOW
}
void slaveDeselect(void)
{
PORTA.OUT |= PIN7_bm; // Set SS pin value to HIGH
}
int main(void)
{
uint8_t data = 0;
SPI0_init();
while (1)
{
slaveSelect();
SPI0_exchangeData(data);
slaveDeselect();
}
}
|