help in wireless communication between two atmega328p using nRF24L01+ ( mirf library )

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

Hello

I have two nRF24L01+ modules with two atmega328p and I'm trying to make wireless communication between the two atmega328p
and I'm using mirf library.
here is the code in both chips :
 

#define F_CPU 1000000L // 1 MHz

#include <avr/io.h>
#include <util/delay.h>

#include "nRF24L01.h"
#include "spi.h"
#include "mirf.h"

int main(void) {
	spi_init();
	mirf_init();
	_delay_ms(50);
	mirf_config();
	
	uint8_t buffer[5] = {9, 9, 9, 9, 9};
	uint8_t buffersize = 5;
	uint8_t buffer2[5];
	
	while(1) {
		mirf_flush_rx_tx(); // Flush TX/RX
		mirf_send(buffer, buffersize);
		_delay_ms(1000);
		
		// If maximum retries were reached, reset MAX_RT
		if (mirf_max_rt_reached()) {
			mirf_config_register(STATUS, 1<<MAX_RT);
		}
		
		while(!mirf_data_ready());
		mirf_get_data(buffer2);
		if(buffer2[0] == 9)
		{
			PORTB |= (1<<6);
		}
		else
		{
			PORTB &= ~(1<<6);
		}
	}
}

so I send a buffer of 5 bytes each hold value of 9

and I'm receiving after that a buffer and I check if the first byte in it equals 9 ( and if so I write HIGH on pin6 of PORTB - there's a LED there and it should light )
but it didn't work
is there any problems in my code ??

This topic has a solution.
Last Edited: Fri. Oct 16, 2015 - 12:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

updates : 
I made some changes to my code
I made one of the atmega328p transmit by this code :
 

/*
 * nRF24L01_with_ATmega328p_2.c
 *
 * Created: 8/25/2015 3:59:59 PM
 *  Author: Khalid
 */ 


#define F_CPU 1000000L // 1 MHz

#include <avr/io.h>
#include <util/delay.h>

#include "nRF24L01.h"
#include "spi.h"
#include "mirf.h"

int main(void) {
	spi_init();
	mirf_init();
	_delay_ms(50);
	mirf_config();
	
	uint8_t buffer[5] = {9, 9, 9, 9, 9};
	uint8_t buffersize = 5;

	while(1) {
		mirf_flush_rx_tx(); // Flush TX/RX
		
		mirf_send(buffer, buffersize);
		_delay_ms(1000);
		
		// If maximum retries were reached, reset MAX_RT
		if (mirf_max_rt_reached()) {
			mirf_config_register(STATUS, 1<<MAX_RT);
		}
	}
}

and I checked if it transmits will using an arduino uno as a receiver and I managed to receive data and print it on arduinos Serial monitor

yet I couldn't till now receive the data on the other atmega328p

 

the code on the receiver atmega328p :

 

/*
 * nRF24L01_with_ATmega328p_2.c
 *
 * Created: 8/25/2015 3:59:59 PM
 *  Author: Khalid
 */ 


#define F_CPU 1000000L // 1 MHz

#include <avr/io.h>
#include <util/delay.h>

#include "nRF24L01.h"
#include "spi.h"
#include "mirf.h"

int main(void) {
	spi_init();
	mirf_init();
	_delay_ms(50);
	mirf_config();
	
	DDRB |= (1<<6);
	
	uint8_t buffer[5] = {0, 0, 0, 0, 0};
	uint8_t buffersize = 5;
	mirf_CE_hi; // Start listening

	while(1) {
	
		mirf_flush_rx_tx(); // Flush TX/RX
		
		while(!mirf_data_ready())
		{
			_delay_ms(50);
		}

		mirf_get_data(buffer);
		for (int x = 0; x < 5; x++) {
			if(buffer[x] == 9)
			{
				PORTB |= (1<<6);
			}
			else
			{
				PORTB &= ~(1<<6);
			}
			buffer[x] = 0;
	    }
		
	}
}

so for now we have a working transmitter but a bad receiver
any ideas ??

Last Edited: Tue. Aug 25, 2015 - 05:40 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok I've actually made it ! :D
the thing is that I had to activate the receiving mode on the other MCU
that's done by the mirf.c lib :
 

	PMODE = RXMODE; // Set to receiver mode
	RX_POWERUP; // Power up

hope this can help others

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

mostly Atmel products discussed here, rather than Nordic.

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

stevech wrote:

mostly Atmel products discussed here, rather than Nordic.

 

Killjoy!

274,207,281-1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

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

I compile your code but can not debug it .
the warnnig is 'choose the simulator'
can you help me  
 

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

Select the tool you want to use for debugging in Project / Properties / Tool in the "Selected debugger/programmer" box.

If you are debugging hardware, you must have a (supported) debugger connected between the uC and the PC and it should show up in that box.

If you are simulating using the Atmel Studio built-in simulator, choose "Simulator".

David (aka frog_jr)