?? nrf2401.c
字號:
/*******************************************
nRF2401發(fā)送程序
*******************************************/
#include <AT89X51.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define nop _nop_()
#define nop5() _nop_(); _nop_(); _nop_(); _nop_(); _nop_() //延時5us
#define DATA P1_0
#define CLK P1_1
#define CS P1_2
#define CE P1_3
#define DIR P1_4 //控制74lvc4245數(shù)據(jù)流向 DIR=1:5V->3V
#define LED P1_5 //通訊
uchar Addr_r[4]={0x00,0x00,0x00,0x11}; // 地址寄存器
uchar AddrByte_cnt=4; // 地址Byte數(shù)
uchar code InitData[15]= // nRF2401 控制字 15 Byte
{ 0x00, // 通道二接收數(shù)據(jù)寬度(1Byte)(112->119) --不用
0x08, // 通道一接收數(shù)據(jù)寬度(1Byte)(104->111) --8位
0x00, // 通道二地址(A24) (5Byte) ( 64->103) --不用
0x00, // 通道二地址(A23)
0x00, // 通道二地址(A22)
0x00, // 通道二地址(A21)
0x00, // 通道二地址(A20)
0x00, // 通道一地址(A14) (5Byte) ( 24->63 ) --地址碼:0x00 cccc cccc
0x00, // 通道一地址(A13)
0x00, // 通道一地址(A12)
0x00, // 通道一地址(A11)
0x00, // 通道一地址(A10)
0x83, // b7~b2:接收地址寬度,和發(fā)送端一致,不超過40bit ( 18->23 ) --地址碼長度:32
// b1~b0:CRC模式選擇(01-8bit;11-16bit;00/10-no) ( 16->17 ) --CRC使能,CRC16
0x4F, // b15:接收通道選擇(0-通道1;1-通道1和2) ( 15 ) --單頻道
// b14:收發(fā)模式(0-Direct模式,1-ShockBurst模式) ( 14 ) --突發(fā)模式
// b13:波特率選擇(0-250Kbps;1-1Mbps) ( 13 ) --250Kbps
// b12~b10:晶振選擇(000-4M;001-8M;010-12M;011-16M) ( 10->12 ) --16M
// b9~b8:發(fā)射功率(11-0db) ( 8 -> 9 ) --0db
0x04 // b7~b1:發(fā)射頻率配置 ( 1 -> 7 ) --2402M
// b0:收發(fā)控制(0-發(fā)送;1-接收) ( 0 ) --發(fā)送狀態(tài)
};
/*******************************************
函數(shù)聲明
*******************************************/
void Delay_ms(uchar n);
void nRF2401_init(void);
void nRF2401_WriteByte(uchar b);
void nRF2401_SendByte(uchar d);
/************************************************************
延時子程序
*************************************************************/
void main(void)
{
uchar a[10]={1,12,23,34,45,56,67,78,89,90};
uchar m,n;
uint k;
LED=1;
DIR=1; //控制74lvc4245數(shù)據(jù)流向 DIR=1:5V->3V
nRF2401_init();
while(1)
for(m=0;m<10;m++)
{
LED=0;
nRF2401_SendByte(a[m]); //發(fā)送地址和數(shù)據(jù)
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
LED=1;
for(k=0;k<5000;k++);
for(k=0;k<5000;k++);
}
}
/************************************************************
延時子程序
*************************************************************/
void Delay_ms(uchar n)
{ uchar i;
while(n--)
for(i = 0; i<122; i++) //延時n ms
{ ; }
}
/************************************************************
語法格式: void nRF2401_init(void)
實現(xiàn)功能: 初始化nRF2401
參數(shù): 無
返回值: 無
*************************************************************/
void nRF2401_init(void)
{
uchar i;
CLK=0; //時鐘線初始化
CE=0;
CS=1; //置配置模式
nop5();
nop5(); //延時10us
for(i=0;i<15;i++)
nRF2401_WriteByte(InitData[i]);
nop5();
CS=0;
CE=1; //置發(fā)送模式
}
/************************************************************
語法格式: void nRF2401_WriteByte(unsigned int b)
實現(xiàn)功能: 向nRF2401A寫入一個字節(jié)數(shù)據(jù)
參數(shù): uchar b: 待寫入數(shù)據(jù)
返回值: 無
*************************************************************/
void nRF2401_WriteByte(uchar b)
{
uchar i; // 發(fā)送數(shù)據(jù)為一個字節(jié)(8位)
for(i=0;i<8;i++)
{ if ((b<<i)&0x80) DATA = 1; //先發(fā)送高位
else DATA = 0;
nop; //延時1us
CLK=1; //上升沿有效
nop;
nop;
CLK=0;
nop;
}
}
/************************************************************
語法格式: void nRF2401_SendByte(uchar d)
實現(xiàn)功能: 將目標地址+字節(jié)數(shù)據(jù)寫入nRF2401
參數(shù): uchar d: 待發(fā)送字節(jié)數(shù)據(jù)
返回值: 無
*************************************************************/
void nRF2401_SendByte(uchar d)
{
uchar i;
CS=0;
CE=1;
nop5();
nop5();
for(i=0; i<AddrByte_cnt; i++) //AddrByte_cnt為主機地址字節(jié)數(shù)
nRF2401_WriteByte(Addr_r[i]); //主機地址
nRF2401_WriteByte(d); //d為待發(fā)數(shù)據(jù)
nop5();
CE=0; //CE置低,激活nRF2401發(fā)送數(shù)據(jù)d
Delay_ms(1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -