|
用C51作为发送端,atmega128作为nrf24l01的接收端,发送端没问题,已经能正常发送,可是接收端一直接收不到数据,查看STATUS寄存器发现一直为0x0e;接收端模拟代码:
#include <iom128v.h>
#include <macros.h>
#include "Wireless.h"
#include "myconfig.h"
uchar Wireless_Write_Buf(uchar Address, uchar *data, uchar D_Width)
{
unsigned char i, status=0;
PORTD &= ~BIT(SPI_CSN); //使能SPI
delay_nus(2);
status = SPI_RW(Address);
for(i=0; i<D_Width; i++)
{
SPI_RW(*data++);
}
PORTD |= BIT(SPI_CSN);
delay_nus(2);
return status;
}
unsigned char Wireless_RW_Reg(uchar Address,uchar value)
{
unsigned char tem, status=0;
PORTD &= ~BIT(SPI_CSN); //使能SPI
delay_nus(2);
status = SPI_RW(Address);
SPI_RW(value);
PORTD |= BIT(SPI_CSN);
return status;
}
uchar Wireless_Read_Buf(uchar Address, uchar *ReD, uchar D_Width)
{
uchar i;
uchar status;
PORTD &= ~BIT(SPI_CSN); //使能SPI
delay_nus(2);
status = SPI_RW(Address);
for(i=0; i<D_Width; i++)
{
ReD[i] = SPI_RW(0);
}
PORTD |= BIT(SPI_CSN);
delay_nus(2);
return(status);
}
unsigned char SPI_RW_REG(uchar Wreg, uchar value) //用于读出status寄存器
{
unsigned char i;
unsigned char status = 0;
PORTD &= ~BIT(SPI_CSN); //使能SPI
delay_nus(2);
status = SPI_RW(Wreg);
SPI_RW(value);
PORTD |= BIT(SPI_CSN);
return status;
}
unsigned char SPI_RW(uchar wdat)
{
unsigned char i;
unsigned char RWdata;
RWdata = wdat;
for(i=0; i<8; i++)
{
if(RWdata&0x80)
PORTD |= BIT(SPI_MOSI);
else
PORTD &= ~BIT(SPI_MOSI);
RWdata <<= 1;
PORTD |= BIT(SPI_SCK);
if(PIND&0x08)
RWdata |= 0x01;
else
RWdata &= 0xFE;
PORTD &= ~BIT(SPI_SCK);
}
return RWdata;
}
unsigned char SPI_Read(unsigned char reg)
{
unsigned char buff;
PORTD &= ~BIT(SPI_CSN);
delay_nus(2);
SPI_RW(reg);
buff = SPI_RW(0);
PORTD |= BIT(SPI_CSN);
delay_nus(2);
return buff;
}
写数据到nrf24l01寄存器中,再读出来完全没有问题。
#include <iom128v.h>
#include <macros.h>
#include "Wireless.h"
#include "myconfig.h"
#include "delay.h"
#pragma interrupt_handler Wireless_IRQ: 4
unsigned char PRX_ADDRESS[RX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01};
unsigned char TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01};
unsigned char Receive_Data[Receive_Data_WIDTH];
unsigned char flag = 0;
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
void nRF24L01_TxPacket(unsigned char *Buf)
{
PORTD &= ~BIT(nRF_CE);
Wireless_Write_Buf(Wireless_W_REG + Wireless_RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
Wireless_Write_Buf(W_TX_PAYLOAD, Buf, Send_Data_WIDTH); //写发送的数据
Wireless_RW_Reg(Wireless_W_REG + Wireless_CONFIG, 0x0E); //TX mode
PORTD |= BIT(nRF_CE); //CE置高,发送数据
delay_nus(20); //保持CE为高至少10us
}
void SetRX_Mode(void)
{
PORTD &= ~BIT(nRF_CE);
delay_nus(2);
Wireless_RW_Reg(Wireless_W_REG + Wireless_CONFIG, 0x0F);//RX mode
delay_nus(2);
PORTD |= BIT(nRF_CE);
delay_nus(500);
}
//////////////////////////////////////////////////////////////////////
void init_NRF24L01(void)
{
delay_nus(130);
PORTD &= ~BIT(nRF_CE);
delay_nus(2);
PORTD |= BIT(SPI_CSN); //disable SPI
delay_nus(2);
PORTD &= ~BIT(SPI_SCK);
delay_nus(2);
PORTD |= BIT(SPI_IRQ);
delay_nus(2);
Wireless_RW_Reg(Wireless_W_REG + Wireless_SETUP_AW, 0x03);//5 byte
Wireless_Write_Buf(Wireless_W_REG + Wireless_TX_ADDR, PRX_ADDRESS, RX_ADR_WIDTH);
Wireless_Write_Buf(Wireless_W_REG + Wireless_RX_ADDR_P0, PRX_ADDRESS, RX_ADR_WIDTH);//RX地址,与PTX地址一致
Wireless_RW_Reg(Wireless_W_REG + Wireless_EN_AA, 0x00);
Wireless_RW_Reg(Wireless_W_REG + Wireless_EN_RXADDR, 0x01); //for RX
Wireless_RW_Reg(Wireless_W_REG + Wireless_RF_CH, 0);
Wireless_RW_Reg(Wireless_W_REG + Wireless_RX_PW_P0, 4);//for RX ,4bytes
Wireless_RW_Reg(Wireless_W_REG + Wireless_RF_SETUP, 0x07); //Datarate=1Mbps, 0dBm
Wireless_RW_Reg(Wireless_W_REG + Wireless_SETUP_RETE, 0x1F); //wait 500us,for TX
}
//////////////////////////////////////////////////////////////////////
void nRF24L01_RxPacket(unsigned char* rx_buf)
{
uchar temp;
delay_nus(2);
temp = SPI_Read(Wireless_STATUS);
while(temp&0x40)
{
PORTD &= ~BIT(nRF_CE);
Wireless_Read_Buf(R_RX_PAYLOAD, rx_buf, Receive_Data_WIDTH);
PORTD |= BIT(nRF_CE);
flag = 1;
PORTB &= ~(1<<PB0);
}
delay_nus(2);
}
////////////////////////
void Wireless_task(void)
{
uchar tmp;
nRF24L01_RxPacket(Receive_Data);
if(flag == 1)
{
flag = 0;
PORTB |= (1<<PB0);
}
tmp = SPI_Read(Wireless_STATUS);
if(tmp == 0x0e)
{
PORTB &= ~(1<<PB0);
delay_nms(1000);
PORTB |= (1<<PB0);
delay_nms(1000);
}
}
中断程序如下:
void Wireless_IRQ(void)
{
uchar temp;
CLI();
PORTD |= BIT(nRF_CE);
temp = SPI_Read(Wireless_STATUS);
if(temp&0x40)
{
nRF24L01_RxPacket(Receive_Data);
if(Receive_Data[0])
{
PORTB &= ~(1<<PB0);
delay_nms(1000);
PORTB |= (1<<PB0);
delay_nms(1000);
}
}
if(temp&0x20)
{
SetRX_Mode();
}
if(temp&0x10)
{
SetRX_Mode();
}
PORTD &= ~BIT(nRF_CE);
Wireless_RW_Reg(Wireless_W_REG + Wireless_STATUS, temp);
PORTD |= BIT(nRF_CE);
delay_nus(20);
SEI();
}
硬件上,直接操作IO口高低电平没有问题,将IRQ脚拉低也能进中断。电压实测是3.28V左右。
可是就是接收不了数据,很郁闷,求大神指教。。 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|