新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于430單片機(jī)的NRF905收發(fā)驅(qū)動(dòng)程序

基于430單片機(jī)的NRF905收發(fā)驅(qū)動(dòng)程序

作者: 時(shí)間:2016-11-27 來(lái)源:網(wǎng)絡(luò) 收藏
#include
#include "BaseType.h"
#include
#include
#define WaitKeyDw 100
#define WaitKeyUp 101
#define KeyDwTimes 30
#define WRC 0x00 //W_RF_CONFIG
#define RRC 0x10 //R_RF_CONFIG
#define WTP 0x20 //W_TX_PAYLOAD
#define RTP 0x21 //R_TX_PAYLOAD
#define WTA 0x22 //W_TX_ADDRESS
#define RTA 0x23 //R_TX_ADDRESS
#define RRP 0x24 //R_RX_PAYLOAD
#define RAD 0x40 //R_ADC_DATA
#define WAC 0x44 //W_ADC_CONFIG
#define RAC 0x46 //R_ADC_CONFIG
#define WTU 0x50 //W_TEST_UNLOCK (use with data A5)
#define WTR 0x52 //W_TEST_REGISTER
#define RTR 0x53 //R_TEST_REGISTER
#define CC 0x80 //CHANNEL_CONFIG
#define SAV 0xC0 //START_ADC_CONV
#define HFREQ 1 // 0=433MHz, 1=868/915MHz
#define POWER 3 // 0=min power...3 = max power
#define RFTxLen 16 //射頻發(fā)送綬沖區(qū)長(zhǎng)度
#define RFRxLen 16 //射頻接收綬沖區(qū)長(zhǎng)度
#define INT8U unsigned char
#define INT16U unsigned int
#define _nop_() _NOP()
INT8U Nrf9e5Config[10] = {
0x6B, //頻道設(shè)置
0x0C, //自動(dòng)重發(fā)關(guān),發(fā)送節(jié)電模式關(guān),輸出功率10dB,433.1MHZ
0x44, //收發(fā)地址都為4字節(jié)
RFRxLen, //接收數(shù)據(jù)長(zhǎng)度,10字節(jié)
RFTxLen, //發(fā)送數(shù)據(jù)長(zhǎng)度,10字節(jié)
0xE7,
0xE7,
0xE7,
0xE7, //地址
0xDF //16位校驗(yàn),CRC開,16M晶振,外部時(shí)鐘使能500KHZ輸出
};
INT8U RFRxBuf[RFRxLen+2]; //射頻接收緩沖區(qū)
INT8U RFTxBuf[RFTxLen+2]; //射頻接收緩沖區(qū)
U32 CntTmp;
void PORT_Init(void)
{
P2SEL &= ~(BIT5|BIT4|BIT3|BIT2|BIT1|BIT0);
P2DIR |= (BIT3|BIT2|BIT1);
//p2.1--PWR_UP,OUTPUT
//p2.2--TRX_CE,OUTPUT
//p2.3--TX_EN, OUTPUT
P2DIR &= ~(BIT5|BIT4|BIT0);
//p2.0--uCLK, INPUT
//p2.4--KEY1, INPUT
//p2.5--KEY2, INPUT
P1SEL &= ~(BIT7|BIT6|BIT5|BIT4|BIT3|BIT2|BIT1);
P1DIR &= ~(BIT7|BIT6|BIT5);
//p1.7--CD, INPUT
//p1.6--AM, INPUT
//p1.5--DR, INPUT
P1DIR |= (BIT4|BIT3|BIT2|BIT1);//LED4--LED1, OUTPUT
P1OUT |= (BIT4|BIT3|BIT2|BIT1);
}
void SPI_Init(void)
{
P3SEL |= (BIT3|BIT2|BIT1);
P3SEL &= ~BIT0;
// P3.1 P3.2 P3.3作為SPI的管腳
// p3.0--CE905,905片選, I/O
// p3.1--MOSI,
// p3.2--MISO,
// P3.3--SCK905,
P3DIR |= (BIT3|BIT1|BIT0);
//P3.3作為輸出
//P3.1作為輸出
//P3.0作為輸出
P3OUT |= (BIT3|BIT1|BIT0);
//P3.0輸出高電平
//P3.1輸出高電平
//P3.3輸出高電平
// 以下設(shè)置SPI口的參數(shù)
UCTL0 |= SWRST; //為設(shè)置串口作準(zhǔn)備
UCTL0 |= (CHAR + SYNC + MM); //數(shù)據(jù)為8比特,選擇SPI模式,單片機(jī)為主機(jī)模式
UTCTL0 &= ~(CKPH|CKPL); //SPI時(shí)鐘設(shè)置
UTCTL0 |= (SSEL1|STC); // 時(shí)鐘源為SMCLK,選擇3線模式
UBR00 = 0x02; //波特率為ACLK/2
UBR10 = 0x00; //波特率為ACLK/2
UMCTL0 = 0x00; //調(diào)整寄存器
ME1 |= USPIE0; //SPI0模塊允許
IE1 |= URXIE0; //接收中斷允許
IE1 |= UTXIE0; //發(fā)送中斷允許
UCTL0 &= ~SWRST; // Initalise USART state machine
}
void Init_CLK(void)
{
unsigned int i;
BCSCTL1 = 0X00;//將寄存器的內(nèi)容清零
//XT2震蕩器開啟
//LFTX1工作在低頻模式
//ACLK的分頻因子為1
do
{
IFG1 &= ~OFIFG; // 清除OSCFault標(biāo)志
for (i = 0x20; i > 0; i--);
}
while ((IFG1 & OFIFG) == OFIFG); // 如果 OSCFault == 1
BCSCTL2 = 0x00; //將寄存器的內(nèi)容清零
BCSCTL2 |=SELM_2; //MCLK select lftx
BCSCTL2 |= SELS; //SMCLK--DCOCLK
}
void Delay_ms(unsigned long nValue)//毫秒為單位,8MHz為主時(shí)鐘
{
unsigned long nCount;
int i;
unsigned long j;
nCount = 2667;
for(i = nValue;i > 0;i--)
{
for(j = nCount;j > 0;j--);
}
return;
}
void Delay_us(unsigned long nValue)//微秒為單位,8MHz為主時(shí)鐘
{
int nCount;
int i;
int j;
nCount = 3;
for(i = nValue;i > 0;i--)
{
for(j = nCount;j > 0;j--);
}
return;
}
void delay(INT16U s)
{
INT16U i;
for(i=0; i
}
void SpiWriteByte(INT8U dat)
{
// while ((IFG1 & UTXIFG0)==0x0);
U0TXBUF = dat;
while ((U0TCTL & TXEPT) == 0x0);
}
INT8U SpiReadByte(void)
{
//INT8U temp;
U0TXBUF = 0;
while ((U0TCTL & TXEPT) == 0x0);
// while((IFG1 & URXIFG0)==0);
// temp=U0RXBUF;
// return temp;
return U0RXBUF;
}
void INIT_905(void)
{
INT8U i;
TRX_CE_0; //TRX_CE = 0;
TX_EN_0; //TX_EN = 0;
CE905_0; //ce905=0
SpiWriteByte(WRC);
for(i=0; i<10; i++)
{
SpiWriteByte(Nrf9e5Config[i]);
}
// while ((IFG1 & UTXIFG0)==0x0);
CE905_1;//CE_905 = 1
}
void TransmitBytes(void)
{
INT8U i;
TX_EN_1; //TX_EN=1
TRX_CE_0; //TRX_CE = 0
delay(1000);
CE905_0; //ce905=0
SpiWriteByte(WTA);
SpiWriteByte(0xE7);
SpiWriteByte(0xE7);
SpiWriteByte(0xE7);
SpiWriteByte(0xE7);
// while ((IFG1 & UTXIFG0)==0x0);
CE905_1; //CE_905 = 1
delay(1);
CE905_0; //ce905=0
SpiWriteByte(WTP);
for(i=0; i
{
SpiWriteByte(RFTxBuf[i]);
}
// while ((IFG1 & UTXIFG0)==0x0);
CE905_1; //CE_905 = 1
delay(1);
TRX_CE_1; //TRX_CE=1
CntTmp = 0;
while(DR905 == 0)//; //DR=0
{
CntTmp ++;
}
TRX_CE_0; //TRX_CE = 0
TX_EN_0; //TX_EN=0
}
INT8U Recepacket(void)
{
INT8U i;
TX_EN_0; //TX_EN=0
TRX_CE_1; //TRX_CE=1
while(CD905 == 0);
TRX_CE_0; //TRX_CE=0
CE905_0; //ce905=0
SpiWriteByte(RRP);
for(i=0; i
{
RFRxBuf[i] = SpiReadByte(); //接收數(shù)據(jù)
}
while(DR905) //DR=1
{
SpiReadByte();
}
CE905_1; //ce905=1
return 1;
}
void ChangeMode(U8 mode)
{
switch(mode)
{
case PowerDown:
PD905; /// 配置為掉電模式 ///
TX_EN_0;
TRX_CE_0;
break;
case Standby:
TRX_CE_0;
TX_EN_0;
PU905;
break;
case RXMode:
PU905;
// delay(2);
TX_EN_0;
_NOP();
_NOP();
TRX_CE_1;
break;
case TXMode:
TX_EN_1;
PU905;
TRX_CE_0;
break;
case TXData:
TRX_CE_1;
delay(2);
TRX_CE_0; /// ?? Make a 20us Pulse as nRF905 Datasheet ///
break;
}
}
上一頁(yè) 1 2 下一頁(yè)

評(píng)論


技術(shù)專區(qū)

關(guān)閉