发送部分:
//此程序为nRF905的发送程序
#include <reg52h>
#include <intrinsh>
#define uchar unsigned char
#define uint unsigned int
//定义命令字
#define WC 0x00 // Write configuration register command
#define RC 0x10 // Read configuration register command
#define WTP 0x20 // Write TX Payload command
#define RTP 0x21 // Read TX Payload command
#define WTA 0x22 // Write TX Address command
#define RTA 0x23
//管脚配置
sbit MOSI=P2^0;
sbit CSN=P2^3;
sbit SCK=P2^6;
sbit MISO=P2^7;
sbit TRX_CE=P0^5;
sbit TXEN=P0^3;
sbit PWR=P0^2;
sbit DR=P0^0;
uchar Txbuf[4]={0xf9,0xa4,0xb0,0x99};
uchar Rfconfig[10]={0x4c,0x0e,0x44,0x04,0x04,0xe7,0xe7,0xe7,0xe7,0xde};
//------------------------------------------------
void delay(uint x)
{
uint i;
for(i=0;i<x;i++)
{
_nop_();
}
}
//------------------------------------------------
void Spiwrite(uchar dat)
{
uchar i=8;
while(i--)
{
delay(10);
SCK=0;
MOSI=(bit)(dat&0x80);
dat<<=1;
delay(10);
SCK=1;
delay(10);
SCK=0;
}
SCK=0;
}
//------------------------------------------------
void Txpacket(void)
{
TXEN=1;
CSN=0;
Spiwrite(0x22);
Spiwrite(0xe7);
Spiwrite(0xe7);
Spiwrite(0xe7);
Spiwrite(0xe7);
CSN=1;
_nop_();_nop_();
CSN=0;
Spiwrite(0x20);
Spiwrite(Txbuf[0]);
Spiwrite(Txbuf[1]);
Spiwrite(Txbuf[2]);
Spiwrite(Txbuf[3]);
CSN=1;
_nop_();_nop_();
TRX_CE=1;
delay(50);
// TRX_CE=0;
while(!DR);
}
//--------------------------------------------------
void ini_system(void)
{
uchar i;
CSN=1;
SCK=0;
PWR=1; // PWR_UP | TRX_CE |TXEN | M O D E
TRX_CE=0; // 1 | 0 | 0 | SPI Programming
TXEN=0;
_nop_();
CSN=0;
Spiwrite(0x00);
for(i=0;i<10;i++)
{
Spiwrite(Rfconfig[i]);
}
CSN=1;
}
//-------------------------------------------------
void setmode()
{
PWR=1; // PWR_UP TRX_CE TXEN MODE
TRX_CE=1; // 1 1 1 SHOCKBURST TX
TXEN=1;
delay(1000); //time must be >=650us
}
//-----------------------------------------------------
void main()
{
ini_system();
while(1)
{
setmode();
Txpacket();
}
}
接收程序:
/接收时用数码管显示接收到的数据/
#include <reg52h>
#include <intrinsh>
#define uchar unsigned char
#define uint unsigned int
#define led P0
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
sbit TXEN=P3^5;
sbit TRX_CE=P3^4;
sbit PWR=P3^6;
sbit MISO=P3^0;
sbit MOSI=P1^2;
sbit SCK=P1^4;
sbit CSN=P1^3;
sbit DR=P1^1;
sbit CD=P1^0;
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
sbit led1=P2^3;
sbit led2=P2^5;
sbit led3=P2^6;
sbit led4=P2^7;
unsigned char Rxbuf[4]={0xf9};
unsigned char Rfconfig[10]={0x4c,0x0e,0x44,0x04,0x04,0xe7,0xe7,0xe7,0xe7,0xde};
void delay(uint x)
{
uint i;
for(i=0;i<x;i++)
{
_nop_();
}
}
void Spiwrite(uchar dat)
{
uchar i=8;
while(i--)
{
delay(10);
SCK=0;
MOSI=(bit)(dat&0x80);
dat<<=1;
delay(10);
SCK=1;
delay(10);
SCK=0;
}
SCK=0;
}
unsigned char Spiread(void)
{
uchar i=8;
uchar ddat;
while(i--)
{
ddat<<=1;
SCK=0;
_nop_();
_nop_();
ddat|=MISO;
SCK=1;
_nop_();_nop_();
}
SCK=0;
return ddat;
}
void Rxpacket(void)
{
unsigned char j=0;
TRX_CE=0;
PWR=1;
CSN=0;
Spiwrite(RRP);
for(j=0;j<4;j++)
{
Rxbuf[j]=Spiread();
}
CSN=1;
}
void ini_system(void)
{
uchar i;
CSN=1;
SCK=0;
PWR=1; // PWR_UP | TRX_CE |TXEN | M O D E
TRX_CE=0; // 1 | 0 | 0 | SPI Programming
TXEN=0;
_nop_();
CSN=0;
Spiwrite(WC);
for(i=0;i<10;i++)
{
Spiwrite(Rfconfig[i]);
}
CSN=1;
}
void setmode()
{
PWR=1; // PWR_UP TRX_CE TXEN MODE
TRX_CE=1; // 1 1 0 SHOCKBURST RX
TXEN=0;
delay(300); //time must be >=650us
}
void display()
{
led1=0;
led2=1;
led3=1;
led4=1;
led=Rxbuf[0];
delay(1);
led1=1;
led2=0;
led3=1;
led4=1;
led=Rxbuf[1];
delay(1);
led1=1;
led2=1;
led3=0;
led4=1;
led=Rxbuf[2];
delay(1);
led1=1;
led2=1;
led3=1;
led4=0;
led=Rxbuf[3];
delay(1);
}
void main()
{
ini_system();
setmode();
while(1)
{
Rxpacket();
while(!DR);
display();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)