NRF24l01+ problem communication failure cannot even read the registers data from ATMEGA16a

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

hello to all

i am interfacing ATMEGA16A with NRF24l01+ and have written the following interfacing code in atmel studio.

First problem is i am unable to read the registers of NRF24l01+.

Second problem is i am unable to Tx or Rx data between two NRF24l01+

I am attaching my code at the Tx side.

/*
 * steppermotor.c
 *
 * Created: 02-02-2015 19:49:15
 *  Author: Naveen Kumar
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "nrf24l01.h"
void lcd_cmd(uint8_t mycmd);
void lcd_data(char *mydata);
void lcd_cdata(char mydata);
void lcd_init();
void spi_init();
char spi_sendreceive(char mydata);
uint8_t* write_to_nrf(uint8_t readwrite,uint8_t reg,uint8_t *cmd,uint8_t noofcmd);
void nrf_init();
void transmit_payload(uint8_t* mydata);
uint8_t getRegStatus(uint8_t reg);
int main()
{
	DDRC=0xFF;
	DDRD=0x1F;
	lcd_init();
	spi_init();
	nrf_init();
	uint8_t mydata[]={65,66,67,68,69};
	transmit_payload(mydata);
	if((getRegStatus(STATUS)&(1<<4))!=0)
	{
		lcd_data("TX Failed");
	}
	else
	{
		lcd_data("Success");
	}
	return 0;
}
void lcd_cmd(uint8_t mycmd)
{
	PORTC=mycmd;
	PORTD|=(1<<PIND2);
	_delay_ms(10);
	PORTD &= ~(1<<PIND2);
}
void lcd_data(char *mydata)
{
	int i;
	for(i=0;mydata[i]!=0;i++)
	{
		PORTC=mydata[i];
		PORTD|=(1<<PIND2)|(1<<PIND0);
		_delay_ms(10);
		PORTD&= ~((1<<PIND2)|(1<<PIND0));
	}
}
void lcd_cdata(char mydata)
{
	PORTC=mydata;
	PORTD|=(1<<PIND2)|(1<<PIND0);
	_delay_ms(10);
	PORTD&= ~((1<<PIND2)|(1<<PIND0));	
}
void lcd_init()
{
	lcd_cmd(0x38);
	lcd_cmd(0x0C);
	lcd_cmd(0x01);
}
void spi_init()
{
	DDRB|=(1<<PINB5)|(1<<PINB7)|(1<<PINB4);
	PORTB|= (1<<PINB4);
	SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
char spi_sendreceive(char mydata)
{
	SPDR=mydata;
	while(!(SPSR & (1<<SPIF)));
	return SPDR;
}
uint8_t* write_to_nrf(uint8_t readwrite,uint8_t reg,uint8_t *cmd,uint8_t noofcmd){
	if(readwrite==W)
	{
		reg += W_REGISTER;
	}
	static uint8_t retval[32];
	int i;
	_delay_us(10);
	CSN_low;
	_delay_us(10);
	spi_sendreceive(reg);
	_delay_us(10);
	for(i=0;i<noofcmd;i++)
	{
		if(readwrite==R && reg != W_TX_PAYLOAD)
		{
			retval[i]=spi_sendreceive(NOP);
			_delay_us(10);
		}
		else
		{
			spi_sendreceive(cmd[i]);
			_delay_us(10);
		}
	}
	CSN_high;
	return retval;
}
void nrf_init()
{
	_delay_ms(100);
	uint8_t val[5];
	val[0]=0x01;
	write_to_nrf(W,EN_AA,val,1);
	val[0]=0x01;
	write_to_nrf(W,EN_RXADDR,val,1);
	val[0]=0x03;
	write_to_nrf(W,SETUP_AW,val,1);
	val[0]=0x01;
	write_to_nrf(W,RF_CH,val,1);
	int i;
	for(i=0;i<5;i++)
	{
		val[i]=0x12;
	}
	write_to_nrf(W,RX_ADDR_P0,val,5);
	for(i=0;i<5;i++)
	{
		val[i]=0x12;
	}
	write_to_nrf(W,TX_ADDR,val,5);
	val[0]=0x05;
	write_to_nrf(W,RX_PW_P0,val,1);
	val[0]=0x1E;
	write_to_nrf(W,CONFIG,val,1);
	_delay_ms(100);
}
void transmit_payload(uint8_t* mydata)
{
	write_to_nrf(R,FLUSH_TX,mydata,0);
	write_to_nrf(R,W_TX_PAYLOAD,mydata,5);
	_delay_ms(10);
	CE_high;
	_delay_us(20);
	CE_low;
	_delay_ms(10);
}
uint8_t getRegStatus(uint8_t reg)
{
	_delay_us(10);
	CSN_low;
	_delay_us(10);
	spi_sendreceive(R_REGISTER+reg);
	_delay_us(10);
	reg=spi_sendreceive(NOP);
	_delay_us(10);
	CSN_high;
	return reg;
}
/*
 * nrf24l01.h
 *
 * Created: 02-02-2015 14:11:31
 *  Author: Naveen Kumar
 */ 


#ifndef NRF24L01_H_
#define NRF24L01_H_

#define R_REGISTER 0x00
#define W_REGISTER 0x20
#define R_RX_PAYLOAD 0x61
#define W_TX_PAYLOAD 0xA0
#define FLUSH_TX 0xE1
#define FLUSH_RX 0xE2
#define REUSE_TX_PL 0xE3
#define NOP 0xFF

#define R 0
#define W 1

#define CONFIG 0x00
#define EN_AA 0x01
#define EN_RXADDR 0x02
#define SETUP_AW 0x03
#define SETUP_RETR 0x04
#define RF_CH 0x05
#define RF_SETUP 0x06
#define STATUS 0x07
#define OBSERVE_TX 0x08
#define RPD 0x09
#define RX_ADDR_P0 0x0A
#define RX_ADDR_P1 0x0B
#define RX_ADDR_P2 0x0C
#define RX_ADDR_P3 0x0D
#define RX_ADDR_P4 0x0E
#define RX_ADDR_P5 0x0F
#define TX_ADDR 0x10
#define RX_PW_P0 0x11
#define RX_PW_P1 0x12
#define RX_PW_P2 0x13
#define RX_PW_P3 0x14
#define RX_PW_P4 0x15
#define RX_PW_P5 0x16
#define FIFO_STATUS 0x17
#define DYNPD 0x1C
#define FEATURE 0x1D

#define CSN_high PORTD|= 1<<PIND5
#define CSN_low PORTD &= ~(1<<PIND5)

#define CE_high PORTD|= 1<<PIND4
#define CE_low PORTD &= ~(1<<PIND4)


#endif /* NRF24L01_H_ */

Please help me as i am new to this transceiver

Last Edited: Fri. Oct 16, 2015 - 12:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

naveen_gvvsnr wrote:

hello to all

i am interfacing ATMEGA16A with NRF24l01+ and have written the following interfacing code in atmel studio.

First problem is i am unable to read the registers of NRF24l01+.

 

You have to be able to communicate with the device, so first fix the register read issue.  I have used code by Davide Gironi (https://code.google.com/p/davide...) with success before. Perhaps start with this code to check that there are no hardware/connection issues, then continue with your code once you know the hardware is working.

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

thanks for your reply. i have double check my code and connections. this time the reading of registers is working. I have forgotten to put CSN to high before shifting it to low. always i am putting it at low only, so nrf was not accepting any commands. this i have corrected. but what i am facing the problem now is

if i am reading the data by just powering up the nrf all the registers are reading.

 but if i am writing any data to the nrf and then trying to read registers all are returning the values 0 only.

 

and thanks for mentioning the library. i just want to write every line of code mostly myself as it will give me the full insight of the code. i am not saying that i will not use libraries, but writing and executing code myself is somewhat happy to me

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

This is my latest code please tell the error in it.

/*
 * myslavespi.c
 *
 * Created: 06-02-2015 10:58:37
 *  Author: Naveen Kumar
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "nrf24l01.h"

void lcd_cmd(char mycmd);
void lcd_data(char mydata);
void lcd_sdata(char* mydata);
void lcd_init();
void spi_init();
char spi_sendreceive(char mydata);
uint8_t* get_register_data(uint8_t reg,uint8_t noval);
uint8_t* write_to_nrf(uint8_t readwrite,uint8_t reg,uint8_t* val,uint8_t noval);
void nrf_init();
int main(void)
{
	DDRC=0xFF;
	DDRD=0x1F;
	lcd_init();
	spi_init();
	
	CSN_high;
	_delay_ms(1000);
	nrf_init();
	
	_delay_ms(1000);
	uint8_t *recvdata=get_register_data(TX_ADDR,1); // here it is supposed to show 0xD4 as the address of the transmitter. but it is showing as 0
	char pdata[16];
	sprintf(pdata,"%d",recvdata[0]);
	lcd_sdata(pdata); 
	
}
void lcd_cmd(char mycmd)
{
	PORTC=mycmd;
	PORTD|=(1<<PINB2);
	_delay_ms(10);
	PORTD &= ~(1<<PINB2);
}
void lcd_data(char mydata)
{
	PORTC=mydata;
	PORTD|=(1<<PINB2)|(1<<PINB0);
	_delay_ms(10);
	PORTD&= ~(1<<PINB2|1<<PINB0);
}
void lcd_sdata(char* mydata)
{
	int i;
	for(i=0;mydata[i]!=0;i++)
	{
		PORTC=mydata[i];
		PORTD|=(1<<PINB2)|(1<<PINB0);
		_delay_ms(10);
		PORTD&= ~(1<<PINB2|1<<PINB0);
	}
}
void lcd_init()
{
	lcd_cmd(0x38);
	lcd_cmd(0x0C);
	lcd_cmd(0x01);
}
void spi_init()
{
	DDRB=(1<<PINB5)|(1<<PINB7)|(1<<PINB4);
	PORTB|=(1<<PINB4);
	SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
char spi_sendreceive(char mydata)
{
	SPDR=mydata;
	while(!(SPSR &(1<<SPIF)));
	return SPDR;
}
uint8_t* get_register_data(uint8_t reg,uint8_t noval)
{
	_delay_ms(10);
	CSN_low;
	_delay_us(10);
	spi_sendreceive(reg);
	_delay_us(10);
	static uint8_t recvdata[32];
	int i;
	for(i=0;i<noval;i++)
	{
		recvdata[i]=spi_sendreceive(NOP);
		_delay_us(10);
	}
	CSN_high;
	_delay_ms(10);
	return recvdata;
}

uint8_t* write_to_nrf(uint8_t readwrite,uint8_t reg,uint8_t* val,uint8_t noval)
{
	_delay_ms(10);
	if(readwrite==W)
	{
		reg += W_REGISTER;
	}
	int i;
	static uint8_t retdata[32];
	CSN_low;
	_delay_us(10);
	spi_sendreceive(reg);
	_delay_us(10);
	for(i=0;i<noval;i++)
	{
		if(readwrite==R && reg != W_TX_PAYLOAD)
		{
			retdata[i]=spi_sendreceive(NOP);
			_delay_us(10);
		}
		else
		{
			spi_sendreceive(val[i]);
			_delay_us(10);
		}
	}
	CSN_high;
	_delay_ms(10);
	return retdata;
}
void nrf_init()
{
	_delay_ms(10);
	uint8_t val[5];
	val[0]=1;
	write_to_nrf(W,EN_AA,val,1);
	val[0]=1;
	write_to_nrf(W,EN_RXADDR,val,1);
	val[0]=2;
	write_to_nrf(W,SETUP_AW,val,1);
	val[0]=1;
	write_to_nrf(W,RF_CH,val,1);
	int i;
	for(i=0;i<5;i++)
	{
		val[i]=0xD4;
	}
	write_to_nrf(W,RX_ADDR_P0,val,5);
	for(i=0;i<5;i++)
	{
		val[i]=0xD4; //Address of the transmitter
	}
	write_to_nrf(W,TX_ADDR,val,5);
	val[0]=5;
	write_to_nrf(W,RX_PW_P0,val,1);
	
	val[0]=0x0E;
	write_to_nrf(W,CONFIG,val,1);
}

 

Last Edited: Sun. Feb 8, 2015 - 05:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks guys. I have a small problem in my code.

solved it.

Thanks for all of you.

 

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

Naveen,

 

You could help someone else in the future if you show the source code that works correctly.

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

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

HELLO TO ALL FRIENDS OUT THERE. SORRY IT WAS SOME LATE TO POST MY CODE AS I AM OUT OF THE TOWN FOR A FEW DAYS ANYWAY HERE IS THE CODE WHICH WORKED. it  is having autoacknowledgement feature so that it will check for packet delivery. i am having the payload width as 5. change it if you need.

/*
 * nrf.h
 *
 * Created: 15-02-2015 22:10:57
 *  Author: Naveen Kumar
 */ 


#ifndef NRF_H_
#define NRF_H_

#define CONFIG      0x00
#define EN_AA       0x01
#define EN_RXADDR   0x02
#define SETUP_AW    0x03
#define SETUP_RETR  0x04
#define RF_CH       0x05
#define RF_SETUP    0x06
#define STATUS      0x07
#define OBSERVE_TX  0x08
#define CD          0x09
#define RX_ADDR_P0  0x0A
#define RX_ADDR_P1  0x0B
#define RX_ADDR_P2  0x0C
#define RX_ADDR_P3  0x0D
#define RX_ADDR_P4  0x0E
#define RX_ADDR_P5  0x0F
#define TX_ADDR     0x10
#define RX_PW_P0    0x11
#define RX_PW_P1    0x12
#define RX_PW_P2    0x13
#define RX_PW_P3    0x14
#define RX_PW_P4    0x15
#define RX_PW_P5    0x16
#define FIFO_STATUS 0x17

/* Bit Mnemonics */
#define MASK_RX_DR  6
#define MASK_TX_DS  5
#define MASK_MAX_RT 4
#define EN_CRC      3
#define CRCO        2
#define PWR_UP      1
#define PRIM_RX     0
#define ENAA_P5     5
#define ENAA_P4     4
#define ENAA_P3     3
#define ENAA_P2     2
#define ENAA_P1     1
#define ENAA_P0     0
#define ERX_P5      5
#define ERX_P4      4
#define ERX_P3      3
#define ERX_P2      2
#define ERX_P1      1
#define ERX_P0      0
#define AW          0
#define ARD         4
#define ARC         0
#define PLL_LOCK    4
#define RF_DR       3
#define RF_PWR      1
#define LNA_HCURR   0
#define RX_DR       6
#define TX_DS       5
#define MAX_RT      4
#define RX_P_NO     1
#define TX_FULL     0
#define PLOS_CNT    4
#define ARC_CNT     0
#define TX_REUSE    6
#define FIFO_FULL   5
#define TX_EMPTY    4
#define RX_FULL     1
#define RX_EMPTY    0

/* Instruction Mnemonics */
#define R_REGISTER    0x00
#define W_REGISTER    0x20
#define REGISTER_MASK 0x1F
#define R_RX_PAYLOAD  0x61
#define W_TX_PAYLOAD  0xA0
#define FLUSH_TX      0xE1
#define FLUSH_RX      0xE2
#define REUSE_TX_PL   0xE3
#define NOP           0xFF

#define CSN_low PORTB &= ~(1<<PINB1)
#define CSN_high PORTB |= (1<<PINB1)

#define CE_low PORTB &= ~(1<<PINB0)
#define CE_high PORTB |= (1<<PINB0)


#define INIT_RX 0
#define INIT_TX 1

#define DATA_RECEIVED 1
#define DATA_NOTRECEIVED 0
#endif /* NRF_H_ */

This is the receiver program

/*
 * uartapp.c
 *
 * Created: 15-02-2015 19:06:13
 *  Author: Naveen Kumar
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "nrf.h"
#define BAUD 9600
#define BAUDRATE (F_CPU/(BAUD*16UL)-1)

void uart_init();
void uart_transmit(uint8_t mydata);
void uart_transmitstring(char* mydata);
uint8_t uart_receive();
void spi_init();
uint8_t spi_sendreceive(uint8_t mydata);
uint8_t* nrf_readregister(uint8_t reg,int noval);
void nrf_writeregister(uint8_t reg,uint8_t *regdata,int noval);
void nrf_init(uint8_t inittype);
void nrf_tx(uint8_t *mydata);
void nrf_reset();
void nrf_rx();
uint8_t nrf_rxdataready();
uint8_t* nrf_readdata();
int main()
{
	DDRB |= ((1<<PINB0)|(1<<PINB1));
	uart_init();
	spi_init();
	uint8_t* recvdata;
	//char pdata[16];
	int i;
	CSN_high;
	CE_low;
	_delay_ms(10);
	nrf_init(INIT_RX);
	while(1)
	{
		nrf_rx();
		if(nrf_rxdataready()==DATA_RECEIVED)
		{
			recvdata=nrf_readdata();
			for(i=0;i<5;i++)
			{
				uart_transmit(recvdata[i]);
			}
		}
		
	}
	
	return 0;
}

void uart_init()
{
	UBRRH=BAUDRATE>>8;
	UBRRL=BAUDRATE;
	UCSRB=(1<<RXEN)|(1<<TXEN);
	UCSRC=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
}
void uart_transmit(uint8_t mydata)
{
	while(!(UCSRA & (1<<UDRE)));
	UDR=mydata;
}
void uart_transmitstring(char* mydata)
{
	int i;
	for(i=0;mydata[i]!=0;i++)
	{
		uart_transmit(mydata[i]);
	}
}
uint8_t uart_receive()
{
	while(!(UCSRA & (1<<RXC)));
	return UDR;
}
void spi_init()
{
	DDRB |= ((1<<PINB5)|(1<<PINB7)|(1<<PINB4));
	PORTB |= (1<<PINB4);
	SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
uint8_t spi_sendreceive(uint8_t mydata)
{
	SPDR=mydata;
	while(!(SPSR & (1<<SPIF)));
	return SPDR;
}
uint8_t* nrf_readregister(uint8_t reg,int noval)
{
	int i;
	static uint8_t retdata[32];
	CSN_low;
	spi_sendreceive(reg);
	for(i=0;i<noval;i++)
	{
		retdata[i]=spi_sendreceive(NOP);
	}
	CSN_high;
	return retdata;
}
void nrf_writeregister(uint8_t reg,uint8_t *regdata,int noval)
{
	int i;
	CSN_low;
	spi_sendreceive(W_REGISTER|reg);
	for(i=0;i<noval;i++)
	{
		spi_sendreceive(regdata[i]);
	}
	CSN_high;
}
void nrf_init(uint8_t inittype)
{
	uint8_t val[5];
	uint8_t i;
	val[0]=0x01;
	nrf_writeregister(EN_AA,val,1);
	val[0]=0x01;
	nrf_writeregister(EN_RXADDR,val,1);
	for(i=0;i<5;i++)
	{
		val[i]=0x3C;
	}
	nrf_writeregister(RX_ADDR_P0,val,5);
	for(i=0;i<5;i++)
	{
		val[i]=0x3C;
	}
	nrf_writeregister(TX_ADDR,val,5);
	val[0]=0x05;
	nrf_writeregister(RX_PW_P0,val,1);
	if(inittype==INIT_TX)
	{
		val[0]=0x0E;			
	}
	if(inittype==INIT_RX)
	{
		val[0]=0x0F;
	}
	nrf_writeregister(CONFIG,val,1);
	
}

void nrf_tx(uint8_t *mydata)
{
	uint8_t i;
	CSN_low;
	spi_sendreceive(FLUSH_TX);
	CSN_high;
	_delay_ms(1);
	CSN_low;
	spi_sendreceive(W_TX_PAYLOAD);
	for(i=0;i<5;i++)
	{
		spi_sendreceive(mydata[i]);
	}
	CSN_high;
	CE_high;
	_delay_us(30);
	CE_low;
}
void nrf_reset()
{
	uint8_t *recvdata;
	recvdata=nrf_readregister(STATUS,1);
	recvdata[0]|=(1<<MAX_RT);
	nrf_writeregister(STATUS,recvdata,1);
}
void nrf_rx()
{
	CSN_low;
	spi_sendreceive(FLUSH_RX);
	CSN_high;
	CE_high;
	_delay_ms(1000);
	CE_low;
}
uint8_t nrf_rxdataready()
{
	uint8_t* recvdata;
	recvdata=nrf_readregister(STATUS,1);
	if(recvdata[0]&(1<<RX_DR))
	{
		return DATA_RECEIVED;
	}
	else
	{
		return DATA_NOTRECEIVED;
	}
}
uint8_t* nrf_readdata()
{
	int i;
	static uint8_t recvdata[5];
	CSN_low;
	spi_sendreceive(R_RX_PAYLOAD);
	for(i=0;i<5;i++)
	{
		recvdata[i]=spi_sendreceive(NOP);
	}
	CSN_high;
	return recvdata;
}

 

Transmitter code

/*
 * uartapp.c
 *
 * Created: 15-02-2015 19:06:13
 *  Author: Naveen Kumar
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "nrf.h"
#define BAUD 9600
#define BAUDRATE (F_CPU/(BAUD*16UL)-1)

void uart_init();
void uart_transmit(uint8_t mydata);
void uart_transmitstring(char* mydata);
uint8_t uart_receive();
void spi_init();
uint8_t spi_sendreceive(uint8_t mydata);
uint8_t* nrf_readregister(uint8_t reg,int noval);
void nrf_writeregister(uint8_t reg,uint8_t *regdata,int noval);
void nrf_init(uint8_t inittype);
void nrf_tx(uint8_t *mydata);
void nrf_reset();
int main()
{
	DDRB |= ((1<<PINB0)|(1<<PINB1));
	uart_init();
	spi_init();
	uint8_t* recvdata;
	//char pdata[16];
	CSN_high;
	CE_low;
	_delay_ms(10);
	nrf_init(INIT_TX);
	uint8_t mydata[]={65,65,65,65,65};
	while(1)
	{
		nrf_tx(mydata);
		recvdata=nrf_readregister(STATUS,1);
		if(recvdata[0]& (1<<MAX_RT))
		{
			nrf_reset();
			uart_transmitstring("Resetting");
		}
		else if(recvdata[0]& (1<<TX_DS))
		{
			uart_transmitstring("Transmission Success");
		}
		_delay_ms(1000);
	}
	
	return 0;
}

void uart_init()
{
	UBRRH=BAUDRATE>>8;
	UBRRL=BAUDRATE;
	UCSRB=(1<<RXEN)|(1<<TXEN);
	UCSRC=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
}
void uart_transmit(uint8_t mydata)
{
	while(!(UCSRA & (1<<UDRE)));
	UDR=mydata;
}
void uart_transmitstring(char* mydata)
{
	int i;
	for(i=0;mydata[i]!=0;i++)
	{
		uart_transmit(mydata[i]);
	}
}
uint8_t uart_receive()
{
	while(!(UCSRA & (1<<RXC)));
	return UDR;
}
void spi_init()
{
	DDRB |= ((1<<PINB5)|(1<<PINB7)|(1<<PINB4));
	PORTB |= (1<<PINB4);
	SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
uint8_t spi_sendreceive(uint8_t mydata)
{
	SPDR=mydata;
	while(!(SPSR & (1<<SPIF)));
	return SPDR;
}
uint8_t* nrf_readregister(uint8_t reg,int noval)
{
	int i;
	static uint8_t retdata[32];
	CSN_low;
	spi_sendreceive(reg);
	for(i=0;i<noval;i++)
	{
		retdata[i]=spi_sendreceive(NOP);
	}
	CSN_high;
	return retdata;
}
void nrf_writeregister(uint8_t reg,uint8_t *regdata,int noval)
{
	int i;
	CSN_low;
	spi_sendreceive(W_REGISTER|reg);
	for(i=0;i<noval;i++)
	{
		spi_sendreceive(regdata[i]);
	}
	CSN_high;
}
void nrf_init(uint8_t inittype)
{
	uint8_t val[5];
	uint8_t i;
	val[0]=0x01;
	nrf_writeregister(EN_AA,val,1);
	val[0]=0x01;
	nrf_writeregister(EN_RXADDR,val,1);
	for(i=0;i<5;i++)
	{
		val[i]=0x3C;
	}
	nrf_writeregister(RX_ADDR_P0,val,5);
	for(i=0;i<5;i++)
	{
		val[i]=0x3C;
	}
	nrf_writeregister(TX_ADDR,val,5);
	val[0]=0x05;
	nrf_writeregister(RX_PW_P0,val,1);
	if(inittype==INIT_TX)
	{
		val[0]=0x0E;			
	}
	if(inittype==INIT_RX)
	{
		val[0]=0x0F;
	}
	nrf_writeregister(CONFIG,val,1);
	
}

void nrf_tx(uint8_t *mydata)
{
	uint8_t i;
	CSN_low;
	spi_sendreceive(FLUSH_TX);
	CSN_high;
	_delay_ms(1);
	CSN_low;
	spi_sendreceive(W_TX_PAYLOAD);
	for(i=0;i<5;i++)
	{
		spi_sendreceive(mydata[i]);
	}
	CSN_high;
	CE_high;
	_delay_us(30);
	CE_low;
}
void nrf_reset()
{
	uint8_t *recvdata;
	recvdata=nrf_readregister(STATUS,1);
	recvdata[0]|=(1<<MAX_RT);
	nrf_writeregister(STATUS,recvdata,1);
}

 

Instructions:

i have used ATMEGA16a

 

NRFPINS              ATMEGA16a PINS

PIN3(CE)             PINB0

PIN4(CSN)           PINB1

PIN5(SCK)           PINB7

PIN6(MOSI)         PINB5

PIN7(MISO)         PINB6

PIN1 (GND)

PIN2 (3.3v)

 

USART PINS

PIND0 - RX

PIND1 - TX

 

use the above pins for ATMEGA16a and ATMEGA32

it will work as it is without any errors.

 

for other microcontrollers you need to change the pins a little bit