讯通肯定不喜欢我了:)
这次调试比较痛苦,因为2401A发送没有任何的指示,根本没法判断问题,必须发送板和接收板程序都要通过才能知道程序对了。
我在楼道里面,使用250KBps,可以到40米,没有正式到楼下拉距离测试。
还是ICCAVR环境 MEGA16L+11.0592M
点击此处下载armok01113426.rar
为什么上一贴内容自动加上了呢?中文提示还乱码。。。。
贴个主要模块的代码,好参考:
#include "config.h"
extern void DelayMs(uchar ms);
extern void DelayUs(uint us);
//×¢Ò⣺
//1¡¢nef2401AÅäÖÃ×ÖÊÇÏÈдµÚ15×ֽڵģ¡£¡
//2¡¢·¢ËͺóÒªÑÓʱµÈ·¢ËͽáÊøºó²ÅÄÜת»Ø½ÓÊÕģʽ£¬·ñÔòÊý¾Ý·¢ËͲ»³ö
#define SIZE 25 //×î´óÊý¾Ý°ü³¤¶È 32-ADDR4-CRC2=26Byte
#define ADDRSIZE 4 //µØÖ·³¤¶ÈByte
#define CH_NO 2 //Ƶ¶Î±àºÅ
#define BPS_1M //±àÒë¿ØÖÆ£º1M or 250Kbps
uchar TxBuf[SIZE],RxBuf[SIZE];
/*-------------------------------------------------------------------------------------------
nrf2401AÅäÖüĴæÆ÷ºê¶¨Òå
nrf2401AÅäÖüĴæÆ÷(15Byte)
¹¤×÷ƵÂÊf=2.4G+CH_NO*1MHz
-------------------------------------------------------------------------------------------*/
#define RX_ADDRESS 0xCCCCCCCC //½ÓÊÕÓÐЧµØÖ·(±¾·½)
#define TX_ADDRESS 0xCCCCCCCC //·¢ËÍÓÐЧµØÖ·(¶Ô·½)
#define CH_NO_FREQ CH_NO<<1 //¹¤×÷Ƶ¶Î Byte1.7-2
#define RX_EN 1 //½ÓÊÕ·¢ËÍÇл» Byte1.0 1½ÓÊÕ 0·¢ËÍ
#define RX2EN_YES 128 //µÚ2½ÓÊÕͨµÀʹÄÜ1 Byte2.7
#define RX2EN_NO 0 //µÚ2½ÓÊÕͨµÀʹÄÜ0 Byte2.7
#define CM_SHOCK 64 //ÊÕ·¢Ä£Ê½ShockBurst Byte2.6
#define CM_DIRECT 0 //ÊÕ·¢Ä£Ê½DirectMode Byte2.6
#define RFDR_1M 32 //¿ÕÖÐBaud=1M Byte2.5
#define RFDR_250K 0 //¿ÕÖÐBaud=250K Byte2.5
#define XOF_20MHz 4*4 //¾§ÌåÕñµ´Æ÷ƵÂÊ20MHz Byte2.4~2
#define XOF_16MHz 3*4 //¾§ÌåÕñµ´Æ÷ƵÂÊ16MHz
#define XOF_12MHz 2*4 //¾§ÌåÕñµ´Æ÷ƵÂÊ12MHz
#define XOF_8MHz 1*4 //¾§ÌåÕñµ´Æ÷ƵÂÊ8MHz
#define XOF_4MHz 0*4 //¾§ÌåÕñµ´Æ÷ƵÂÊ4MHz
#define PA_PWR_0 0 //Êä³ö¹¦ÂÊ-20dBm Byte2.1~0
#define PA_PWR_1 1 //Êä³ö¹¦ÂÊ-10dBm
#define PA_PWR_2 2 //Êä³ö¹¦ÂÊ-5dBm
#define PA_PWR_3 3 //Êä³ö¹¦ÂÊ 0dBm
#define ADDR_W (ADDRSIZE<<5) //µØַλÊý Byte3.7~2
#define CRC_MODE_16BIT 2 //CRC16ģʽ Byte3.1 1
#define CRC_MODE_8BIT 0 //CRC8ģʽ Byte3.1
#define CRC_EN 1 //CRCʹÄÜ Byte3.0 1
#define CRC16_EN 3 //CRC16ģʽʹÄÜ
#define CRC8_EN 1 //CRC8ģʽʹÄÜ
#define RX_ADDRESS_0 RX_ADDRESS & 0xff //ͨµÀ1½ÓÊÕÓÐЧµØÖ·µÚ1×Ö½Ú Byte4
#define RX_ADDRESS_1 (RX_ADDRESS >> 8) & 0xff //ͨµÀ1½ÓÊÕÓÐЧµØÖ·µÚ2×Ö½Ú Byte5
#define RX_ADDRESS_2 (RX_ADDRESS >> 16) & 0xff //ͨµÀ1½ÓÊÕÓÐЧµØÖ·µÚ3×Ö½Ú Byte6
#define RX_ADDRESS_3 RX_ADDRESS >> 24 //ͨµÀ1½ÓÊÕÓÐЧµØÖ·µÚ4×Ö½Ú Byte7
#define RX_ADDRESS_4 0 //ͨµÀ1½ÓÊÕÓÐЧµØÖ·µÚ5×Ö½Ú Byte8
#define RX_ADDRESS2 0 //Byte9,10,11,12,13×Ö½ÚΪͨµÀ2½ÓÊÕµØÖ·
#define DATA1_W SIZE*8 //½ÓÊÕͨµÀ1Êý¾Ý°ü³¤¶È£¬Bit¼ÆËã Byte14
#define DATA2_W SIZE*8 //½ÓÊÕͨµÀ2Êý¾Ý°ü³¤¶È£¬Bit¼ÆËã Byte15
#define TX_ADDRESS_4 0 //·¢ËÍÓÐЧµØÖ·µÚ5×Ö½Ú
#define TX_ADDRESS_3 TX_ADDRESS >> 24 //·¢ËÍÓÐЧµØÖ·µÚ4×Ö½Ú
#define TX_ADDRESS_2 (TX_ADDRESS >> 16) & 0xff //·¢ËÍÓÐЧµØÖ·µÚ3×Ö½Ú
#define TX_ADDRESS_1 (TX_ADDRESS >> 8) & 0xff //·¢ËÍÓÐЧµØÖ·µÚ2×Ö½Ú
#define TX_ADDRESS_0 TX_ADDRESS & 0xff //·¢ËÍÓÐЧµØÖ·µÚ1×Ö½Ú
//д1Byte
void nrf2401A_WR(uchar val)
{uchar temp,i;
//MSB first ¸ßλ¿ªÊ¼,ÏÈÊä³öDATA£¬È»ºóCLK_Up£¬ÔÙCLK_Dn
SET(DDRA,DATA);//ÖÃDATAÊä³ö
temp=val;
for (i=0;i<8;i++)
{
if ((temp&0x80)>0)
SET(PORTA,DATA);
else
CLR(PORTA,DATA);
NOP();
SET(PORTA,CLK);
NOP();
CLR(PORTA,CLK);
temp=temp<<1;
}
}
//¶Á1Byte
uchar nrf2401A_RD()
{uchar temp,i;
//MSB first ¸ßλ¿ªÊ¼,ÏȶÁÈëDATA£¬È»ºóCLK_Up£¬ÔÙCLK_Dn
CLR(DDRA,DATA);//ÖÃDATAÊäÈë
temp=0;
for (i=0;i<8;i++)
{
temp=temp<<1;
if ((PINA&(1<<DATA))>0)
temp=temp|1;
NOP();
SET(PORTA,CLK);
NOP();
CLR(PORTA,CLK);
}
return temp;
}
//PwrDown->StandBy
void nrf2401A_StandBy()
{
SET(PORTA,PWR2);
CLR(PORTA,CE);
CLR(PORTA,CS);
}
//->PwrDown
void nrf2401A_Off()
{
CLR(PORTA,PWR2);
CLR(PORTA,CE);
CLR(PORTA,CS);
}
//ÅäÖÃģʽ
void nrf2401A_RwOn()
{
SET(PORTA,PWR2);
CLR(PORTA,CE);
SET(PORTA,CS);
}
//¹¤×÷ģʽ
void nrf2401A_Run()
{
SET(PORTA,PWR2);
CLR(PORTA,CS);//first
SET(PORTA,CE);
}
//ÅäÖÃnRF2401A
void nrf2401A_Config(void)
{
nrf2401A_RwOn();//ÅäÖÃ״̬
nrf2401A_WR(DATA2_W);
nrf2401A_WR(DATA1_W);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS_4);
nrf2401A_WR(RX_ADDRESS_3);
nrf2401A_WR(RX_ADDRESS_2);
nrf2401A_WR(RX_ADDRESS_1);
nrf2401A_WR(RX_ADDRESS_0);
nrf2401A_WR(CRC16_EN|ADDR_W);//CRC16,AddrWidth
#ifdef BPS_1M
nrf2401A_WR(PA_PWR_3|XOF_16MHz|RFDR_1M|CM_SHOCK);//0dbm,16M,1Mbps,ShockBurst
#else
nrf2401A_WR(PA_PWR_3|XOF_16MHz|CM_SHOCK);//0dbm,16M,250Kbps,ShockBurst
#endif
nrf2401A_WR(CH_NO_FREQ|RX_EN); //RxMode,Chan
nrf2401A_StandBy();//Ö´ÐÐÅäÖÃ
}
//½ÓÊÕģʽ
void nrf2401A_RxOn()
{
nrf2401A_RwOn();
nrf2401A_WR(CH_NO_FREQ|RX_EN); //RxMode,Chan
nrf2401A_Run();
}
//·¢ËÍģʽ
void nrf2401A_TxOn()
{
nrf2401A_RwOn();
nrf2401A_WR(CH_NO_FREQ); //TxMode,Chan
nrf2401A_Run();
}
//³õʼ»¯nrf2401A
void nrf2401A_Init()
{
CLR(PORTA,CLK);//
nrf2401A_StandBy();
DelayMs(10);//!!!must >3ms
nrf2401A_Config();
nrf2401A_RxOn();//½ÓÊÕ״̬
}
//дTxµØÖ·
void nrf2401A_SetTxAddr()
{
nrf2401A_WR(TX_ADDRESS_3);
nrf2401A_WR(TX_ADDRESS_2);
nrf2401A_WR(TX_ADDRESS_1);
nrf2401A_WR(TX_ADDRESS_0);
}
//дTxÊý¾Ý
void nrf2401A_SetData()
{uchar i;
for (i=0;i<SIZE;i++)
nrf2401A_WR(TxBuf);
}
//·¢ËÍTxBufÖеÄÊý¾Ý
void nrf2401A_SendData()
{
nrf2401A_TxOn();
SET(PORTA,CE);
nrf2401A_SetTxAddr();//д·¢Ë͵ØÖ·
nrf2401A_SetData();//дÊý¾Ý
CLR(PORTA,CE);//Æô¶¯·¢ËÍ
//·¢ËÍÍê±Ïºó½øÈëStandBy
}
//¶Á³ö½ÓÊÕµ½µÄÊý¾Ý
void nrf2401A_ReadData()
{uchar i;
for (i=0;i<SIZE;i++)
{
RxBuf=nrf2401A_RD();
}
}
再传个资料试试:
PTR4000模块PDF:
点击此处打开armok01113449.pdf
nrf2401A英文PDF:
点击此处打开armok01113450.pdf |
|