【求助】那位高手搞过nRF2401,拉小弟一把
[复制链接]
请高手看看我的程序问题在哪.
1发送
void main (void)
{ uint i;
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1&=~XT2OFF; //XT2 ON ,LFXT1 work at low frequency (32.768kHz)
BCSCTL2=0x88;//10001000; XT2 as MCLK,
//BCSCTL2=0xBA;//1001 1010; XT2 as MCLK,
//wait for clock source charging complete
do
{
IFG1&=~OFIFG; //clear OSCFault flag_breathe
for(i=0xFF;i>0;i--); //delay wait
} while((IFG1&OFIFG)!=0);
init_io();
nRF2401_ON();
BuildInitWord();
InitnRF2401();
//CE_High();
// RXEN_HIGH();
// Delay10();
//TXEN_LOW();
BuildShockWord();
ShockBurst();
// Delay10();
//RXEN_HIGH();
//while(1)
//{if(P2IN&DR1)
// ReceiveShock();
//}
}
void init_io(void)
{P1DIR|=CS+DATA+PWR;//P1.0+P1.3+P1.4
P1DIR&=~DR1;//P1.1
P6DIR|=CLK1;//P5.6
P5DIR|=CE; //P6.6
P1OUT|=DATA+PWR;
P1OUT&=~CS;
P6OUT&=~CLK1;
P5OUT&=~CE;
}
void BuildInitWord(void)
{InitData[0]=0x50;//频道2数据长度
InitData[1]=0x50;//频道1数据长度,80bit,10byte.
InitData[2]=0x00;//频道2地址
InitData[3]=0xcc;//频道2地址
InitData[4]=0xcc;//频道2地址
InitData[5]=0xcc;//频道2地址
InitData[6]=0x00;//频道2地址
InitData[7]=0x00;//频道1地址
InitData[8]=0xcc;//频道1地址
InitData[9]=0xcc;//频道1地址
InitData[10]=0xcc;//频道1地址
InitData[11]=0xcc;//频道1地址
InitData[12]=0x83;//4byte地址,16CRC,CRC允许
InitData[13]=0x4f;//一个频道接收,shock模式,250K,16MCRY,0DBm
InitData[14]=0x04;//发射
}
void InitnRF2401(void)
{P5OUT&=~CE;
P1OUT|=CS;
Delay5us();//
P1OUT&=~DATA;
for(ByteCnt=0;ByteCnt<15;ByteCnt++)
{tempi=InitData[ByteCnt];
for(BitCnt=0;BitCnt<8;BitCnt++)
{ P6OUT&=~CLK1;
if(tempi&0x80)
P1OUT|=DATA;
else
P1OUT&=~DATA;
Delay500ns();//
P6OUT|=CLK1;
Delay500ns();//
P6OUT&=~CLK1;
tempi=tempi<<1;
}
}
P1OUT&=~CS;
}
void DoSPIClock(void)
{ P6OUT|=CLK1;
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
_NOP();
P6OUT&=~CLK1;
}
void CE_High(void)
{P5OUT|=CE;
}
void CE_Low(void)
{P5OUT&=~CE;
}
void nRF2401_ON(void)
{P1OUT|=PWR;
}
void RXEN_HIGH (void)
{P5OUT&=~CE;
P1OUT|=CS;
P1OUT|=DATA;
DoSPIClock();
//Delay100();
P1OUT&=~CS;
P5OUT|=CE;
}
void TXEN_LOW(void)
{P5OUT&=~CE;
P1OUT|=CS;
P1OUT&=~DATA;
DoSPIClock();
//Delay100();
P1OUT&=~CS;
P5OUT|=CE;
}
void Delay500ns(void)
{
_NOP();
_NOP();_NOP();_NOP();
}
void Delay5us(void)
{uchar x;
for(x=0;x<10;x++)
Delay500ns();
}
void BuildShockWord(void)
{TXData[0]=0xcc;
TXData[1]=0xcc;
TXData[2]=0xcc;
TXData[3]=0xcc;
TXData[4]=0x55;
TXData[5]=0x55;
TXData[6]=0x55;
TXData[7]=0x54;
TXData[8]=0xf0;
TXData[9]=0xf0;
TXData[10]=0xf0;
TXData[11]=0x0f;
TXData[12]=0x0f;
TXData[13]=0x0f;
}
void ShockBurst(void)
{P1OUT&=~CS;
P5OUT|=CE;
P1OUT&=~DATA;
Delay5us();//
for(ByteCnt1=0;ByteCnt1<14;ByteCnt1++)
{tempi=TXData[ByteCnt1];
for(BitCnt1=0;BitCnt1<8;BitCnt1++)
{P6OUT&=~CLK1;
if(tempi&0x80)
P1OUT|=DATA;
else
P1OUT&=~DATA;
Delay500ns();//
P6OUT|=CLK1;
Delay500ns();//
P6OUT&=~CLK1;
tempi=tempi<<1;
}
}
P5OUT&=~CE;
}
2.接收
void main (void)
{ uint i;
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1&=~XT2OFF; //XT2 ON ,LFXT1 work at low frequency (32.768kHz)
BCSCTL2=0x88;//10001000; XT2 as MCLK,
//BCSCTL2=0xBA;//1001 1010; XT2 as MCLK,
//wait for clock source charging complete
do
{
IFG1&=~OFIFG; //clear OSCFault flag_breathe
for(i=0xFF;i>0;i--); //delay wait
} while((IFG1&OFIFG)!=0);
init_io();
nRF2401_ON();
BuildInitWord();
InitnRF2401();
//CE_High();
//RXEN_HIGH();
//Delay10();
//TXEN_LOW();
// BuildShockWord();
//ShockBurst();
//Delay10();
// RXEN_HIGH();
while(1)
{if(P4IN&DR1)
ReceiveShock();
}
}
void init_io(void)
{P4DIR|=CS+DATA+PWR;//P1.0+P1.3+P1.4
P4DIR&=~DR1;//P1.1
P4DIR|=CLK1;//P5.6
P4DIR|=CE; //P6.6
P4OUT|=DATA+PWR;
P4OUT&=~CS;
P4OUT&=~CLK1;
P4OUT&=~CE;
}
void BuildInitWord(void)
{InitData[0]=0x50;//频道2数据长度
InitData[1]=0x50;//频道1数据长度,80bit,10byte.
InitData[2]=0x00;//频道2地址
InitData[3]=0xcc;//频道2地址
InitData[4]=0xcc;//频道2地址
InitData[5]=0xcc;//频道2地址
InitData[6]=0xcc;//频道2地址
InitData[7]=0x00;//频道1地址
InitData[8]=0xcc;//频道1地址
InitData[9]=0xcc;//频道1地址
InitData[10]=0xcc;//频道1地址
InitData[11]=0xcc;//频道1地址
InitData[12]=0x83;//4byte地址,16CRC,CRC允许
InitData[13]=0x4f;//一个频道接收,shock模式,250K,16MCRY,0DBm
InitData[14]=0x05;//receive
}
void InitnRF2401(void)
{P4OUT&=~CE;
P4OUT|=CS;
Delay5us();//
P4OUT&=~DATA;
for(ByteCnt=0;ByteCnt<15;ByteCnt++)
{tempi=InitData[ByteCnt];
for(BitCnt=0;BitCnt<8;BitCnt++)
{if(tempi&0x80)
P4OUT|=DATA;
else
P4OUT&=~DATA;
Delay500ns();//
P4OUT|=CLK1;
Delay500ns();//
P4OUT&=~CLK1;
tempi=tempi<<1;
//DoSPIClock();
}
}
P4OUT&=~CS;
}
/* void DoSPIClock(void)
{ P4OUT|=CLK1;
_NOP();
_NOP();
P4OUT&=~CLK1;
}
void CE_High(void)
{P4OUT|=CE;
}
void CE_Low(void)
{P4OUT&=~CE;
}*/
void nRF2401_ON(void)
{P4OUT|=PWR;
}
/* void RXEN_HIGH (void)
{P4OUT&=~CE;
P4OUT|=CS;
P4OUT|=DATA;
DoSPIClock();
Delay100();
P4OUT&=~CS;
P4OUT|=CE;
}
void TXEN_LOW(void)
{P4OUT&=~CE;
P4OUT|=CS;
P4OUT&=~DATA;
DoSPIClock();
Delay100();
P4OUT&=~CS;
P4OUT|=CE;
}*/
void Delay500ns(void)
{
_NOP();
_NOP();_NOP();_NOP();
}
void Delay5us(void)
{uchar x;
for(x=0;x<10;x++)
Delay500ns();
}
void ReceiveShock(void)
{uchar i=0,j=0;
uchar dat;
P4DIR&=~DATA;
while(P4IN&DR1)
{ dat=dat<<1;
P4OUT|=CLK1;
if(P4IN&DATA)
dat=dat|0x01;
i++;
if(i>7)
{i=0;
RXData[j]=dat;
j++;
}
P4OUT&=~CLK1;
}
}