Arduino搭配NRF24L01 2.4G無線傳輸器
發射端:Arduino Genuino uno(MEGA328P)+NRF24L01
接收端:Arduino Nano(MEGA328P)+NRF24L01
程序參考範例:nRF24L01 Module Demo for Arduino
這個程式直接套用就可以實現無線傳輸, 每1秒會發射32 Bytes長的字串,執行成功畫面如下:
(原始程序不會顯示紅色框起來部分)
發射程序:
1.Anduino與NRF24L01硬體連接配置
↑NRF24L01硬體線路(VCC為3.3V, IO Pin雖建議3.3V, 但是規格書說明可至5.25V)
2.設定發射端地址, 接收端需與射端匹配,否則無法成功連接.
3.運行發射模式子程序TX_Mode()
(1).設定發設端地址 (TX_ADDR)
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(命令+暫存器位址,寫入的資料,資料寬度);
SPI_Write_Buf(0x20 + 0x10, [0x34,0x43,0x10,0x10,0x01] , 5);
(2).設定接收端地址 (RX_ADDR_P0)
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(0x20 + 0x0A, [0x34,0x43,0x10,0x10,0x01] , 5);
(3). 自動響應Enable Auto ACK (EN_AA)
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);
SPI_RW_Reg(0x20+0x01, 0x01);
(4). 致能第一組接收 Enable PIPE (EN_RXADDR)
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
SPI_RW_Reg(0x20+ 0x02, 0x01);
(5).設定自動重發次數 (SETUP_RETR)
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a);
SPI_RW_Reg(0x20+ 0x04, 0x1a); // 500us + 86us, 10 retrans...
(6).選擇通訊頻率 (RF_CH)
SPI_RW_Reg(WRITE_REG + RF_CH, 40);
SPI_RW_Reg(0x20 + 0x05, 40); //0b0 0000100
(7).設定發設參數 (RF_SETUP)
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
SPI_RW_Reg(0x20 + 0x06, 0x07); //0b000 0 0 111
(8).設定nRF24L01硬體參數及切換工作模式(CONFIG)
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);
SPI_RW_Reg(0x20 + 0x00, 0x0e); //0b0 0 0 0 1 1 1 0
(寫入的設定小於1個Byte用SPI_RW_Reg, 超過1個byte的用SPI_Write_Buf.)
4. 主程序
API.h 宣告檔
// BYTE type definition
#ifndef API_H
#define API_H
命令
//****************************************************
// SPI(nRF24L01) commands
#define READ_REG 0x00 // Define read command to register
#define WRITE_REG 0x20 // Define write command to register
#define RD_RX_PLOAD 0x61 // Define RX payload register address
#define WR_TX_PLOAD 0xA0 // Define TX payload register address
#define FLUSH_TX 0xE1 // Define flush TX register command
#define FLUSH_RX 0xE2 // Define flush RX register command
#define REUSE_TX_PL 0xE3 // Define reuse TX payload register command
#define NOP 0xFF // Define No Operation, might be used to read status register
//***************************************************
#define RX_DR 0x40
#define TX_DS 0x20
#define MAX_RT 0x10
//***************************************************
暫存器
// SPI(nRF24L01) registers(addresses)
#define CONFIG 0x00 // 'Config' register address
#define EN_AA 0x01 // 'Enable Auto Acknowledgment' register address
#define EN_RXADDR 0x02 // 'Enabled RX addresses' register address
#define SETUP_AW 0x03 // 'Setup address width' register address
#define SETUP_RETR 0x04 // 'Setup Auto. Retrans' register address
#define RF_CH 0x05 // 'RF channel' register address
#define RF_SETUP 0x06 // 'RF setup' register address
#define STATUS 0x07 // 'Status' register address
#define OBSERVE_TX 0x08 // 'Observe TX' register address
#define CD 0x09 // 'Carrier Detect' register address
#define RX_ADDR_P0 0x0A // 'RX address pipe0' register address
#define RX_ADDR_P1 0x0B // 'RX address pipe1' register address
#define RX_ADDR_P2 0x0C // 'RX address pipe2' register address
#define RX_ADDR_P3 0x0D // 'RX address pipe3' register address
#define RX_ADDR_P4 0x0E // 'RX address pipe4' register address
#define RX_ADDR_P5 0x0F // 'RX address pipe5' register address
#define TX_ADDR 0x10 // 'TX address' register address
#define RX_PW_P0 0x11 // 'RX payload width, pipe0' register address
#define RX_PW_P1 0x12 // 'RX payload width, pipe1' register address
#define RX_PW_P2 0x13 // 'RX payload width, pipe2' register address
#define RX_PW_P3 0x14 // 'RX payload width, pipe3' register address
#define RX_PW_P4 0x15 // 'RX payload width, pipe4' register address
#define RX_PW_P5 0x16 // 'RX payload width, pipe5' register address
#define FIFO_STATUS 0x17 // 'FIFO Status Register' register address
留言列表