Altri
Sei qui: Home>> Componenti>>Altri>>
  • Categorie
  • diodo
  • ponte raddrizzatore
  • MOSFET
  • Altri
  • nRF24L01 无线 modulo 在 PIC16F877 单片机上的 applicazione 解析
    nRF24L01 无线 modulo 在 PIC16F877 单片机上的 applicazione 解析
  • nRF24L01 无线 modulo 在 PIC16F877 单片机上的 applicazione 解析
  •   Pubblicato: 2019-11-26  Visualizzazioni: 2,396

    先简单的介绍下 nRF24L01 无线 modulo

    (1) 2.4Ghz 全球开放 ISM 频段免许可证使用

     

    (2) 最高工作速率 2Mbps, 高效 GFSK 调制, 抗干扰能力强, 特别适合 industria 控制场合

    (3) 126 频道, 满足多点 telecomunicazioni 和跳频 telecomunicazioni 需要

    (4) 内置硬件 CRC 检错和点对多点 telecomunicazioni 地址控制

    (5) 低功耗 1.9 - 3.6V 工作, 待机模式下状态为 22uA; 掉电模式下为 900nA

    (6) 内置 2.4Ghz 天线, 体积小巧 15mm X29mm

    (7) modulo 可软件设地址, 只有收到本机地址时才会输出数据 (提供中断指示) , 可直接接各种单片机使用, 软件编程非常方便

    nRF24L01 无线 modulo 在 PIC16F877 单片机上的 applicazione 解析

    nRF24L01 无线 modulo 在 PIC16F877 单片机上的 applicazione 解析

    通过 SPI方式完成数据的交换, 包括数据的发送, 数据的接收. 说明一下, 单片机中如果没有 SPI 的硬件circuito, 我们可以使用单片机的普通 IO 口进行 SPI 的时序模拟, 只要符合无线 modulo 的时序逻辑, 一样能控制无线 modulo 的 telecomunicazioni. FPGA是可编程逻辑, 最大的特点就是灵活, 用户可根据需求加入所需要的逻辑 dispositivo, 当然它所包含的逻辑单元也是相当的丰富, 有 SPI 硬件 modulo. 这样用户就省去了 SPI 方式的时序逻辑, 可以更好的专注于功能的开发.

    单片机: 这里我们使用的单片机 modello 为 PIC16F877.

    nRF24L01 无线 modulo 在 PIC16F877 单片机上的 applicazione 解析

    图 1.3 NRF24L01 接入 PIC 的原理图

    说明: 从图 1.3 中可以看出, 主要是图 1.1 中的 6 个信号 (还有 2 个是地与电源) 接入单片机中. 而那些引脚是普通的 IO 口, 需要用户模仿 SPI 时序进行控制.

    无线 modulo 进行数据的交换就是数据的发送与数据的接收, 下面将从这 2 个方面进行介绍. 不管是数据的发送还是数据的接收, 要想控制好 NRF24L01 无线 modulo, 先要通过 SPI 方式对无线 modulo 进行配置, 只需要往它对应的寄存器里写入数值便可.

    先定义一下 PIC 上的宏, 下面我们就可以很方便的对 PIC 的引脚进行操作.

    1 #define MISO RC2

    2 #define MOSI RC3

    3 #define SCK RD0

    4 #define CE RD2

    5 #define CSN RD1

    6 #define IRQ RC1

    7 #define LED RD3

    8 #define KEY0 RB0

    9 #define KEY1 RB1

    10 #define KEY2 RB2

    11 #define KEY3 RB3

    12 #define KEY4 RB4

    13 #define KEY5 RB5

    14 #define KEY6 RB6

    15 #define KEY7 RB7

    NRF24L01 无线 modulo 的寄存器

    1 //*******************NRF24L01 寄存器指令

    2 #define READ_REG 0x00 // 读寄存器指令

    3 #define WRITE_REG 0x20 // 写寄存器指令

    4 #define RD_RX_PLOAD 0x61 // 读取接收数据指令

    5 #define WR_TX_PLOAD 0xA0 // 写待发数据指令

    6 //*******************SPI (nRF24L01) 寄存器地址

    7 #define CONFIG 0x00   // 配置收发状态,

    8 #define EN_AA 0x01   // 自动应答功能设置

    9 #define EN_RXADDR 0x02   // 可用信道设置

    10 #define SETUP_AW 0x03   // 收发地址宽度设置

    11 #define SETUP_RETR 0x04   // 自动重发功能设置

    12 #define RF_CH 0x05   // 工作频率设置

    13 #define RF_SETUP 0x06   // 发射速率, 功耗功能设置

    14 #define STATUS 0x07   // 状态寄存器

    15 #define RX_ADDR_P0 0x0A   // 频道 0 接收数据地址

    16 #define TX_ADDR 0x10   // 发送地址寄存器

    17 #define RX_PW_P0 0x11   // 接收频道 0 接收数据长度

    18 #define FIFO_STATUS 0x17   // FIFO 栈入栈出状态寄存器设置

    有 2 类寄存器是用户可以根据自己的需求所确定的, 那就是地址的长度以及内容, 发送与接收数据的长度, 但无线 modulo 一次最多可以发送 32 个字节, 这两类寄存器一般设置为 3~4 个字节.

    1 #define TX_PLOAD_WIDTH 4

    2 #define RX_PLOAD_WIDTH 4

    3 unsigned char TX_ADDRESS[TX_ADR_WIDTH]= {0x34, 0x43, 0x10}; //本地地址

    4 unsigned char RX_ADDRESS[RX_ADR_WIDTH]= {0x34, 0x43, 0x10}; //接收地址

    A 模拟 SPI 方式

    1 /****************************************************************************************************

    2 /*函数: uint SPI_RW (uint uchar)

    3 /*功能: NRF24L01 的 SPI 时序

    4 /****************************************************************************************************/

    5 unsigned char SPI_RW (unsigned char a)

    6 {

    7 unsigned char i;

    8 for (i=0;i "8;i++)

    9 {

    10 if ( (a&0x80) ==0x80)

    11 MOSI=1;

    12 else MOSI=0; // output ‘uchar’, MSB to MOSI

    13 a= (a " "1) ; // shift next bit into MSB..

    14 SCK=1; // Set SCK high..

    15 if (MISO==1)

    16 a|=0x01;

    17 else a&=0xfe; // capture current MISO bit

    18 SCK=0; // . .then set SCK low again

    19 }

    20 return (a) ; // return read uchar

    21 }

    B 以 SPI 方式对寄存器的操作

    1 /****************************************************************************************************

    2 /*函数: uchar SPI_Read (uchar reg)

    3 /*功能: NRF24L01 的 SPI 读操作

    4 /****************************************************************************************************/

    5 unsigned char SPI_Read (unsigned char reg)

    6 {

    7 unsigned char reg_val;

    8 CSN=0; // CSN low, initialize SPI communicaTIon.. .

    9 SPI_RW (reg) ; // Select register to read from..

    10 reg_val=SPI_RW (0) ; // . .then read registervalue

    11 CSN=1; // CSN high, terminate SPI communicaTIon

    12 return (reg_val) ; // return register value

    13 }

    14 /****************************************************************************************************/

    15 /*功能: NRF24L01 读写寄存器函数

    16 /****************************************************************************************************/

    17 unsigned char SPI_RW_Reg (unsigned char reg, unsigned char value)

    18 {

    19 unsigned char status;

    20 CSN = 0; // CSN low, init SPI transacTIon

    21 status=SPI_RW (reg) ; // select register

    22 SPI_RW (value) ; // . .and write value to it..

    23 CSN = 1; // CSN high again

    24 return (status) ; // return nRF24L01 status uchar

    25 }

    26 /****************************************************************************************************/

    27 /*函数: uint SPI_Read_Buf (uchar reg, uchar *pBuf, uchar uchars)

    28 /*功能: 用于读数据, reg: 为寄存器地址, pBuf: 为待读出数据地址, uchars: 读出数据的个数

    29 /****************************************************************************************************/

    30 unsigned char SPI_Read_Buf (unsigned char reg, unsigned char *pBuf, unsigned char uchars)

    31 {

    32 unsigned char status, uchar_ctr;

    33 CSN = 0; // Set CSN low, init SPI tranacTIon

    34 status=SPI_RW (reg) ; // Select register to write to and read status uchar

    35

    36 for (uchar_ctr=0;uchar_ctr

    37 {

    38 pBuf[uchar_ctr]=SPI_RW (0) ;

    39 }

    40 CSN = 1;

    41

    42 re


  • ·Precedente:
    ·Successivo:
  • Notizie correlate
    Shenzhen Riyuechen Technology S.r.l.
    Indirizzo: 3° piano, edificio 27, zona A, Tantou Second Industrial City, Songgang, Bao'an, Shenzhen
    Tel: 0755-2955 6626
    Fax: 0755-2978 1585
    Cellulare: 131 1300 0010
    E-mail: hu@szryc.com

    Shenzhen Riyuechen Technology S.r.l. Tutti i diritti riservati. Copyright©2010-2027 www.szryc.com Tel: 13113000010 Licenza ICP n. 2021111333 (Guangdong)