Jump to content
Electronics-Lab.com Community

[SOLVED] Spi "freezes"


Guest thebignoob

Recommended Posts

Guest thebignoob

Hello,
I have an issue with spi that freezes. I'm using atmega88 with nrf24l01 but when i try to test spi communication i get stuck.
I initialize spi based on atmel specifications:

DDRB |= (1<<DDB5) | (1<<DDB3) | (1<<DDB2) |(1<<DDB1);
SPCR |= (1<<SPE)|(1<<MSTR);

And when I try to test out writing to spi (and print out to terminal ) i get stuck looping... ( i have usart initialized and used without any problems)

char spi_rw(unsigned char x)
{
	SPDR = x;	
	//get stuck in this while loop 
	while(!(SPSR & (1<<SPIF)));
	//printing to terminal after get's out of loop but never go out	
	return SPDR;
}

I'm trying to read STATUS register:

uint8_t get_reg(uint8_t reg)
{	

	_delay_us(10);
	CLEARBIT(PORTB, 2);	//CSN low so nrf start listen for command 
	_delay_us(10);
	spi_rw(R_REGISTER + reg);	
	_delay_us(10);
	reg = spi_rw(NOP);
	_delay_us(10);
	SETBIT(PORTB, 2);	//CSN IR_High nrf do nothing now
	return reg;	
}

I try calling get_reg(STATUS) and passing it to usart to print out resultand I get nowhere since code stuck in while loop. P.S. i use this simple functions to set and clear bits

#define BIT(x) (1<<(x))
#define SETBITS(x,y) ((x)|=(y)))
#define CLEARBITS(x,y) ((x) &=(~(y)))
#define SETBIT(x,y) SETBITS((x), (BIT((y))))
#define CLEARBIT(x,y) CLEARBITS((x), (BIT((Y))))



Thanks in advance, any help would be greatly appreciated.  :(

Link to comment
Share on other sites


Guest liquibyte

You had locked your topic, I unlocked it.  Read this and see if it helps.

When the SPI is configured as a Master (MSTR in SPCR is set), the user can determine the direction of the SS pin.

If SS is configured as an output, the pin is a general output pin which does not affect the SPI system. Typically, the pin will be driving the SS pin of the SPI Slave.

If SS is configured as an input, it must be held high to ensure Master SPI operation.
Link to comment
Share on other sites

Guest thebignoob

I locked it, since it was my huge mistake... :( i had badly organized project with huge amount of code and was already pretty tired and frustrated since everything was working and than just stopped... i forgot that i added some more initializing functions that at one place set B2 to low ... i was sure that i had deleted that line so that didn't cross my mind to check...

Probably i shouldn't lock thread before i post that is solved and what was the error.... Sorry for that.


Thank you very much :D

Link to comment
Share on other sites

Guest liquibyte

No worries, I thought that you might have inadvertently locked it.  When I read that quote from the datasheet I thought the problem might be along those lines.  Hope everything works out though.  I'll leave things as they are and if you want to come back and open things up again and show us what you're doing, that would be cool.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
  • Create New...