|
楼主 |
发表于 2010-6-3 00:23:33
|
显示全部楼层
贴出代码。帮分析下
#include<reg51.h> //包含单片机寄存器的头文件
#include<intrins.h> //包含_nop_()函数定义的头文件
#include<stdio.h>
#include "api.h"
sbit MISO =P1^3;
sbit MOSI =P1^5;
sbit SCK =P1^4;
sbit CE =P1^6;
sbit CSN =P1^7;
sbit IRQ =P1^2;
sbit LEDC=P2^4;
//----
#define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width
#define TX_PLOAD_WIDTH 20 // 20 bytes TX payload
#define RX_PLOAD_WIDTH 20 // 20 bytes TX payload
#define MMAX_RT 0x10 // Max #of TX retrans interrupt
#define TTX_DS 0x20 // TX data sent interrupt
#define RRX_DR 0x40 // RX data received
uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // Define a static TX address
uchar rx_buf[TX_PLOAD_WIDTH];
uchar tx_buf[TX_PLOAD_WIDTH];
uchar flag;
uchar rx_com_buffer[10];
uchar tx_com_buffer[10];
uchar i;
uchar accept_flag;
unsigned char key_debug;
unsigned int accept_time=0;
unsigned char accept_flag=0;
unsigned char send_flag=0;
uchar bdata sta;
sbit RX_DR =sta^6;
sbit TX_DS =sta^5;
sbit MAX_RT =sta^4;
//----
/*****************************************************
函数功能:延时1ms
(3j+2)*i=(3×33+2)×10=1010(微秒),可以认为是1毫秒
***************************************************/
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
/*****************************************************
函数功能:延时若干毫秒
入口参数:n
***************************************************/
void delay(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
void Uart_Delay(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
//-----------------nrf function--------------------------------------
uchar SPI_RW(uchar byte)
{
uchar 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..
SCK = 1; // Set SCK high..
byte |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(byte); // return read byte
}
//最基本的函数,完成GPIO 模拟SPI 的功能。将输出字节(MOSI)从MSB 循环输出,
//同时将输入字节(MISO)从LSB 循环移入。上升沿读入,下降沿输出。(从SCK 被初始化
//为低电平可以判断出)。
//---------------------------------------------------------------
uchar SPI_RW_Reg(uchar reg, uchar value)
{
uchar status;
CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN = 1; // CSN high again
return(status); // return nRF24L01 status byte
}
//寄存器访问函数:用来设置24L01 的寄存器的值。基本思路就是通过WRITE_REG 命令(也
//就是0x20+寄存器地址)把要设定的值写到相应的寄存器地址里面去,并读取返回值。对于
//函数来说也就是把value 值写到reg 寄存器中。
//需要注意的是,访问NRF24L01 之前首先要enable 芯片(CSN=0;),访问完了以后再disable
//芯片(CSN=1;)。
//---------------------------------------------------------------
uchar SPI_Read(uchar reg)
{
uchar reg_val;
CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN = 1; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
//读取寄存器值的函数:基本思路就是通过READ_REG 命令(也就是0x00+寄存器地址),把
//寄存器中的值读出来。对于函数来说也就是把reg 寄存器的值读到reg_val 中去。
//---------------------------------------------------------------
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes)
{
uchar status,byte_ctr;
CSN = 0; // 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 = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
//接收缓冲区访问函数:主要用来在接收时读取FIFO 缓冲区中的值。基本思路就是通过
//READ_REG 命令把数据从接收FIFO(RD_RX_PLOAD)中读出并存到数组里面去。
//---------------------------------------------------------------
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes)
{
uchar status,byte_ctr;
CSN = 0; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
Uart_Delay(10);
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
SPI_RW(*pBuf++);
CSN = 1; // Set CSN high again
return(status); // return nRF24L01 status byte
}
//-------------------------------------------------------------------
//设定24L01 为接收方式,配置过程详见3.2 Rx 模式初始化过程。
void RX_Mode(void)
{
CE=0;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled..
CE = 1; // Set CE pin high to enable RX device
// This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address
// '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
}
//----------------------------------------------------------------
//设定24L01 为发送方式,配置过程详见3.1 Tx 模式初始化过程。
void TX_Mode(void)
{
CE=0;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps,LNA:HCURR
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_DS enabled..
CE=1;
}
void main(void) //主函数
{
CE=0;
CSN=1;
SCK=0;
flag=0;
LEDC=0;
delay(700);
LEDC=1;
RX_Mode();
while(1)
{
RX_Mode();
sta=SPI_Read(STATUS);
if(RX_DR)
{
CE = 0;
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
flag=1;
}
if(MAX_RT)
{
SPI_RW_Reg(FLUSH_TX,0);
}
SPI_RW_Reg(WRITE_REG+STATUS,sta);
if(flag==1)
{
flag=0;
LEDC=0;
delay(700);
LEDC=1;
}
}
} |
|