Skip to main content
Graduate
August 10, 2024
Question

STM32 and NRF24l01

  • August 10, 2024
  • 2 replies
  • 990 views

Hello everyone
I make  SPI communication between Stm32f411 and module NRF24l01.

I have written a function to transfer data from stm32 to Nrf24l01, and read data from Nrf24l01.

Everyone help me to edit code because this is my first time using this protocol

 

void nrf24_WriteReg (uint8_t Reg, uint8_t Data)

{

uint8_t buf[2];

buf[0] = Reg|1<<5;

buf[1] = Data;

 

// Pull the CS Pin LOW to select the device

CS_Select();

 

HAL_SPI_Transmit(NRF24_SPI, buf, 2, 1000);

 

// Pull the CS HIGH to release the device

CS_UnSelect();

}

 

//write multiple bytes starting from a particular register

void nrf24_WriteRegMulti (uint8_t Reg, uint8_t *data, int size)

{

uint8_t buf[2];

buf[0] = Reg|1<<5;

// buf[1] = Data;

 

// Pull the CS Pin LOW to select the device

CS_Select();

 

HAL_SPI_Transmit(NRF24_SPI, buf, 1, 100);

HAL_SPI_Transmit(NRF24_SPI, data, size, 1000);

 

// Pull the CS HIGH to release the device

CS_UnSelect();

}

 

 

uint8_t nrf24_ReadReg (uint8_t Reg)

{

uint8_t buf[1];

buf[0]=Reg|0<<5;

uint8_t data;

 

 

// Pull the CS Pin LOW to select the device

CS_Select();

 

HAL_SPI_Transmit(NRF24_SPI, buf[0], 1, 100);

HAL_SPI_Receive(NRF24_SPI, &data, 1, 100);

 

// Pull the CS HIGH to release the device

CS_UnSelect();

 

return data;

}

 

 

/* Read multiple bytes from the register */

void nrf24_ReadReg_Multi (uint8_t Reg, uint8_t *data, int size)

{

// Pull the CS Pin LOW to select the device

CS_Select();

 

HAL_SPI_Transmit(NRF24_SPI, &Reg, 1, 100);

HAL_SPI_Receive(NRF24_SPI, data, size, 1000);

 

// Pull the CS HIGH to release the device

CS_UnSelect();

}

 

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    September 9, 2024

    Hello @ThienNguyen0301 ,

     

    Could you please give more detail about the issue? Do you facing an issue?

    Are you able to transmit and/or to receive data?

    To start with SPI protocol, I advise you to refer to this wiki: Getting started with SPI - stm32mcu.

     

    Thank you.

    Kaouthar

    Super User
    September 9, 2024

    Everyone help me to edit code because this is my first time using this protocol

    Here you can find help with your stm32 project.