|
//============头文件nRF905.h============//
/********************************************************
#define PWR_UP PORTB_PB7 //out
#define TX_EN PORTB_PB6 //out
#define TRX_CE PORTB_PB5 //out
#define CSN PORTB_PB4 //out
#define CD PORTB_PB3 //in
#define DR PORTB_PB2 //in
#define AM PORTB_PB1 //in
------------------------------------------------
*********************************************************/
#ifndef _NRF905_H
#define _NRF905_H
#include "derivative.h" /* derivative-specific definitions */
#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 // Read TX Address command
#define RRP 0x24 // Read RX Payload command
/*
#define AM PORTA_PA0 //in
#define DR PORTA_PA1 //in
#define MISO PORTA_PA2 //in
#define CD PORTA_PA3 //in
*/
#define AM PTJ_PTJ0 //in
#define DR PTJ_PTJ1 //in
#define MISO PTJ_PTJ6 //in
#define CD PTJ_PTJ7 //in
///*
#define PWR_UP PORTB_PB7 //out
#define TX_EN PORTB_PB6 //out
#define TRX_CE PORTB_PB2 //out
#define CSN PORTB_PB3 //out
#define MOSI PORTB_PB4 //out
#define SCK PORTB_PB5 //out
//*/
extern volatile char nRF905TxBuf[32],nRF905RxBuf[32];
void nRF905_Interrupt(void);
void SetTxMode(void);
void SetRxMode(void);
void RxPacket(void);
void TxPacket(void);
void Config905(void);
void nRF905_Delay_100us(byte ms);
void nRF905_Delay_ms(int ms);
void nRF905_Interrupt(void);
#endif
//==========C文件===============//
#include "nRF905.h"
#include "MC9S12XS128.h"
volatile char nRF905TxBuf[32],nRF905RxBuf[32];
typedef struct RFConfig
{
byte n;
byte buf[10];
}RFConfig;
RFConfig RxTxConf =
{
10,
0x01, 0x0c, 0x44, 0x20, 0x20, 0xcc, 0xcc, 0xcc,0xcc, 0x58 // 433Mhz
//0x4C, 0x0e, 0x44, 0x20, 0x20, 0xcc, 0xcc, 0xcc,0xcc, 0x58 // 868/915MHz
};
byte ReceiveAddress[4]={0xcc, 0xcc, 0xcc,0xcc};
void nRF905_Interrupt(void) {
//PTJ_PTJ1中断
PPSJ_PPSJ1=1;
PIEJ_PIEJ1=1;
}
//延时约100微秒
void nRF905_Delay_100us(byte ms)
{
int ii,jj;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<540;jj++); //busclk:64MHz--100us
}
//-----------------------------------------------------
//延时约1毫秒
void nRF905_Delay_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
ms=4*ms;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<1335;jj++); //busclk:64MHz--1ms
}
//======================================================
//function SpiWrite();
//======================================================
//======================================================
//function SpiWrite();
//======================================================
void SPIWrite(byte tmch)
{
byte i;
for(i=0;i<8;i++) // Setup byte chirculation bits
{
if(tmch&0X80) // Put DATA_BUF.7 on data line
MOSI=1;
else
MOSI=0;
SCK=1; // Set clock line high
tmch <<= 1; // Shift DATA_BUF
SCK=0; // Set clock line low
}
}
//======================================================
// function SpiRead();
//======================================================
byte SPIRead(void)
{
byte i,dat=0;
for (i=0;i<8;i++) // Setup byte circulation bits
{
dat <<=1 ; // Right shift DATA_BUF
SCK=1; // Set clock line high
if(MISO)
dat |= 0x01; // Read data
SCK=0; // Set clock line low
}
return dat; // Return function parameter
}
//======================================================
//function Config905();
//开发板IO及模块寄存器配置
//======================================================
void Config905(void)
{
byte i;
//DDRA=0XF0;
DDRB=0XFF;
DDRJ=0x00;
DR=1; // Init DR for input
AM=1; // Init AM for input
PWR_UP=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TX_EN=0; // set radio in Rx mode
CSN=0; // Spi enable for write a spi command
SPIWrite(WC); // Write config command写放配置命令
for (i=0;i<RxTxConf.n;i++) // Write configration words 写放配置字
{
SPIWrite(RxTxConf.buf);
}
CSN=1; // Disable Spi
}
//======================================================
//function TxPacket();
//======================================================
void TxPacket(void)
{
byte i;
CSN=0; // Spi enable for write a spi command
SPIWrite(WTP); // Write payload command
for (i=0;i<32;i++)
{
SPIWrite(nRF905TxBuf); // Write 32 bytes Tx data
}
CSN=1; // Spi disable
nRF905_Delay_100us(1);
CSN=0; // Spi enable for write a spi command
SPIWrite(WTA); // Write address command
for (i=0;i<4;i++) // Write 4 bytes address
{
SPIWrite(ReceiveAddress);
}
CSN=1; // Spi disable
//TX_EN=1; 是否需要置1?
TRX_CE=1; // Set TRX_CE high,start Tx data transmission
nRF905_Delay_100us(1); // while (DR!=1);
TRX_CE=0; // Set TRX_CE low
}
//======================================================
//function RxPacket();
//======================================================
void RxPacket(void)
{
byte i;
TRX_CE=0; // Set nRF905 in standby mode
CSN=0; // Spi enable for write a spi command
SPIWrite(RRP); // Read payload command
for (i=0;i<32;i++)
{
nRF905RxBuf=SPIRead(); // Read data and save to buffer
}
CSN=1; // Disable spi
while(DR||AM);
TRX_CE=1;
}
//======================================================
//function SetTxMode();
//======================================================
void SetTxMode(void)
{
TX_EN=1;
TRX_CE=1;
nRF905_Delay_ms(1); // nRF905_Delay_100us for mode change(>=650us)
}
//======================================================
//function SetRxMode();
//======================================================
void SetRxMode(void)
{
TX_EN=0;
TRX_CE=1;
nRF905_Delay_ms(1); // nRF905_Delay_100us for mode change(>=650us)
}
点击此处下载 ourdev_716697LE9KZA.rar(文件大小:2K) (原文件名:nRF905.rar) |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|