Xmeg128A1 SPI issue! need help

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello every one

 

I'm working on implementing radio transceiver nrf24l01 on xmega128a1 via SPI,

at first i just want to write data on RF_setup register than read the same data i wrote, but the function return  0 and not the value i wrote.

i think i have a problem with my spi init because my code works well on arduino

here is my code and any help will be appreciated

thank you

#define SPIPort PORTD
#define SPIMO   7
#define SPIMI   6
#define SPISC   5
#define SPISS   4

 

void ceHi()

{
	PORTJ.OUTSET = (1<<3); 
}




void ceLow()

{
	PORTJ.OUTCLR = (1<<3);
	
}


void csnLow()

{
	PORTD.OUTCLR = (1<<4);

}

void csnHi()

{
	PORTD.OUTSET = (1<<4);
}


void spiopen(void)
{
	SPIDir = SPIPort.DIR ;
	PORTJ.DIRSET = (1<<3); // ce pin as output
	SPIPort.DIRSET =  (1<<SPISS);
	ceLow();
	csnHi();
	SPIPort.DIRCLR = (1<<SPIMI);
	SPIPort.DIRSET = (1<<SPIMO) | (1<<SPISS) | (1<<SPISC) ; 
	SPID.CTRL = SPI_ENABLE_bm | SPI_MASTER_bm | SPI_MODE_0_gc | SPI_PRESCALER_DIV64_gc;
	
}

uint8_t spi(uint8_t val)
{
	SPID.DATA = val ;
	while(!(SPID.STATUS & (1<<7)));
	return SPID.DATA;
}

// read register from nrf24l01
void readr(uint8_t reg,uint8_t *value,uint8_t leng)

{       csnLow();
	spi(R_REGISTER | (REGISTER_MASK & reg)); // defined in the datasheet R_register to read and W_register to write
	uint8_t i;
	for(i = 0;i < leng;i++)
	{
		value[i] = spi(value[i]);
	}
	csnHi();
}

uint8_t read_rf_setup() // function to read the rf_setup reg
{
	uint8_t a;
	readr(RF_SETUP,&a,1);
	return a;
	
}

void configReg(uint8_t reg, uint8_t value) // function to write on register

{
	csnLow();
	spi(W_REGISTER | (REGISTER_MASK & reg));
	spi(value);
	csnHi();
}


int main (void)
{
	// Insert system clock initialization code here (sysclk_init()).

	board_init();
	spiopen();
	configReg(RF_SETUP,0x06 // write 0x06 on rf_setup register
	
	
	while(1)
	{
		f=read_rf_setup();
		
		
		
	}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

#define SPIMO   7
#define SPIMI   6
#define SPISC   5
#define SPISS   4

Looks like you have swapped MOSI and CLK there. Obviously that only makes a difference if this is also how you connected the nrf24l01.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your reply

 

MOSI 5 and SCK 7 that's true, but that it doesn't matter, the initialization still correct because the pin connection is correct.

 

My code is fine and The communication works now, the pin header weren't welded well.

Last Edited: Sun. Oct 11, 2015 - 09:18 PM