Hi,
I worked with following code on ATXMEGA128a4U to create SPI communication:
#define SPI_PORT PORTC
#define SPI_MOSI 5
#define SPI_MISO 6
#define SPI_SCK 7
#define SPI_SS 4
#define SPI_STATUS 0x80 //SPI waiting for operation
#include <avr/io.h>
#include <util/delay.h>
//#include <avr/interrupt.h>
volatile int16_t i=0,j=0;
int SPI_st(void) //SPI init
{
PORTC_DIRSET = (1<<SPI_MOSI);
PORTC_DIRSET = (0<<SPI_MISO);
PORTC_DIRSET = (1<<SPI_SCK);
PORTC_DIRSET = (1<<SPI_SS);
//SS pin high
PORTC_OUTSET = (1<<SPI_SS);
//SPI_master, SPI Mode=00, CLK2x=0, DORD=0, prescaler=11
SPIC.CTRL = 0b0010011;
SPIC.INTCTRL = SPI_INTLVL_OFF_gc ; // no interrupt
//SPI enable
SPIC.CTRL|=64;
return 0;
}
int16_t SPI_re(void) //SPI read
{
int16_t spi_hodnota=0;
//SS pin low
PORTC_OUTCLR = (1<<SPI_SS);
while(SPIC.STATUS & 0x80) spi_hodnota=SPIC.DATA;
//SS high
PORTC_OUTSET = (1<<SPI_SS);
return spi_hodnota;
}
uint8_t SPI_wr(uint8_t vstup) //SPI write
{
//SS pin low
PORTC_OUTCLR=(1<<SPI_SS);
SPIC.DATA=vstup;
while(SPIC.STATUS & 0x80);
//SS high
PORTC_OUTSET = (1<<SPI_SS);
return 0;
}
void SPI_close(void) //SPI CLOSE
{
while(SPIC.STATUS & 0x80);
SPIC_CTRL =0;
}
int main(void)
{
SPI_st();
while(1)
{
for(j=0;j<=255;j++)
{
i=SPI_wr(19);
i=SPI_wr(j);
_delay_ms(120);
}
}
SPI_close();
}
I'm trying to communicate with SPI potentiometer MCP42010. I first connected it with Arduino Due (3.3V logic), everything was working well. After that I connected it to Atmel ATXMEGA128A4U, but I it wasn't working,
nothing on osciloscope. So I try Xplained Pro with ATXMEGA128A1, the same story. When I did debugging in SPI_uvod, I found all pins are properly setup CTRL register has value 01010011, which is expected. Hopefully someone can figure out, what I'm doing wrong.
Thx for looking into issue.