程序如下:
一、发送:
1、 主程序:
#include<msp430g2553.h>
#include "nrf24L01.h"
#include "time.h"
char TxBuf[5]=
{
//0x03,0x03,0x03,0x03,0x05,0x06,0x07,0x08,
//0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
//0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,
0x31,0x26,0x27,0x28,0x29,//0x30,0x31,0x32,
};
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
RF24L01_IO_set();//初始化SPI引脚
init_NRF24L01();//初始化NRF24L01模块
Init_timeA();
while(1)
{
}
}
2、其他程序:
1)nrf24l01.c
#include <msp430g2553.h>
#include "nrf24L01.h"
char TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
char RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
char sta;
void RF24L01_IO_set(void);
char SPI_RW(char data);
char SPI_Read(char reg);
char SPI_RW_Reg(char reg, char value);
char SPI_Read_Buf(char reg, char *pBuf, char uchars);
char SPI_Write_Buf(char reg, char *pBuf, char uchars);
void SetRX_Mode(void);
char nRF24L01_RxPacket(char* rx_buf);
void nRF24L01_TxPacket(char * tx_buf);
void init_NRF24L01(void);
//===========================RF24L01端口设置==========================================
void RF24L01_IO_set(void)
{
/*
* RF24L01发送
* CE->P1.5
* SCK->P1.4
* CSN->P1.6
* MISO->P1.1
* MOSI->P1.2
* IRQ->P1.7
*/
#ifdef HW_SPI
P1DIR |= BIT5 + BIT6;//NRFCE,NRFCSN输出
P1DIR &= ~BIT7; //IRQ输入
//引脚初始化为SPI功能
P1SEL = BIT1 + BIT2 + BIT4;//UCA0SOMI,UCA0SIMO,UCA0CLK
P1SEL2 = BIT1 + BIT2 + BIT4;
//SPI功能配置
UCA0CTL1 |= UCSWRST;
UCA0CTL0 |= UCMSB + UCMST + UCSYNC; // 3-pin, 8-bit SPI master
UCA0CTL1 |= UCSSEL_2;// SMCLK
UCA0BR0 = 0;
UCA0BR1 = 0;
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
#else
P1DIR |= BIT5 + BIT6;//NRFCE,NRFCSN输出
P1DIR &= ~BIT7; //IRQ输入
P1DIR |= BIT2 + BIT4;//UCA0SIMO,UCA0CLK
P1DIR &= ~BIT1;//UCA0SOMI
#endif
}
//==============================================================================
//函数:uint SPI_RW(uint uchar)
//功能:NRF24L01的SPI写时序
//******************************************************************************
char SPI_RW(char data)
{
#ifdef HW_SPI
while(!(IFG2 & UCA0TXIFG));
UCA0TXBUF = data;
while(!(IFG2 & UCB0RXIFG));
return UCA0RXBUF;
#else
char i,temp=0;
for(i=0;i<8;i++) // output 8-bit
{
if((data & 0x80)==0x80)
{
RF24L01_MOSI_1; // output 'uchar', MSB to MOSI
}
else
{
RF24L01_MOSI_0;
}
//==============================================================================
data = (data << 1); // shift next bit into MSB..
temp <<=1;
RF24L01_SCK_1; // Set SCK high..
RF24L01_MISO_IN;
if(RF24L01_MISO_read)
temp |=0x01;
RF24L01_SCK_0; // ..then set SCK low again
}
return(temp); // return read uchar
#endif
}
//****************************************************************************************************
//函数:uchar SPI_Read(uchar reg)
//功能:NRF24L01的SPI时序
//****************************************************************************************************
char SPI_Read(char reg)
{
char reg_val;
RF24L01_CSN_0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
RF24L01_CSN_1; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
//****************************************************************************************************/
//功能:NRF24L01读写寄存器函数
//****************************************************************************************************/
char SPI_RW_Reg(char reg, char value)
{
char status1;
RF24L01_CSN_0; // CSN low, init SPI transaction
status1 = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
RF24L01_CSN_1; // CSN high again
return(status1); // return nRF24L01 status uchar
}
//****************************************************************************************************/
//函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
//功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
//****************************************************************************************************/
char SPI_Read_Buf(char reg, char *pBuf, char chars)
{
char status2,uchar_ctr;
RF24L01_CSN_0; // Set CSN low, init SPI tranaction
status2 = SPI_RW(reg); // Select register to write to and read status uchar
for(uchar_ctr=0;uchar_ctr<chars;uchar_ctr++)
{
pBuf[uchar_ctr] = SPI_RW(0); //
}
RF24L01_CSN_1;
return(status2); // return nRF24L01 status uchar
}
//*********************************************************************************************************
//函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
//*********************************************************************************************************/
char SPI_Write_Buf(char reg, char *pBuf, char chars)
{
char status1,uchar_ctr;
RF24L01_CSN_0; //SPI使能
status1 = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr<chars; uchar_ctr++) //
{
SPI_RW(*pBuf++);
}
RF24L01_CSN_1; //关闭SPI
return(status1); //
}
//****************************************************************************************************/
//函数:void SetRX_Mode(void)
//功能:数据接收配置
//****************************************************************************************************/
void SetRX_Mode(void)
{
RF24L01_CE_0 ;
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
RF24L01_CE_1;
__delay_cycles(130);//注意不能太小
}
//******************************************************************************************************/
//函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//功能:数据读取后放如rx_buf接收缓冲区中
//******************************************************************************************************/
char nRF24L01_RxPacket(char* rx_buf)
{
char revale=0;
sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况
if(sta&0x40) // 判断是否接收到数据 RX_DR
{
RF24L01_CE_0 ; //SPI使能
SPI_Read_Buf(R_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
revale =1; //读取数据完成标志
}
SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
return revale;
}
//***********************************************************************************************************
//函数:void nRF24L01_TxPacket(char * tx_buf)
//功能:发送 tx_buf中数据
//**********************************************************************************************************/
void nRF24L01_TxPacket(char * tx_buf)
{
RF24L01_CE_0 ; //StandBy I模式
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
SPI_Write_Buf(W_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
// SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
RF24L01_CE_1; //置高CE,激发数据发送
//inerDelay_us(10);
}
//****************************************************************************************
//NRF24L01初始化
//***************************************************************************************/
void init_NRF24L01(void)
{
_delay_cycles(100);
RF24L01_CE_0; // chip enable
RF24L01_CSN_1; // Spi disable
//RF24L01_SCK_0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址频道0,
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0E); // IRQ收发完成中断响应,16位CRC ,主发射模式
}
2)time.c
#include<msp430g2553.h>
#include "nrf24L01.h"
#include "time.h"
void Init_timeA(void)
{
TACTL = TACLR;//计数器清零
CCTL0 = CCIE;//使能定时中断
CCR0 = 50000;//时间间隔50ms
TACTL = TASSEL_2 + MC_1;//SMCLK ,增计数模式
_EINT();//使能全局中断
}
//定时器中断函数入口
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
nRF24L01_TxPacket(TxBuf);
//SPI_RW_Reg(WRITE_REG+STATUS,0xff);
}
二、接受:
1、主程序:
#include"msp430g2553.h"
#include "nrf24L01.h"
#include "time.h"
//#include "uart.h"
char RxBuf[5];
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= BIT0;
P1OUT |= ~BIT0;
RF24L01_IO_set(); //初始化SPI引脚
init_NRF24L01(); //初始化NRF24L01模块
Init_timeA();
//Init_Uart();
while(1)
{
}
}
2、其他程序
1)nrf24l01.C:与发送基本一致
2)time.c:
#include<msp430g2553.h>
#include "nrf24L01.h"
#include "time.h"
//#include "uart.h"
void Init_timeA(void)
{
TACTL = TACLR;//计数器清零
CCTL0 = CCIE;//使能定时中断
CCR0 = 500000;//时间间隔50ms
TACTL = TASSEL_2 + MC_1;//SMCLK ,增计数模式
_EINT();//使能全局中断
}
//定时器中断函数入口
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
SetRX_Mode();//接受模式
if(nRF24L01_RxPacket(RxBuf))
{
P1DIR |= BIT0;
P1OUT |= BIT0;//灯亮
}
//SPI_RW_Reg(WRITE_REG+STATUS,0xff);
}
此帖出自
小平头技术问答
一周热门 更多>