why does't work my NRF24l01

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
safar1981
Posts: 15
Joined: 13 Nov 2016 07:17

why does't work my NRF24l01

#1 Post by safar1981 » 02 Feb 2017 19:15

Hello I can't get and send the code
Please see my job, If anyone can read it and give me a good solutions.
It is for avr atmega8


/* Memory Map */
#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
#define DYNPD 0x1C
#define FEATURE 0x1D


/* Bit Mnemonics */
#define MASK_RX_DR 0x40 //6
#define MASK_TX_DS 0x20 //5
#define MASK_MAX_RT 0x10 //4
#define EN_CRC 0x08 //3
#define CRCO 0x04 //2
#define PWR_UP 0x02 //1
#define PRIM_RX 0x01 //0
#define ENAA_P5 0x20 //5
#define ENAA_P4 0x10 //4
#define ENAA_P3 0x08 //3
#define ENAA_P2 0x04 //2
#define ENAA_P1 0x02 //1
#define ENAA_P0 0x01 //0
#define ERX_P5 0x20 //5
#define ERX_P4 0x10 //4
#define ERX_P3 0x08 //3
#define ERX_P2 0x04 //2
#define ERX_P1 0x02 //1
#define ERX_P0 0x01 //0
#define AW3 0x01 //0 3 bytes
#define AW4 0x02 //0 4 bytes
#define AW5 0x03 //0 5 bytes
#define ARD 0x10 //4
#define ARC 0x01 //0
#define PLL_LOCK 0x10 //4
#define RF_DR 0x08 //3
#define RF_PWR 0x06 //6
#define RX_DR 0x40 //6
#define TX_DS 0x20 //5
#define MAX_RT 0x10 //4
#define RX_P_NO 0x02 //1
#define TX_FULL 0x01 //0
#define PLOS_CNT 0x10 //4
#define ARC_CNT 0x01 //0
#define TX_REUSE 0x40 //6
#define FIFO_FULL 0x20 //5
#define TX_EMPTY 0x10 //4
#define RX_FULL 0x02 //1
#define RX_EMPTY 0x01 //0
#define DPL_P5 0x20 //5
#define DPL_P4 0x10 //4
#define DPL_P3 0x08 //3
#define DPL_P2 0x04 //2
#define DPL_P1 0x02 //1
#define DPL_P0 0x01 //0
#define EN_DPL 0x04 //2
#define EN_ACK_PAY 0x02 //1
#define EN_DYN_ACK 0x01 //0


/* Instruction Mnemonics */
#define R_REGISTER 0x00
#define W_REGISTER 0x20
#define REGISTER_MASK 0x1F
#define ACTIVATE 0x50
#define R_RX_PL_WID 0x60
#define R_RX_PAYLOAD 0x61
#define W_TX_PAYLOAD 0xA0
#define W_ACK_PAYLOAD 0xA8 // and with ack pipe number
#define FLUSH_TX 0xE1
#define FLUSH_RX 0xE2
#define REUSE_TX_PL 0xE3
#define NOP 0xFF


/* Non-P omissions */
#define LNA_HCURR 0x01 //0


/* P model memory Map */
#define RPD 0x09


/* P model bit Mnemonics */
#define RF_DR_LOW 0x10 //5
#define RF_DR_HIGH 0x08 //3
#define RF_PWR_LOW 0x02 //1
#define RF_PWR_HIGH 0x04 //2


#define TX_mode // define TX_mode or RX_mode
//#include "nRF24L01.h"



const char test[] = "Test message.";


// Pipe Addresses
// adr0 should be unique
// adr1 through adr5 must differ only in the lowest significant byte
// Bytes are arranged lowest to highest
const char adr0[] = {0x78,0x78,0x78,0x78,0x78}; // LSB first
const char adr1[] = {0xf1,0xb4,0xb5,0xb6,0xb3};
const char adr2[] = {0xb3,0xb4,0xb5,0xb6,0xb3};
const char adr3[] = {0x83,0xb4,0xb5,0xb6,0xb3};
const char adr4[] = {0xcf,0xb4,0xb5,0xb6,0xb3};
const char adr5[] = {0x75,0xb4,0xb5,0xb6,0xb3};

char Data_In[21], Data_Out[21] , stat;
short dataLength = 21;

//char Data_In[1], Data_Out[1] = {'A'}, stat;
//short dataLength = 1;

// You can change these pins as long as your microcontroller supports SPI on the required pins
sbit Irq_pin at PORTB0_bit; sfr;
sbit Mosi_pin at PORTB3_bit; sfr;
sbit Ce_pin at PORTB1_bit; sfr;
sbit Sclk_pin at PORTB5_bit; sfr;
sbit Csn_pin at PORTB2_bit; sfr;
sbit Miso_pin at PORTB4_bit; sfr;

sbit Irq_tris at DDB0_bit; sfr;
sbit Mosi_tris at DDB3_bit; sfr;
sbit Ce_tris at DDB1_bit; sfr;
sbit Sclk_tris at DDB5_bit; sfr;
sbit Csn_tris at DDB2_bit; sfr;
sbit Miso_tris at DDB4_bit; sfr;

// LCD module connections
sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;
sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;

// I use a procedure for any delay that is used more than once
void Comm_Delay() {Delay_us(10);} // You can experiment with this delay
void Delay_10() {Delay_ms(10);}
void Delay_100() {Delay_ms(100);}
void Delay_500() {Delay_ms(500);}

void toggleCSN(){
Csn_pin = 1;
Delay_us(20); // You can experiment with this delay
Csn_pin = 0;
Comm_Delay();
}

char *getConst(char dest[], const char source[]){
int i = 0;
while (source) // while (source != '\0')
{dest = source; i++;}
dest = '\0';
return dest;
}

char Get_FIFO_Flags(){
char s;
Ce_pin = 0;
toggleCSN();
SPI1_Write(FIFO_STATUS);
Comm_Delay();
s = SPI1_Read(NOP);
Comm_Delay();
return s;
}

void makeMsg(){ // Put your data in here
memset(Data_Out, ' ', 20);
getConst(Data_Out, test);
}

void fillAckBuffer(){
char i;
Ce_pin = 0;
toggleCSN();
SPI1_Write(W_ACK_PAYLOAD);
for(i=0; i < dataLength; i++) {Comm_Delay(); SPI1_Write(NOP);}
Comm_Delay();
Csn_pin = 1;
}

char readBuffer(){
char i, s;
Ce_pin = 0;
memset(&Data_In, 'a', 20);
fillAckBuffer();
s = Get_FIFO_Flags();
if((s & 2) != 0){
toggleCSN();
SPI1_Write(R_RX_PAYLOAD);
for(i=0; i < dataLength; i++){Comm_Delay(); Data_In = SPI1_Read(0);}
Comm_Delay();
}
Csn_pin = 1;
Ce_pin = 1;
return s;
}

void sendBuffer(){
char i, j;
PORTD.B0 = 0;
PORTD.B1 = 1;
toggleCSN();
SPI1_Write(STATUS | W_REGISTER);
Comm_Delay();
SPI1_Write(0xff); //clear flags
Comm_Delay();
toggleCSN();
SPI1_Write(FLUSH_TX);
Comm_Delay();
//for(j=0; j<3; j++){ // You can load up to 3 payloads
toggleCSN();
SPI1_Write(W_TX_PAYLOAD);
for(i = 0; i < dataLength; i++) {Comm_Delay(); SPI1_Write(Data_Out);}
Comm_Delay();
//}
Csn_pin = 1;
Ce_pin = 1;
Delay_10();
Ce_pin = 0;
}

char init_Radio(){
char i;
Ce_pin = 0; // must be in standby or power down to write
Comm_Delay();
toggleCSN();
SPI1_Write(CONFIG | W_REGISTER);
Comm_Delay();
#ifdef RX_mode
SPI1_Write(PRIM_RX + PWR_UP + CRCO + EN_CRC); // Receiver
#endif
#ifdef TX_mode
SPI1_Write(PWR_UP + CRCO + EN_CRC); // Transmitter
#endif
Comm_Delay();

toggleCSN();
SPI1_Write(EN_AA | W_REGISTER);
Comm_Delay();
SPI1_Write(ENAA_P0);
Comm_Delay();

toggleCSN();
SPI1_Write(EN_RXADDR | W_REGISTER);
Comm_Delay();
SPI1_Write(ERX_P0);
Comm_Delay();

toggleCSN();
SPI1_Write(SETUP_AW | W_REGISTER);
Comm_Delay();
SPI1_Write(AW5);
Comm_Delay();

toggleCSN();
SPI1_Write(SETUP_RETR | W_REGISTER);
Comm_Delay();
SPI1_Write(0xff);
Comm_Delay();

toggleCSN();
SPI1_Write(RF_CH | W_REGISTER);
Comm_Delay();
SPI1_Write(83);
Comm_Delay();

toggleCSN();
SPI1_Write(RF_SETUP | W_REGISTER);
Comm_Delay();
SPI1_Write(RF_PWR + LNA_HCURR);
Comm_Delay();

toggleCSN();
SPI1_Write(RX_ADDR_P0 | W_REGISTER);
for(i=0; i<5; i++){Comm_Delay(); SPI1_Write(0xAA);}
Comm_Delay();

toggleCSN();
SPI1_Write(TX_ADDR | W_REGISTER);
for(i=0; i<5; i++){Comm_Delay(); SPI1_Write(0xAA);}
Comm_Delay();

toggleCSN();
SPI1_Write(RX_PW_P0 | W_REGISTER);
Comm_Delay();
SPI1_Write(dataLength);
Comm_Delay();

toggleCSN();
SPI1_Write(STATUS);
Comm_Delay();
i = SPI1_Read(NOP);
Comm_Delay();
Csn_pin = 1;
return i;
}

// =============================================================================
// =============================================================================

void main(){
//#ifdef RX_mode
char txt[5], dat1;
//#endif
Csn_tris = 0;
Csn_pin = 1;
Ce_tris = 0;
Ce_pin = 0; // 1 = listen or transmit

SPI1_Init();

DDD0_bit=0; //PORTD0_bit=0;
DDD0_bit=1; PORTD0_bit=0;
DDB0_bit=1; PORTB0_bit=0;
DDB1_bit=1; PORTB1_bit=0;
DDB2_bit=1; PORTB2_bit=0;
DDB3_bit=1; PORTB3_bit=0;
DDB4_bit=0; //PORTB4_bit=1;

// Delay_ms(200);
stat = init_Radio();

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, "nRF24L01+");
ByteToHex(stat, txt);
Lcd_Out(2, 1, txt);
Delay_500();

do {
dat1 = 0;
Lcd_Cmd(_LCD_CLEAR); // Clear display

toggleCSN();
SPI1_Write(STATUS);
Comm_Delay();
dat1 = SPI1_Read(NOP);
Comm_Delay();
Csn_pin = 1;
ByteToHex(dat1, txt);
Delay_10();
Lcd_Out(1, 1, txt);
dat1 = Get_FIFO_Flags();
ByteToHex(dat1, txt);
Delay_10();
Lcd_Out(1, 10, txt);

#ifdef TX_mode
Lcd_Out(2, 1, Data_Out);
PORTD0_bit = 1;
PORTD1_bit = 0;
Delay_100();
makeMsg();
sendBuffer();
Delay_100();
#endif

#ifdef RX_mode
Ce_pin = 1;
dat1 = readBuffer();
ByteToHex(dat1, txt);
Delay_10();
Lcd_Out(1, 10, txt);
if((dat1 & 2) != 0) Lcd_Out(2, 1, Data_In);
Delay_500();
#endif
}while(1);
}

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: why does't work my NRF24l01

#2 Post by dusan.poluga » 03 Feb 2017 12:18

Hi,

This is the code for initializing the LCD display.

Code: Select all

// LCD module connections
sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;

sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;
// End LCD module connections

char txt1[] = "mikroElektronika";

char i;                              // Loop variable

void main(){
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,txt1);                 // Write text in first row


  while(1) {                         // Endless loop

  }
}
Be sure to select atmega8 when you create your project.
And please specify what boards you use in your project so i can give you a more detailed explanation.
As for NRF24l01 we have example codes on libstock. https://libstock.mikroe.com/projects/vi ... 01-example
It is written for PIC. You can use the code as reference to try and get it to work.

Regards,
Dusan Poluga.

safar1981
Posts: 15
Joined: 13 Nov 2016 07:17

Re: why does't work my NRF24l01

#3 Post by safar1981 » 03 Feb 2017 20:38

Thank you for your answer dear
dusan.poluga.

And thank you for your codes on libstock.
Can you change it for atmega8?
Please if you can do it for me. I am trying to make it for 1 month, But I can't send and get the data.
Thank you very much.

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: why does't work my NRF24l01

#4 Post by dusan.poluga » 06 Feb 2017 10:38

Hi,

Please test if all connections between NRF and atmega8 are correct.

Regards,
Dusan Poluga.

safar1981
Posts: 15
Joined: 13 Nov 2016 07:17

Re: why does't work my NRF24l01

#5 Post by safar1981 » 06 Feb 2017 21:05

Hello
I have tested. mk mosi to ... . .... nrf24 mosi,
... ........................ miso to ......... miso
........... ... ............ sck(spi) to ............ sck
.... ............ ............portb2 to ............ csn
.......... .... ........... portb1 to.. ........... ce
Everething is ok.

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: why does't work my NRF24l01

#6 Post by dusan.poluga » 07 Feb 2017 11:16

Hi,

Go to the datasheet for the NRF24L01 and see if you are setting the correct registers.

Regards,
Dusan Poluga.

Post Reply

Return to “mikroC PRO for AVR General”