智能车制作
标题:
nrf905 接受程序 单片机80C51
[打印本页]
作者:
数据库——嘎嘎
时间:
2012-9-2 19:43
标题:
nrf905 接受程序 单片机80C51
#include <reg52.h>
#include <ABSACC.h>
#include <intrins.h>
#include <stdio.h>
#define uint unsigned int //0 ~ 255
#define uchar unsigned char
//------------------------------------------------------------------------------------------------------------------
#define BYTE_BIT0 0x01
#define BYTE_BIT1 0x02
#define BYTE_BIT2 0x04
#define BYTE_BIT3 0x08
#define BYTE_BIT4 0x10
#define BYTE_BIT5 0x20
#define BYTE_BIT6 0x40
#define BYTE_BIT7 0x80
bdata unsigned char DATA_BUF;//bdata ---> 可位寻址的片内ram
#define DATA7 ((DATA_BUF&BYTE_BIT7) != 0)
#define DATA0 ((DATA_BUF&BYTE_BIT0) != 0)
//sbit flag =DATA_BUF^7;
//sbit flag1 =DATA_BUF^0;
//---------------------------------------------------------------------------------------------------------------
#define TxRxBuf_Len 4
unsigned char TxRxBuffer[TxRxBuf_Len];
//----------------------------------------------NRF905控制IO------------------------------------------------------
sbit TXEN=P2^4;
sbit TRX_CE=P0^0;
sbit PWR=P2^5;
//----------------------------------------------NRF905 SPI接口---------------------------------------------------
sbit MISO=P0^3;
sbit MOSI=P0^6;
sbit SCK=P0^4;
sbit CSN=P0^5;
//----------------------------------------nrf905状态标志---------------------------------------------------------
sbit AM=P0^2;
sbit DR=P2^7;
sbit CD=P2^6;
//---------------------------------------------------------------------------------------------------------------
sbit led=P1^0;
//-----------------------------------------------NRF905寄存器配置-----------------------------------------------
unsigned char idata RFConf[11]=
{
0x00, //配置命令//
0x4c, //CH_NO,配置频段在430MHZ
0x0c, //输出功率为10db,不重发,节电为正常模式
0x44, //地址宽度设置,为4字节
0x04,0x04, //接收发送有效数据长度为32字节
0xCC,0xCC,0xCC,0xCC, //接收地址
0x58, //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振
};
//---------------------------------------------------NRF905控制命令------------------------------------------------------
#define WC 0x00
#define RC 0x10
#define WTP 0x20
#define RTP 0x21
#define WTA 0x22
#define RTA 0x23
#define RRP 0x24
//---------------------------------------------------------------延时----------------------------------------
void Delay(uchar n)
{
uint k;
while(n--)
for(k=0;k<8;k++);
}
//-------------------------------------------------------NRF905 SPI读函数-------------------------------------------------
unsigned char SpiRead(void)
{
unsigned char j;
for (j=0;j<8;j++)
{
DATA_BUF=DATA_BUF<<1;
SCK=1;
if (MISO) //读取最高位,保存至最末尾,通过左移位完成整个字节
{
DATA_BUF|=BYTE_BIT0;
}
else
{
DATA_BUF&=~BYTE_BIT0;
}
SCK=0;
}
return DATA_BUF;
}
//-------------------------------------------------------NRF905 SPI写函数--------------------------------------------------------
void SpiWrite(unsigned char send)
{
unsigned char i;
DATA_BUF=send;
for (i=0;i<8;i++)
{
if (DATA7) //总是发送最高位
{
MOSI=1;
}
else
{
MOSI=0;
}
SCK=1;
DATA_BUF=DATA_BUF<<1;
SCK=0;
}
}
//------------------------------------------初始化nRF905---------------------------------------------------------
void nRF905Init(void)
{
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
DR=1; // Init DR for input
AM=1; // Init AM for input
CD=1; // Init CD for input
PWR=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TXEN=0; // set radio in Rx mode
}
//------------------------------------------初始化寄存器---------------------------------------------------------
void Config905(void)
{
uchar i;
CSN=0; // Spi enable for write a spi command
for (i=0;i<11;i++) // Write configration words 写放配置字
{
SpiWrite(RFConf[i]);
}
CSN=1; // Disable Spi
}
//--------------------------------------------------设置接收状态-----------------------------------------------------
void SetRxMode(void)
{
TXEN=0;
TRX_CE=1;
Delay(1); // delay for mode change(>=650us)
}
//---------------------------------------------------接收状态判断--------------------------------------------------
unsigned char CheckDR(void) //检查是否有新数据传入 Data Ready
{
if (AM==1&&CD==1&&DR==1)
{
return 1;
}
else
{
return 0;
}
}
//-----------------------------------------------------读取接收缓冲区数据----------------------------------------------
void RxPacket(void) //读数据
{
uchar i;
Delay(1);
// TRX_CE=0; // Set nRF905 in standby mode
Delay(100);
TRX_CE=0;
CSN=0; // Spi enable for write a spi command
Delay(1);
SpiWrite(RRP);
for (i = 0 ;i < 4 ;i++)
{
TxRxBuffer[i]=SpiRead(); // Read data and save to buffer
}
CSN=1;
Delay(10);
TRX_CE=1;
}
//-------------------------------------------------------接收数据------------------------------------------------
void RX(void)
{
SetRxMode(); // Set nRF905 in Rx mode
led=0;
while (CheckDR()==0);
RxPacket();
while(AM==1||DR==1);
}
void ini_uart()
{
TMOD=0x20; //T1方式2,用于UART波特率
TH1=0xFD;
TL1=0xFD; //UART波特率设置:9600
TR1=1;
SCON=0x50; //UART方式1:8位UART; REN=1:允许接收
PCON=0x00; //SMOD=0:波特率不加倍
TI = 0;
RI = 0;
EA = 1;
ES=0;
}
//------------------------------------------------------主函数---------------------------------------------------
void main(void)
{ uchar j;
nRF905Init();
Config905();
ini_uart();
while(1)
{
RX();
Delay(10);
CSN=0;
led=0;
Delay(5000);
led=1;
Delay(5000);
for (j=0;j<4;j++)
{
SBUF = TxRxBuffer[j];
while (TI == 0);
TI = 0;
}
CSN=1;
Delay(1);
}
}
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2