新手入门
- 积分
- 28
- 金钱
- 28
- 注册时间
- 2013-8-17
- 在线时间
- 0 小时
|
<p> 首先我调试的是接收,用51单片机作为从机,能够发送出去,但是反过来,用mega8控制NRF2401就接收不到了,不清楚到底是什么情况,在51上调试的挺好的,换到AVR上为什么不行了呢,GPIO的设置应该没有问题,以下是我的程序,各位大神,求指点啊。<br />
#include <mega8.h></p>
<p>//------------------------------------------------------------------------------<br />
//--------------------------------NRF24L01接口定义--------------------------------<br />
//------------------------------------------------------------------------------<br />
//#define NRF24L01_MISO PD5 //输入0 <br />
#define Hign_24L01_MISO PORTD |= (1 << PORTD5) <br />
#define Low_24L01_MISO PORTD &= ~(1 << PORTD5)<br />
#define Read_24L01_MISO PIND & (1 << PORTD5) </p>
<p>//#define NRF24L01_MOSI PD6 //输出1<br />
#define Hign_24L01_MOSI PORTD |= (1 << PORTD6) <br />
#define Low_24L01_MOSI PORTD &= ~(1 << PORTD6)<br />
#define Read_24L01_MOSI PIND & (1 << PIND6) </p>
<p>//#define NRF24L01_CSN PB0 //输出1<br />
#define Low_24L01_CSN PORTB &= ~(1 << PORTB0)<br />
#define Hign_24L01_CSN PORTB |= (1 << PORTB0)</p>
<p>//#define NRF24L01_SCK PD7 //输出1<br />
#define Hign_24L01_SCK PORTD |= (1 << PORTD7) <br />
#define Low_24L01_SCK PORTD &= ~(1 << PORTD7) <br />
#define Read_24L01_SCK PIND & (1 << PIND7);</p>
<p>//#define NRF24L01_CE PB1 //输出1<br />
#define Hign_24L01_CE PORTB |= (1 << PORTB1) <br />
#define Low_24L01_CE PORTB &= ~(1 << PORTB1) <br />
#define Read_24L01_CE PINB & (1 << PINB1)</p>
<p>//#define NRF24L01_IRQ PD3 //输入<br />
#define Hign_NRF24L01_IRQ PORTD |= (1 << PORTD3) <br />
#define Low_NRF24L01_IRQ PORTD &= ~(1 << PORTD3)<br />
#define Read_NRF24L01_IRQ PIND & (1 << PIND3)<br />
#define LED PORTB.2</p>
<p><br />
unsigned int sta;<br />
//*********************************************NRF24L01*************************************<br />
#define TX_ADR_WIDTH 5 // 5 uints TX address width<br />
#define RX_ADR_WIDTH 5 // 5 uints RX address width<br />
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload<br />
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload</p>
<p>unsigned char TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址<br />
unsigned char RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址<br />
//***************************************NRF24L01寄存器指令*******************************************************<br />
#define READ_REG 0x00 // 读寄存器指令<br />
#define WRITE_REG 0x20 // 写寄存器指令<br />
#define RD_RX_PLOAD 0x61 // 读取接收数据指令<br />
#define WR_TX_PLOAD 0xA0 // 写待发数据指令<br />
#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令<br />
#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令<br />
#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令<br />
#define NOP1 0xFF // 保留<br />
//*************************************SPI(nRF24L01)寄存器地址****************************************************<br />
#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式<br />
#define EN_AA 0x01 // 自动应答功能设置<br />
#define EN_RXADDR 0x02 // 可用信道设置<br />
#define SETUP_AW 0x03 // 收发地址宽度设置<br />
#define SETUP_RETR 0x04 // 自动重发功能设置<br />
#define RF_CH 0x05 // 工作频率设置<br />
#define RF_SETUP 0x06 // 发射速率、功耗功能设置<br />
#define STATUS 0x07 // 状态寄存器<br />
#define OBSERVE_TX 0x08 // 发送监测功能<br />
#define CD 0x09 // 地址检测 <br />
#define RX_ADDR_P0 0x0A // 频道0接收数据地址<br />
#define RX_ADDR_P1 0x0B // 频道1接收数据地址<br />
#define RX_ADDR_P2 0x0C // 频道2接收数据地址<br />
#define RX_ADDR_P3 0x0D // 频道3接收数据地址<br />
#define RX_ADDR_P4 0x0E // 频道4接收数据地址<br />
#define RX_ADDR_P5 0x0F // 频道5接收数据地址<br />
#define TX_ADDR 0x10 // 发送地址寄存器<br />
#define RX_PW_P0 0x11 // 接收频道0接收数据长度<br />
#define RX_PW_P1 0x12 // 接收频道0接收数据长度<br />
#define RX_PW_P2 0x13 // 接收频道0接收数据长度<br />
#define RX_PW_P3 0x14 // 接收频道0接收数据长度<br />
#define RX_PW_P4 0x15 // 接收频道0接收数据长度<br />
#define RX_PW_P5 0x16 // 接收频道0接收数据长度<br />
#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置<br />
//**************************************************************************************<br />
void Delay(unsigned int s);<br />
void init_NRF24L01(void);<br />
unsigned int SPI_RW(unsigned char data);<br />
unsigned char SPI_Read(unsigned char reg);<br />
unsigned int SPI_RW_Reg(unsigned char reg,unsigned char value);<br />
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf,unsigned char uchars);<br />
unsigned int SPI_Write_Buf(unsigned char reg, unsigned char *pBuf,unsigned char uchars);<br />
void SetRX_Mode(void);<br />
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);<br />
void nRF24L01_TxPacket(unsigned char * tx_buf);</p>
<p>unsigned char RxBuf[20]={0};<br />
unsigned char TxBuf[20]={0};</p>
<p>//*****************************************长延时*****************************************<br />
void Delay(unsigned int s)<br />
{<br />
unsigned int i;<br />
for(i=0; i<s; i++);<br />
#asm("nop");<br />
}</p>
<p>//****************************************************************************************************<br />
//*函数:uint SPI_RW(uint uchar)<br />
//*/功能:NRF24L01的SPI写时序<br />
//****************************************************************************************************/<br />
unsigned int SPI_RW(unsigned char data)<br />
{<br />
unsigned int i,temp=0;<br />
for(i=0;i<8;i++) // output 8-bit<br />
{<br />
if(data & 0x80)<br />
{<br />
Hign_24L01_MOSI ; // output 'uchar', MSB to MOSI<br />
}<br />
else<br />
{<br />
Low_24L01_MOSI; <br />
} <br />
data = (data << 1); // shift next bit into MSB..<br />
temp<<=1;<br />
Hign_24L01_SCK ; // Set SCK high..<br />
if(PINB&0x20)temp++; // capture current MISO bit,because the MISO bit is the fourth<br />
Low_24L01_SCK; // ..then set SCK low again<br />
}<br />
return(temp); // return read uchar MISO vaule<br />
}<br />
//****************************************************************************************************<br />
//*函数:uchar SPI_Read(uchar reg)<br />
//*功能:NRF24L01的SPI时序<br />
/****************************************************************************************************/<br />
unsigned char SPI_Read(unsigned char reg)<br />
{<br />
unsigned char reg_val;<br />
Low_24L01_CSN; // CSN low, initialize SPI communication...<br />
SPI_RW(reg); // Select register to read from..<br />
reg_val = SPI_RW(0XFF); // ..then read registervalue<br />
Hign_24L01_CSN; // CSN high, terminate SPI communication <br />
return(reg_val); // return register value<br />
}<br />
//****************************************************************************************************/<br />
//*功能:NRF24L01读写寄存器函数<br />
//****************************************************************************************************/<br />
unsigned int SPI_RW_Reg(unsigned char reg, unsigned char value)<br />
{<br />
unsigned int status;<br />
Low_24L01_CSN; // CSN low, init SPI transaction<br />
status = SPI_RW(reg); // select register<br />
SPI_RW(value); // ..and write value to it..<br />
Hign_24L01_CSN; // CSN high again<br />
<br />
return(status); // return nRF24L01 status uchar<br />
}<br />
//****************************************************************************************************/<br />
//*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)<br />
//*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数<br />
//****************************************************************************************************/<br />
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)<br />
{<br />
unsigned char status1,uchar_ctr;<br />
Low_24L01_CSN; // Set CSN low, init SPI tranaction<br />
status1 = SPI_RW(reg); // 发送寄存器值(位置),并读取状态值<br />
for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)<br />
pBuf[uchar_ctr] = SPI_RW(0xff); // <br />
Hign_24L01_CSN; <br />
return(status1); // 返回状态寄存器的值<br />
}<br />
//*********************************************************************************************************<br />
//*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)<br />
//*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数<br />
/*********************************************************************************************************/<br />
unsigned int SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)<br />
{<br />
unsigned int status1,uchar_ctr;<br />
Low_24L01_CSN; //SPI使能 <br />
status1 = SPI_RW(reg); <br />
for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //<br />
SPI_RW(*pBuf++);<br />
Hign_24L01_CSN; //关闭SPI<br />
return(status1); // <br />
}<br />
//****************************************************************************************************/<br />
//*函数:void SetRX_Mode(void)<br />
//*功能:数据接收配置 <br />
//****************************************************************************************************/<br />
void SetRX_Mode(void)<br />
{<br />
Low_24L01_CE;<br />
//SPI_RW_Reg(FLUSH_RX,0xff);<br />
//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0d); <br />
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); <br />
Hign_24L01_CE;<br />
Delay(5000);//注意不能太小 130us <br />
}<br />
//******************************************************************************************************/<br />
//*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)<br />
//*功能:数据读取后放如rx_buf接收缓冲区中<br />
/******************************************************************************************************/<br />
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)<br />
{<br />
unsigned char revale=0;<br />
sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况 <br />
SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志 <br />
if(sta&0x40) // 判断是否接收到数据<br />
{<br />
//Low_24L01_CE; //SPI使能<br />
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer<br />
//Hign_24L01_CE;<br />
SPI_RW_Reg(FLUSH_RX,0xff);<br />
revale =1; //读取数据完成标志<br />
}<br />
<br />
return revale;<br />
}<br />
//***********************************************************************************************************<br />
//*函数:void nRF24L01_TxPacket(char * tx_buf)<br />
//*功能:发送 tx_buf中数据<br />
/**********************************************************************************************************/<br />
void nRF24L01_TxPacket(unsigned char * tx_buf)<br />
{<br />
Low_24L01_CE; //StandBy I模式 <br />
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址<br />
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据 <br />
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送<br />
Hign_24L01_CE; //置高CE,激发数据发送<br />
Delay(600);<br />
}<br />
//****************************************************************************************<br />
//*NRF24L01初始化<br />
//***************************************************************************************/<br />
void init_NRF24L01(void)<br />
{</p>
<p> Low_24L01_CE; // chip enable<br />
Hign_24L01_CSN; // Spi disable <br />
Low_24L01_SCK; // Spi clock line init high<br />
//SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址 <br />
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址<br />
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许 <br />
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21 <br />
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致<br />
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为20字节<br />
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //设置发射速率为1MHZ,发射功率为最大值0dB<br />
//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收<br />
<br />
}<br />
void UART_Send(unsigned char i)<br />
{ <br />
while (UCSRA.5==0);// waiting for the buffer is empty<br />
UDR=i; //PUT data into buffer,sends the data<br />
}</p>
<p><br />
void ini_devices(void)<br />
{ //initial ports<br />
DDRB = 0x07;<br />
DDRC = 0xff;<br />
DDRD = 0xc0;<br />
SFIOR&=~(1<<UD);<br />
//PORTB=0;<br />
LED=1;<br />
PORTD=0x00; <br />
// USART intitial<br />
UBRRH=0;<br />
UBRRL=0x2F; //baud rate:9600<br />
UCSRB=0x18; //enable transmitter and receiver<br />
UCSRC=0x86; //set frame format:8data,1stop bit,enable parity <br />
init_NRF24L01() ;</p>
<p>}<br />
//******************************************************************************<br />
void main(void)<br />
{<br />
<br />
ini_devices();<br />
<br />
<br />
//******************************************************************************<br />
RxBuf[1] = 0x00;<br />
RxBuf[2] = 0x00;<br />
<br />
//nRF24L01_TxPacket(TxBuf); <br />
while(1) <br />
{ SetRX_Mode();<br />
RxBuf[1] = 0x00;<br />
RxBuf[2] = 0x00;<br />
<br />
<br />
nRF24L01_RxPacket(RxBuf);<br />
// nRF24L01_TxPacket(TxBuf);<br />
//SPI_RW_Reg(WRITE_REG + STATUS, 0xff); <br />
// Delay(1000); <br />
<br />
if(RxBuf[1]==1)<br />
{ <br />
LED=1;<br />
}<br />
//Delay(10000);<br />
<br />
RxBuf[1] = 0x00;<br />
RxBuf[2] = 0x00;<br />
LED=0;<br />
<br />
<br />
<br />
<br />
}<br />
<br />
}<br />
</p> |
|