您的位置 首页 厂商

根据430单片机的NRF905收发驱动程序

includemsp430x14xh>includeBaseTypehincludestringh>includectypeh>defineWaitKeyDw100defineWai

#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 //射频发送绶冲区长度
#define RFRxLen 16 //射频接纳绶冲区长度
#define INT8U unsigned char
#define INT16U unsigned int
#define _nop_() _NOP()
INT8U Nrf9e5Config[10] = {
0x6B, //频道设置
0x0C, //主动重发关,发送节电形式关,输出功率10dB,433.1MHZ
0x44, //收发地址都为4字节
RFRxLen, //接纳数据长度,10字节
RFTxLen, //发送数据长度,10字节
0xE7,
0xE7,
0xE7,
0xE7, //地址
0xDF //16位校验,CRC开,16M晶振,外部时钟使能500KHZ输出
};
INT8U RFRxBuf[RFRxLen+2]; //射频接纳缓冲区
INT8U RFTxBuf[RFTxLen+2]; //射频接纳缓冲区
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输出高电平
// 以下设置SPI口的参数
UCTL0 |= SWRST; //为设置串口作预备
UCTL0 |= (CHAR + SYNC + MM); //数据为8比特,挑选SPI形式,单片机为主机形式
UTCTL0 &= ~(CKPH|CKPL); //SPI时钟设置
UTCTL0 |= (SSEL1|STC); // 时钟源为SMCLK,挑选3线形式
UBR00 = 0x02; //波特率为ACLK/2
UBR10 = 0x00; //波特率为ACLK/2
UMCTL0 = 0x00; //调整寄存器
ME1 |= USPIE0; //SPI0模块答应
IE1 |= URXIE0; //接纳中止答应
IE1 |= UTXIE0; //发送中止答应
UCTL0 &= ~SWRST; // Initalise USART state machine
}
void Init_CLK(void)
{
unsigned int i;
BCSCTL1 = 0X00;//将寄存器的内容清零
//XT2震动器敞开
//LFTX1作业在低频形式
//ACLK的分频因子为1
do
{
IFG1 &= ~OFIFG; // 铲除OSCFault标志
for (i = 0x20; i > 0; i–);
}
while ((IFG1 & OFIFG) == OFIFG); // 假如 OSCFault == 1
BCSCTL2 = 0x00; //将寄存器的内容清零
BCSCTL2 |=SELM_2; //MCLK select lftx
BCSCTL2 |= SELS; //SMCLK–DCOCLK
}
void Delay_ms(unsigned long nValue)//毫秒为单位,8MHz为主时钟
{
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为主时钟
{
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(); //接纳数据
}
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;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/257753.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部