📄 nrf24l01.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: nRF24L01.c
** Last modified Date:
** Last Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Created by:
** Created date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
/**************************************************/
void init_io(void);
void init_chip(void);
unsigned char SPI_RW(unsigned char byte);
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value);
unsigned char SPI_Read(unsigned char reg);
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes);
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes);
void RX_Mode(unsigned char *address);
void TX_Mode(unsigned char *address);
//void __irq IRQ_Eint(void);
void IRQ_Eint(void);
void delay_ms(unsigned int x);
/**************************************************/
#define ADR_WIDTH 5 // 5 bytes TX(RX) address width (地址长度)
#define PLOAD_WIDTH 20 // 20 bytes TX payload (最大是 32 bytes)
unsigned char TX_ADDRESS[ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address (即目标地址)
unsigned char Local_ADDRESS[ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x02}; // Define a static RX address (即本地地址)
unsigned char rx_buf[PLOAD_WIDTH]; //接收缓存容器
unsigned char tx_buf[PLOAD_WIDTH]; //发送缓存容器
unsigned char flag; //标志
/**************************************************/
#define CE 0x00000001<<3 //P0.3 ----OUT
#define CSN 0x00000001<<4 //P0.4 ----OUT
#define SCK 0x00000001<<27 //P0.27 ----OUT
#define MOSI 0x00000001<<17 //P1.17 ----OUT
#define MISO 0x00000001<<25 //P0.25 ----IN
#define IRQ_ARM 0x00000001<<25 //P1.25 ----EXINT0
/**************************************************/
#define LED1 0x00000001<<24 //P1.24
#define LED2 0x00000001<<7 //P0.7
#define LED3 0x00000001<<6 //P0.6
#define LED4 0x00000001<<5 //P0.5
#define TEST 0x00000001<<14 //P0.14
/**************************************************/
unsigned char sta;
#define RX_DR ((sta & 0x40) >> 6)
#define TX_DS ((sta & 0x20) >> 5)
#define MAX_RT ((sta & 0x10) >> 4)
/**************************************************/
//*******************************************************************************************************************//
// 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
// 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
//*******************************************************************************************************************//
/**************************************************
Function: init_io();
Description:
Set GPIO to ready.
**************************************************/
void init_io(void)
{
//先,全部管脚连接到GPIO
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
//设置输出
IO0DIR = IO0DIR | CE | CSN | SCK | LED2 | LED3 | LED4 | TEST;
IO1DIR = IO1DIR | MOSI | LED1;
//设置输入
//IO0DIR = IO0DIR & (~MISO);
/*
//再,设置管脚连接到中断
//配置中断
EXTMODE = 0x00; //设置EINT0中断为电平触发模式
EXTPOLAR = 0x00; //设置EINT0中断为低电平触发
//打开EINT0中断
VICIntSelect = 0x00000000; //设置所有中断分配为IRQ中断(0:IRQ中断 1:FIQ中断)
//向量控制x (0-15个优先级:高-->低)
VICVectCntl1 = (0x20 | 14); //分配中断向量EINT0为Slot1
VICVectAddr1 = (int)IRQ_Eint; //设置中断服务程序地址
EXTINT = 1<<0; //清除EINT0中断标志
VICIntEnable = VICIntEnable | (1<<14); //使能EINT0中断
*/
}
/**************************************************/
/**************************************************
Function: init_chip();
Description:
chip enable(ready to TX or RX Mode),
Spi disable,Spi clock line init high
**************************************************/
void init_chip(void)
{
IO0CLR = CE; // chip enable(CE=0)
IO0SET = CSN; // Spi disable(CSN=1)
IO0CLR = SCK; // Spi clock line init high(SCK=0)
}
/**************************************************/
/**************************************************
Function: SPI_RW();
Description:
Writes one byte to nRF24L01, and return the byte read
from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char byte)
{
unsigned char bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
//MOSI = (byte & 0x80); // output 'byte', MSB to MOSI
//byte = (byte << 1); // shift next bit into MSB..
if((byte & 0x80) != 0)
IO1SET = MOSI;
else
IO1CLR = MOSI;
byte <<= 1;
IO0SET = SCK; // Set SCK high..
//byte |= MISO; // capture current MISO bit
if((IO0PIN & MISO) != 0)
byte |= 0x01;
else
byte |= 0x00;
IO0CLR = SCK; // ..then set SCK low again
}
return(byte); // return read byte
}
/**************************************************/
/**************************************************
Function: SPI_RW_Reg();
Description:
Writes value 'value' to register 'reg'
**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned char status;
IO0CLR = CSN; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it.
IO0SET = CSN; // CSN high again
return(status); // return nRF24L01 status byte
}
/**************************************************/
/**************************************************
Function: SPI_Read();
Description:
Read one byte from nRF24L01 register, 'reg'
**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -