Right…
I’ve finally hooked up the rf24g to my atmega128 on the stk501 board. I’ve configured it for no crc, 8 bit address and then run an infinite while loop calling a receive method.
Hopeful outcome: To receive lots of rubbish and have the DR1 line toggling nicely. Unfortunately this does’nt happen. In fact very little happens.
I put out the data on PORTC (led’s) and every now and then the lights change to 00000011. Either it does nothing, or changes to 00000011 and then does nothing. I am extremely confused.
Here is my code, i hope I’m doing something obviously stupid and some genius picks it up immediately.
int main(void)
{
u08 data;
//initiate SPI
cbi(DDRD,0); //set PD0/DR1 as input
cbi(PORTD,0); //initialise PORTD to 0
cbi(PIND,0); //initialise PIND to 0
sbi(DDRB, 1); // set SCK as output
sbi(DDRB, 5); // set CS as output
sbi(DDRB, 4); // set CE as output
cbi(DDRB, 3); // set MISO as input
sbi(DDRB, 2); // set MOSI as output
sbi(DDRB, 0); // SS must be output for Master mode sbi(SPCR, MSTR);
sbi(SPCR, SPR0);//SET UP CLOCK this is MCU clock/128
sbi(SPCR, SPR1);
cbi(SPCR, CPOL);// CPOL = 0, CPHA = 1
sbi(SPCR,CPHA);//setup rising, sample falling
cbi(SPCR,DORD);// Data order MSB first
sbi(SPCR, SPE);// enable SPI
SPSR&= 0x3E;// clear status
sbi(SPCR, SPIE);// enable SPI interrupt
cbi(SPSR,SPI2X);
sei();//enable interrupts
DDRC= 0xFF; //PORT C output
PORTC = 0xAA;//default pattern
cbi(PORTB,4);//pull CE, CS low
cbi(PORTB,5);//putting tRF2.4GHz into standby mode
ms_spin(3); //wait 3ms to settle
while(1)
{
//*****RECEIVES A BYTE AND WRITES TO PORT C
cbi(PORTB,4);//pull CE low
sbi(PORTB,5);//pull CS high
//wait 5 us to settle
data = 0x03;
spiSendByte(data);//set up 2.4GHz and receive sbi(PORTB,4);//pull CE high
//wait 195 us to settle
us_spin(195);
while((PIND&0x01))//wait till DR1 is pulled high
{
}
cbi(PORTB,4);//pull CE low
PORTC = spiReceiveByte();
}
return 0;
}
void spiSendByte(u08 data)
{
// send a byte over SPI and ignore reply
#ifdef SPI_USEINT
while(!spiTransferComplete);
#else
while(!(inp(SPSR) & (1<<SPIF)));
#endif
spiTransferComplete = FALSE;
SPDR = data;
#ifdef SPI_USEINT
while(!spiTransferComplete);
#endif
}
u08 spiReceiveByte(void)
{
#ifdef SPI_USEINT
while(!spiTransferComplete);
#else
while(!(inp(SPSR) & (1<<SPIF)));
spiTransferComplete = TRUE;
#endif
// return the received data
return SPDR;
}
Sorry for the long code, but i’ve been stuck for almost a week now and don’t want to leave anything out. btw I’m using WinAVR as my c compiler.
Your infinite wisdom on this subject would be much appreciated.
Richard
ADMIN EDIT : Use the code button please