📄 main.c
字号:
#include "config.h"
#include <stdio.h>
#include <string.h>
//-----------------------------------------------
// nrf905命令控制字
//------------------------------------------------
#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
//-----------------------------------------------
// nrf905模式定义
//-----------------------------------------------
#define PowerDown 1
#define Standby 2
#define RDMode 3
#define RXMode 4
#define TXMode 5
#define TXData 6
//-------------------------------------------------------------------------------------------
//nrf905配置寄存器宏定义
// nrf905配置寄存器(10Byte)
//工作频率f=(422.4+CH_NO/10)*(1+HFREQ_PLL)MHz
//-------------------------------------------------------------------------------------------/
//--------------------------------------------------------------------------------------------/
unsigned long RX_ADDRESS = 0x50515253 ; // 接收有效地址(本方)
unsigned long RX_ADDRESSB = 0xAA515255 ;
#define CH_NO_FREQ_422_4MHz 0x0000 //工作频率422.4MHz(433MHz频段最低频率)
#define CH_NO_FREQ_422_5MHz 0x0001 //工作频率422.5MHz
#define CH_NO_FREQ_425_0MHz 0x001a //工作频率425.0MHz
#define CH_NO_FREQ_427_5MHz 0x0033 //工作频率427.5MHz
#define CH_NO_FREQ_430_0MHz 0x004c //工作频率430.0MHz
#define CH_NO_FREQ_433_0MHz 0x006a //工作频率433.0MHz(433MHz频段基准频率)
#define CH_NO_FREQ_433_1MHz 0x006b //工作频率433.1MHz
#define CH_NO_FREQ_433_2MHz 0x006c //工作频率433.2MHz
#define CH_NO_FREQ_434_7MHz 0x007b //工作频率434.7MHz
#define CH_NO_FREQ_473_5MHz 0x01ff //工作频率473.5MHz(433MHz频段最高频率)
#define CH_NO_FREQ_844_8MHz 0x0000 //工作频率844.8MHz(868MHz频段最低频率)
#define CH_NO_FREQ_862_0MHz 0x0056 //工作频率862.0MHz
#define CH_NO_FREQ_868_0MHz 0x0074 //工作频率868.0MHz(868MHz频段基准频率)
#define CH_NO_FREQ_868_2MHz 0x0075 //工作频率868.2MHz
#define CH_NO_FREQ_868_4MHz 0x0076 //工作频率868.4MHz
#define CH_NO_FREQ_869_8MHz 0x007d //工作频率869.8MHz
#define CH_NO_FREQ_895_8MHz 0x00ff //工作频率895.8MHz
#define CH_NO_FREQ_896_0MHz 0x0100 //工作频率896.0MHz
#define CH_NO_FREQ_900_0MHz 0x0114 //工作频率900.0MHz
#define CH_NO_FREQ_902_2MHz 0x011f //工作频率902.2MHz
#define CH_NO_FREQ_902_4MHz 0x0120 //工作频率902.4MHz
#define CH_NO_FREQ_915_0MHz 0x015f //工作频率915.0MHz(915MHz频段基准频率)
#define CH_NO_FREQ_927_8MHz 0x019f //工作频率927.8MHz
#define CH_NO_FREQ_947_0MHz 0x01ff //工作频率947.0MHz(915MHz频段最高频率)
#define CH_NO_FREQ CH_NO_FREQ_433_0MHz //工作频率433.0MHz
#define CH_NO_BYTE CH_NO_FREQ & 0xff //工作频率低8位 Byte0 01101100
#define AUTO_RETRAN 0x20 //重发数据包 Byte1.5 00100000
#define RX_RED_PWR 0x10 //接收低功耗模式 Byte1.4 00010000
#define PA_PWR__10dBm 0x00 //输出功率-10dBm Byte1.3~2 00000000
#define PA_PWR_2dBm 0x04 //输出功率+2dBm Byte1.3~2 00000100
#define PA_PWR_6dBm 0x08 //输出功率+6dBm Byte1.3~2 00001000
#define PA_PWR_10dBm 0x0c //输出功率+10dBm Byte1.3~2 00001100
#define HFREQ_PLL_433MHz 0x00 //工作在433MHz频段 Byte1.1 00000000
#define HFREQ_PLL_868MHz 0x02 //工作在868MHz频段 Byte1.1 00000010
#define HFREQ_PLL_915MHz 0x02 //工作在915MHz频段 Byte1.1 00000010
#define CH_NO_BIT8 CH_NO_FREQ >> 8 //工作频率第9位 Byte1.0 0
#define TX_AFW_1BYTE 1 * 16 //发送地址宽度1字节 Byte2.7~4
#define TX_AFW_2BYTE 2 * 16 //发送地址宽度2字节 Byte2.7~4
#define TX_AFW_3BYTE 3 * 16 //发送地址宽度3字节 Byte2.7~4
#define TX_AFW_4BYTE 4 * 16 //发送地址宽度4字节 Byte2.7~4 100
#define RX_AFW_1BYTE 1 //接收地址宽度1字节 Byte2.3~0
#define RX_AFW_2BYTE 2 //接收地址宽度2字节 Byte2.3~0
#define RX_AFW_3BYTE 3 //接收地址宽度3字节 Byte2.3~0
#define RX_AFW_4BYTE 4 //接收地址宽度4字节 Byte2.3~0 100
#define RX_PW_1BYTE 1 //接收数据宽度1字节 Byte3.5~0
#define RX_PW_16BYTE 16 //接收数据宽度16字节 Byte3.5~0
#define RX_PW_32BYTE 32 //接收数据宽度32字节 Byte3.5~0 00100000
#define TX_PW_1BYTE 1 //发送数据宽度1字节 Byte4.5~0
#define TX_PW_16BYTE 16 //发送数据宽度16字节 Byte4.5~0
#define TX_PW_32BYTE 32 //发送数据宽度32字节 Byte4.5~0 00100000
#define RX_ADDRESS_0 RX_ADDRESS >> 24 //接收有效地址第1字节 Byte5 11100111
#define RX_ADDRESS_1 (RX_ADDRESS >> 16) & 0xff //接收有效地址第2字节 Byte6 11100111
#define RX_ADDRESS_2 (RX_ADDRESS >> 8) & 0xff //接收有效地址第3字节 Byte7 11100111
#define RX_ADDRESS_3 RX_ADDRESS & 0xff //接收有效地址第4字节 Byte8 11100111
#define RX_ADDRESSB_0 RX_ADDRESSB >> 24 //接收有效地址第1字节 Byte5 11100111
#define RX_ADDRESSB_1 (RX_ADDRESSB >> 16) & 0xff //接收有效地址第2字节 Byte6 11100111
#define RX_ADDRESSB_2 (RX_ADDRESSB >> 8) & 0xff //接收有效地址第3字节 Byte7 11100111
#define RX_ADDRESSB_3 RX_ADDRESSB & 0xff
#define CRC_MODE_16BIT 0x80 //CRC16模式 Byte9.7 1
#define CRC_MODE_8BIT 0x00 //CRC8模式 Byte9.7
#define CRC_EN 0x40 //CRC使能 Byte9.6 1
#define CRC16_EN 0xc0 //CRC16模式使能 Byte9.7~6 11
#define CRC8_EN 0x40 //CRC8模式使能 Byte9.7~6
#define XOF_20MHz 0x20 //晶体振荡器频率20MHz Byte9.5~3
#define XOF_16MHz 0x18 //晶体振荡器频率16MHz Byte9.5~3 100
#define XOF_12MHz 0x10 //晶体振荡器频率12MHz Byte9.5~3
#define XOF_8MHz 0x08 //晶体振荡器频率8MHz Byte9.5~3
#define XOF_4MHz 0x00 //晶体振荡器频率4MHz Byte9.5~3
#define UP_CLK_EN 0x40 //输出时钟使能 Byte9.2 1
#define UP_CLK_FREQ_500kHz 0x03 //输出时钟频率500kHz Byte9.1~0 11
#define UP_CLK_FREQ_1MHz 0x02 //输出时钟频率1MHz Byte9.1~0
#define UP_CLK_FREQ_2MHz 0x01 //输出时钟频率2MHz Byte9.1~0
#define UP_CLK_FREQ_4MHz 0x00 //输出时钟频率4MHz Byte9.1~0
#define UP_CLK_EN_500kHz 0x43 //输出时钟频率500kHz Byte9.2~0 111
#define UP_CLK_EN_1MHz 0x42 //输出时钟频率1MHz Byte9.2~0
#define UP_CLK_EN_2MHz 0x41 //输出时钟频率2MHz Byte9.2~0
#define UP_CLK_EN_4MHz 0x40 //输出时钟频率4MHz Byte9.2~0
//RF905(发送)的控制断口
const uint32 RF905_CSN0 = 1 << 21;
const uint32 RF905_SCLH0 = 1 << 20;
const uint32 RF905_DOUT0 = 1 << 19;
const uint32 RF905_DIN0 = 1 << 18;
const uint32 RF905_DRDY0 = 1 << 17;
const uint32 RF905_TX_EN0 = 1 << 21;
const uint32 RF905_TRX_EN0= 1 << 22;
const uint32 RF905_PWR0 = 1 << 23;
const uint32 RF905_CD0= 1 << 24;
const uint32 RF905_AM0 = 1 << 25;
#define RF905_ACSNH IO0SET = RF905_CSN0
#define RF905_ACSNL IO0CLR = RF905_CSN0
#define RF905_ADOUTH IO0SET = RF905_DOUT0
#define RF905_ADOUTL IO0CLR = RF905_DOUT0
#define RF905_ASCLKH IO0SET = RF905_SCLH0
#define RF905_ASCLKL IO0CLR = RF905_SCLH0
#define RF905_ATX_ENH IO1SET = RF905_TX_EN0
#define RF905_ATX_ENL IO1CLR = RF905_TX_EN0
#define RF905_ATRX_ENH IO1SET = RF905_TRX_EN0
#define RF905_ATRX_ENL IO1CLR = RF905_TRX_EN0
#define RF905_APWRH IO1SET = RF905_PWR0
#define RF905_APWRL IO1CLR = RF905_PWR0
#define OUTRF905_ACSN IO0DIR = IO0DIR | RF905_CSN0
#define OUTRF905_ADOUT IO0DIR = IO0DIR | RF905_DOUT0
#define OUTRF905_ASCLK IO0DIR = IO0DIR | RF905_SCLH0
#define OUTRF905_ATX_EN IO1DIR = IO1DIR | RF905_TX_EN0
#define OUTRF905_ATRX_EN IO1DIR = IO1DIR | RF905_TRX_EN0
#define OUTRF905_APWR IO1DIR = IO1DIR | RF905_PWR0
#define INRF905_ADRDY IO0DIR = IO0DIR & ~ RF905_DRDY0
#define JUDGE_RF905_ADRDY (IO0PIN & RF905_DRDY0)
#define INRF905_ADIN IO0DIR = IO0DIR & ~ RF905_DIN0
#define JUDGE_RF905_ADIN (IO0PIN & RF905_DIN0)
#define INRF905_ACD IO1DIR = IO1DIR & ~ RF905_CD0
#define JUDGE_RF905_ACD (IO1PIN & RF905_CD0)
#define INRF905_AAM IO1DIR = IO1DIR & ~ RF905_AM0
#define JUDGE_RF905_AAM (IO1PIN & RF905_AM0)
#define CSN=0 RF905_ACSNL
void _delay_loop_(unsigned int val)
{
unsigned int i;
for(i = 0; i < (val * 60); i ++);
}
void Delayus(uint16 xus)
{
uint16 i = xus;
while(i--);
}
void ChangeModeA(uint8 num)
{
if(num==PowerDown)
{
RF905_APWRL; // 配置为掉电模式
_delay_loop_(1);
RF905_ATX_ENL;
_delay_loop_(1);
RF905_ATRX_ENL;
_delay_loop_(1); // delay for mode change(>=650us)
}
if(num==Standby)
{
RF905_ATRX_ENL;
_delay_loop_(1);
RF905_ATX_ENL;
_delay_loop_(1);
RF905_APWRH;
_delay_loop_(1);
}
if(num==RXMode)
{
RF905_ATX_ENL;
_delay_loop_(1);
RF905_APWRH;
Delayus(2);
_delay_loop_(1);
RF905_ATRX_ENH;
_delay_loop_(1);
}
if(num==TXMode)
{
RF905_ATX_ENH;
_delay_loop_(1);
RF905_APWRH;
_delay_loop_(1);
RF905_ATRX_ENL;
_delay_loop_(1);
}
if(num==TXData)
{
RF905_ATRX_ENH; //Set TRX_CE high,start Tx data transmission
Delayus(10);
_delay_loop_(1);
RF905_ATRX_ENL; /* ?? Make a 20us Pulse as nRF905 Datasheet */
_delay_loop_(1);
}
}
void RFSpiWriteA(unsigned char Out_data)
{
unsigned char i;
for(i = 0;i<8;i++)
{
if((Out_data << i) & 0x80)
{
RF905_ADOUTH;
}
else
{
RF905_ADOUTL;
}
RF905_ASCLKH;
Delayus(1);
RF905_ASCLKL;
}
}
unsigned char RFSpiReadA(void)
{
unsigned char i;
unsigned char dd;
dd=0;
for(i=0;i<8;i++)
{
dd<<=1;
RF905_ASCLKH;
if(JUDGE_RF905_ADIN)
{
dd|=0x00000001;
}
else
{
dd&=0xFFFFFFFE;
}
RF905_ASCLKL;
}
return(dd);
}
void Config905A(void) //配置nRF905
{
RF905_ACSNL; //Spi enable for write a spi command
RFSpiWriteA(WC); // Write config command
RFSpiWriteA(CH_NO_BYTE); //中心频率低8位
RFSpiWriteA(PA_PWR_10dBm | HFREQ_PLL_433MHz); //发射+10dBm,发射频率433MHz,中心频率第9位=0
RFSpiWriteA(TX_AFW_4BYTE | RX_AFW_4BYTE); //接收地址宽度4字节,发送地址宽度4字节
RFSpiWriteA(RX_PW_32BYTE); //接收数据宽度32字节
RFSpiWriteA(TX_PW_32BYTE); //发送数据宽度32字节
RFSpiWriteA(RX_ADDRESS_0); //接收有效地址第1字节
RFSpiWriteA(RX_ADDRESS_1); //接收有效地址第2字节
RFSpiWriteA(RX_ADDRESS_2); //接收有效地址第3字节
RFSpiWriteA(RX_ADDRESS_3); //接收有效地址第4字节
RFSpiWriteA(CRC16_EN | XOF_16MHz); //CRC16模式使能,晶体振荡器频率16MHz
RF905_ACSNH; // Disable Spi
}
void RF905_ReadA(uint8 RFcmd,uint8 *RFdata)
{
uint8 i,len;
if(RFcmd==WTA|RFcmd==RTA) len=4;
else if(RFcmd==WC|RFcmd==RC) len=10;
else if(RFcmd==WTP|RFcmd==RTP|RFcmd==RRP) len=32;
else len=0;
RF905_ACSNL; // Spi enable for write a spi command
RFSpiWriteA(RFcmd);
for(i=0;i<len;i++) RFdata[i]=RFSpiReadA();
RF905_ACSNH;
}
void RF905_WriteA(uint8 RFcmd,uint8 *RFdata)
{
uint8 i,len;
if(RFcmd==WTA|RFcmd==RTA) len=4;
else if(RFcmd==WC|RFcmd==RC) len=10;
else if(RFcmd==WTP|RFcmd==RTP|RFcmd==RRP) len=32;
else len=0;
RF905_ACSNL; // Spi enable for write a spi command
RFSpiWriteA(RFcmd);
for(i=0;i<len;i++) RFSpiWriteA(RFdata[i]); // len Bytes Data
RF905_ACSNH; // Disable Spi
}
#define cishu_n 3
uint8 Databuf[32]; // 16字节数据存储缓冲区
uint8 TX_ADDRESS[4]={0xAA,0x51,0x52,0x55};
//uint8 TX_ADDRESS[4]={0x50,0x51,0x52,0x53};
void TxPacketA(void)
{
ChangeModeA(TXMode);
RF905_WriteA(WTP,Databuf); // Spi disable
Delayus(1);
RF905_WriteA(WTA,TX_ADDRESS); // Spi disable
ChangeModeA(TXData);
RF905_APWRH; // Set TRX_CE low
ChangeModeA(RXMode);
}
void RxPacketA(void)
{ //uint i;
RF905_ATRX_ENL; // Set nRF905 in standby mode
RF905_ReadA(RRP,Databuf);
}
///////////////////////////////////////////////////////////////////////
//RF905(发送)的控制断口
const uint32 RF905_DRDY = 1 << 14;
const uint32 RF905_DIN = 1 << 12;
const uint32 RF905_DOUT = 1 << 9;
const uint32 RF905_SCLK = 1 << 11;
const uint32 RF905_CSN = 1 << 15;
const uint32 RF905_TX_EN = 1 << 18;
const uint32 RF905_TRX_EN= 1 << 16;
const uint32 RF905_PWR = 1 << 19;
const uint32 RF905_CD= 1 << 8;
const uint32 RF905_AM = 1 << 13;
#define RF905_CSNH IO0SET = RF905_CSN
#define RF905_CSNL IO0CLR = RF905_CSN
#define RF905_DOUTH IO0SET = RF905_DOUT
#define RF905_DOUTL IO0CLR = RF905_DOUT
#define RF905_SCLKH IO0SET = RF905_SCLK
#define RF905_SCLKL IO0CLR = RF905_SCLK
#define RF905_TX_ENH IO1SET = RF905_TX_EN
#define RF905_TX_ENL IO1CLR = RF905_TX_EN
#define RF905_TRX_ENH IO0SET = RF905_TRX_EN
#define RF905_TRX_ENL IO0CLR = RF905_TRX_EN
#define RF905_PWRH IO1SET = RF905_PWR
#define RF905_PWRL IO1CLR = RF905_PWR
#define OUTRF905_CSN IO0DIR = IO0DIR | RF905_CSN
#define OUTRF905_DOUT IO0DIR = IO0DIR | RF905_DOUT
#define OUTRF905_SCLK IO0DIR = IO0DIR | RF905_SCLK
#define OUTRF905_TX_EN IO1DIR = IO1DIR | RF905_TX_EN
#define OUTRF905_TRX_EN IO0DIR = IO0DIR | RF905_TRX_EN
#define OUTRF905_PWR IO1DIR = IO1DIR | RF905_PWR
#define INRF905_DRDY IO0DIR = IO0DIR & ~ RF905_DRDY
#define JUDGE_RF905_DRDY (IO0PIN & RF905_DRDY)
#define INRF905_DIN IO0DIR = IO0DIR & ~ RF905_DIN
#define JUDGE_RF905_DIN (IO0PIN & RF905_DIN)
#define INRF905_CD IO0DIR = IO0DIR & ~ RF905_CD
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -