main。c
#include <msp430.h>
#include "NRF24L01.h"
#include "delay.h"
#define _EINT() __bis_SR_register(GIE)
#define _DINT() __bic_SR_register(GIE)
/*************************************************************************
MSP430内部看门狗初始化
*************************************************************************/
void WDT_Init()
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
}
/*************************************************************************
MSP430锁相环初始化
*************************************************************************/
void CLK_Init()
{
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
// Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
UCSCTL2 |= 121; // Set DCO Multiplier for 8MHz
// (N + 1) * FLLRef = Fdco
// (249 + 1) * 32768 = 8MHz
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle
__delay_cycles(250000);
}
void main(void)
{
WDT_Init();
CLK_Init(); //MCLK = SMCLK = 4M
NRF_Init(); //初始化NRF24L01
while(NRF_Check()) //检查NRF24L01是否在位.
{
LED_L; //LED
}
LED_H;
RX_Mode();
_EINT();
while(1)
{
NRF_RxPacket(rx_buf);
delay_ms(10);
}
}
Nrf2401.c
#include "NRF24L01.h"
uchar TX_Address[5]={0x34,0x34,0x10,0x10,0x01};//设置发送端的地址
uchar RX_Address[5]={0x34,0x34,0x10,0x10,0x01};//设置接收端的地址
uchar tx_buf[TX_PLOAD_WIDTH]={0x00};
uchar rx_buf[RX_PLOAD_WIDTH]={0x00};
/****************************STM32程序***************************************/
//初始化24L01的IO口
void NRF_Init(void)
{
P4SEL=0;
P4DIR |= CSN + MOSI + SCK + CE; //SOMI , IRQ INPUT
P4OUT&=~(CSN + MOSI + SCK + CE);
CSN_H;
//P4IES |= IRQ; //下降沿置标志位
//P4IE |= IRQ;
P1DIR |= BIT0; //LED
P1OUT |= BIT0; //LED
}
void RX_Mode(void)
{
CE_L;
//设置端址
//SPI_Write_Buf(WRITE_REG_NRF + TX_ADDR, TX_Address, TX_ADR_WIDTH);
SPI_Write_Buf(WRITE_REG_NRF + RX_ADDR_P0, TX_Address, TX_ADR_WIDTH);
//初始化配置字
SPI_WR_Reg(WRITE_REG_NRF + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_WR_Reg(WRITE_REG_NRF + EN_RXADDR, 0x01); // Enable Pipe0
SPI_WR_Reg(WRITE_REG_NRF + SETUP_AW,0x03);
SPI_WR_Reg(WRITE_REG_NRF + SETUP_RETR,0x1a);
SPI_WR_Reg(WRITE_REG_NRF + RF_CH, 40);
SPI_WR_Reg(WRITE_REG_NRF + RX_PW_P0, TX_PLOAD_WIDTH);
SPI_WR_Reg(WRITE_REG_NRF + RF_SETUP, 0x0f);
//PWR_UP、TX_DS、IRQ、EN CRC、rX
SPI_WR_Reg(WRITE_REG_NRF + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes)& Prim:RX. RX_DR enabled..
//清除标志位
uchar sta=SPI_RD_Reg(STATUS); //读取状态寄存器的值
SPI_WR_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS,RX_DR或MAX_RT中断标志
//CE_H; // Set CE pin high to enable RX device
}
void TX_Mode(void)
{
CE_L;
//设置发送端的地址
SPI_Write_Buf(WRITE_REG_NRF+TX_ADDR,TX_Address,TX_ADR_WIDTH);//写TX节点地址
SPI_Write_Buf(WRITE_REG_NRF+RX_ADDR_P0, RX_Address,RX_ADR_WIDTH); //设置TX节点地址,主要为了使能ACK
//初始化配置字
SPI_WR_Reg(WRITE_REG_NRF + EN_AA, 0x01); // Enable Auto.Ack:Pipe0//
SPI_WR_Reg(WRITE_REG_NRF + EN_RXADDR, 0x01); // Enable Pipe0
SPI_WR_Reg(WRITE_REG_NRF + SETUP_AW,0x03);
SPI_WR_Reg(WRITE_REG_NRF + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
SPI_WR_Reg(WRITE_REG_NRF + RF_CH, 40); // Select RF channel 2.45G
SPI_WR_Reg(WRITE_REG_NRF + RF_SETUP, 0x0f); // TX_PWR:0dBm, Datarate:2Mbps,LNA:HCURR
SPI_WR_Reg(WRITE_REG_NRF + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes)& Prim:TX. MAX_RT & TX_DS enabled..//
//清除TX_DS标志位
uchar sta=SPI_RD_Reg(STATUS); //读取状态寄存器的值
SPI_WR_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS或MAX_RT中断标志
//CE_H;
}
//检测24L01是否存在
//返回值:0,成功;1,失败
uchar NRF_Check(void)
{
uchar buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
uchar i;
SPI_Write_Buf(WRITE_REG_NRF+TX_ADDR,buf,5);//写入5个字节的地址.
SPI_Read_Buf(TX_ADDR,buf,5); //读出写入的地址
for(i=0;i<5;i++)if(buf[i]!=0XA5)break;
if(i!=5)return 1;//检测24L01错误
return 0; //检测到24L01
}
uchar NRF_TxPacket(uchar *txbuf)
{
uchar sta;
CE_L;
SPI_Write_Buf(WR_TX_PLOAD,txbuf,TX_PLOAD_WIDTH);//写数据到TX BUF 32个字节
CE_H;//启动发送
delay_us(10);
while((P2IN&IRQ)!=0);//等待发送完成
sta=SPI_RD_Reg(STATUS); //读取状态寄存器的值
SPI_WR_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&MAX_TX!=0)//达到最大重发次数
{
SPI_WR_Reg(FLUSH_TX,0xff);//清除TX FIFO寄存器
return MAX_TX;
}
if(sta&TX_OK)//发送完成
{
LED_J;
return TX_OK;
}
return 0xff;//其他原因发送失败
}
uchar NRF_RxPacket(uchar *rxbuf)
{
uchar sta;
CE_H; //CE 高电平>=130us,接收模式
delay_us(130);
sta=SPI_RD_Reg(STATUS); //读取状态寄存器的值
SPI_WR_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&RX_OK)
{
CE_L;
LED_J;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,RX_PLOAD_WIDTH);//读取数据
SPI_WR_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器
return 0;
}
else return 1;
}
/***************************spi程序**************************/
uchar SPI_RW(uchar data)
{
uchar bit;
for(bit=0;bit<8;bit++)
{ //先高位,后低位
if(data & BIT7) MOSI_H;
else MOSI_L;
delay_us(2); //数据建立时间 1us
SCK_H;
delay_us(2);
data <<= 1; //一条指令的时间
if(SPI_IN) data |= BIT0; // capture current MISO bit
else data &=~BIT0;
SCK_L;
delay_us(1);
}
delay_us(1);
return(data); // return read data
}
uchar SPI_WR_Reg(uchar reg, uchar value)
{
uchar status;
CSN_L; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN_H; // CSN high again
return(status); // return nRF24L01 status byte
}
uchar SPI_RD_Reg(uchar reg)
{
uchar reg_val;
CSN_L; // CSN low, initialize SPI communication
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN_H; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/*接收缓冲区访问函数:主要用来在接收时读取FIFO 缓冲区中的值。
基本思路就是通过READ_REG 命令把数据从接收FIFO(RD_RX_PLOAD)中读出并存到数组里面去。*/
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes)
{
uchar status,byte_ctr;
CSN_L; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01
CSN_H; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/*发射缓冲区访问函数:主要用来把数组里的数放到发射FIFO 缓冲区中。
基本思路就是通过WRITE_REG 命令把数据存到发射FIFO(WR_TX_PLOAD)中去。*/
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes)
{
uchar status,byte_ctr;
CSN_L; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
delay_us(1);
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
SPI_RW(*pBuf++);
CSN_H; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/***************************中断程序**************************/
#pragma vector=PORT2_VECTOR
__interrupt void p2(void)
{
LED_J; //LED
P2IFG =0;
}
NRF2401。h
#ifndef NRF24L01_H
#define NRF24L01_H
#include "msp430f5529.h"
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int
//POW_UP通过配置子写入
#define CSN BIT2 //每次SPI前一个下降沿,SPI后上升沿
#define MOSI BIT0
#define MISO BIT1
#define SCK BIT2
#define CE BIT4
#define IRQ BIT5
#define CSN_H P2OUT |= CSN
#define CSN_L P2OUT &=~CSN
#define CE_H P3OUT |= CE
#define CE_L P3OUT &=~CE
#define SCK_H P3OUT |= SCK
#define SCK_L P3OUT &=~SCK
#define MOSI_H P3OUT |= MOSI
#define MOSI_L P3OUT &=~MOSI
#define SPI_IN P3IN&MISO
//SPI(NRF24L01)寄存器地址
#define CONFIG 0x00 //配置寄存器地址;bit0:1接收模式,0发射模式;bit1:电选择;bit2:CRC模式;bit3:CRC使能;
//bit4:中断MAX_RT(达到最大重发次数中断)使能;bit5:中断TX_DS使能;bit6:中断RX_DR使能
#define EN_AA 0x01 //使能自动应答功能 bit0~5,对应通道0~5
#define EN_RXADDR 0x02 //接收地址允许,bit0~5,对应通道0~5
#define SETUP_AW 0x03 //设置地址宽度(所有数据通道):bit1,0:00,3字节;01,4字节;02,5字节;
#define SETUP_RETR 0x04 //建立自动重发;bit3:0,自动重发计数器;bit7:4,自动重发延时 250*x+86us
#define RF_CH 0x05 //RF通道,bit6:0,工作通道频率;
#define RF_SETUP 0x06 //RF寄存器;bit3:传输速率(0:1Mbps,1:2Mbps);bit2:1,发射功率;bit0:低噪声放大器增益
#define STATUS 0x07 //状态寄存器;bit0:TX FIFO满标志;bit3:1,接收数据通道号(最大:6);bit4,达到最多次重发
//bit5:数据发送完成中断;bit6:接收数据中断;
#define OBSERVE_TX 0x08 //发送检测寄存器,bit7:4,数据包丢失计数器;bit3:0,重发计数器
#define CD 0x09 //载波检测寄存器,bit0,载波检测;
#define RX_ADDR_P0 0x0A //数据通道0接收地址,最大长度5个字节,低字节在前
#define RX_ADDR_P1 0x0B //数据通道1接收地址,最大长度5个字节,低字节在前
#define RX_ADDR_P2 0x0C //数据通道2接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P3 0x0D //数据通道3接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P4 0x0E //数据通道4接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define RX_ADDR_P5 0x0F //数据通道5接收地址,最低字节可设置,高字节,必须同RX_ADDR_P1[39:8]相等;
#define TX_ADDR 0x10 //发送地址(低字节在前),ShockBurstTM模式下,RX_ADDR_P0与此地址相等
#define RX_PW_P0 0x11 //接收数据通道0有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P1 0x12 //接收数据通道1有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P2 0x13 //接收数据通道2有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P3 0x14 //接收数据通道3有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P4 0x15 //接收数据通道4有效数据宽度(1~32字节),设置为0则非法
#define RX_PW_P5 0x16 //接收数据通道5有效数据宽度(1~32字节),设置为0则非法
#define NRF_FIFO_STATUS 0x17 //FIFO状态寄存器;bit0,RX FIFO寄存器空标志;bit1,RX FIFO满标志;bit2,3,保留
//bit4,TX FIFO空标志;bit5,TX FIFO满标志;bit6,1,循环发送上一数据包.0,不循环;
//24L01发送接收数据宽度定义
#define TX_ADR_WIDTH 5 //5字节的地址宽度
#define RX_ADR_WIDTH 5 //5字节的地址宽度
#define TX_PLOAD_WIDTH 32 //32字节的用户数据宽度
#define RX_PLOAD_WIDTH 32 //32字节的用户数据宽度
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//NRF24L01寄存器操作命令
#define READ_REG_NRF 0x00 //读配置寄存器,低5位为寄存器地址
#define WRITE_REG_NRF 0x20 //写配置寄存器,低5位为寄存器地址
#define RD_RX_PLOAD 0x61 //读RX有效数据,1~32字节
#define WR_TX_PLOAD 0xA0 //写TX有效数据,1~32字节
#define FLUSH_TX 0xE1 //清除TX FIFO寄存器.发射模式下用
#define FLUSH_RX 0xE2 //清除RX FIFO寄存器.接收模式下用
#define REUSE_TX_PL 0xE3 //重新使用上一包数据,CE为高,数据包被不断发送.
#define NOP 0xFF //空操作,可以用来读状态寄存器
#define MAX_TX 0x10 //达到最大发送次数中断
#define TX_OK 0x20 //TX发送完成中断
#define RX_OK 0x40 //接收到数据中断
extern uchar tx_buf[TX_PLOAD_WIDTH];
extern uchar rx_buf[RX_PLOAD_WIDTH];
void TX_Mode(void);
void RX_Mode(void);
uchar SPI_RW(uchar data);
uchar SPI_WR_Reg(uchar reg, uchar value);
uchar SPI_RD_Reg(uchar reg);
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes);
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes);
void NRF_Init(void);
uchar NRF_Check(void);
uchar NRF_TxPacket(uchar *txbuf);
uchar NRF_RxPacket(uchar *rxbuf);
/****************LED指示灯*********************/
#define LED_J P1OUT ^= BIT0
#define LED_H P1OUT |= BIT0
#define LED_L P1OUT &=~BIT0
#endif
|