#include "main.h"
extern uchar initdata[15];
extern uchar txdata[32];
uchar temp,bytecnt1,bitcnt,bitcnt1,tempi;
//**************************延时********************
void delay500ns(void)
{
_NOP();
}
//**************************延时********************
void delay5us(void)
{
uchar x,y;
for(y=0;y<5;y++)
for(x=0;x<2;x++);
}
//**************************延时********************
void delay202us(void)
{
uchar n,m;
for(m=0;m<200;m++)
for(n=0;n<3;n++);
}
//**************************延时********************
void delay3ms(void)
{
int i,j;
for(i=3;i>0;i--)
for(j=2000;j>0;j--);
}
//*************************IO口初始化***************
void init_io(void)
{
P1DIR|=CS+PWR;
P3DIR|=DATA;
P1DIR&=~DR1;
P3DIR|=CLK1;
P1DIR|=CE;
P3OUT&=~DATA;
P1OUT&=~PWR;
P1OUT&=~CS;
P3OUT&=~CLK1;
P1OUT&=~CE;
}
//*************************2401上电*****************
void nrf2401_on(void)
{
P1OUT|=PWR;
}
//************************2401配置******************
void buildinitword(void)
{
initdata[0]=0x10;
initdata[1]=0xff;
initdata[2]=0x00;
initdata[3]=0x1c;
initdata[4]=0xcc;
initdata[5]=0xcc;
initdata[6]=0xcc;
initdata[7]=0x00;
initdata[8]=0xcc;
initdata[9]=0xcc;
initdata[10]=0xcc;
initdata[11]=0xcc;
initdata[12]=0x83;
initdata[13]=0x4f;
initdata[14]=0x04;
}
//***********************初始化配置*****************
void initnrf2401(void)
{
P1OUT|=CS;
delay5us();
for(bytecnt1=0;bytecnt1<15;bytecnt1++)
{
tempi=initdata[bytecnt1];
for(bitcnt=0;bitcnt<8;bitcnt++)
{
P3OUT&=~CLK1;
if(tempi&0x80)
P3OUT|=DATA;
else
P3OUT&=~DATA;
delay500ns();
P3OUT|=CLK1;
delay500ns();
P3OUT&=~CLK1;
tempi=tempi<<1;
}
}
P1OUT&=~CS;
P3OUT&=~DATA;
}
//**********************************************
/*void buildshockword(void)
{
txdata[0]=0xcc;
txdata[1]=0xcc;
txdata[2]=0xcc;
txdata[3]=0xcc;
}*/
//************************发送数据包************
void shockburst(unsigned char size,unsigned char *tx_data)
{
P1OUT|=CE;
delay5us();
for(bytecnt1=0;bytecnt1<size;bytecnt1++)
{
tempi=tx_data[bytecnt1];
for(bitcnt1=0;bitcnt1<8;bitcnt1++)
{
P3OUT&=~CLK1;
if(tempi&0x80)
P3OUT|=DATA;
else
P3OUT&=~DATA;
delay500ns();
P3OUT|=CLK1;
delay500ns();
P3OUT&=~CLK1;
tempi=tempi<<1;
}
}
// delay3ms();
P1OUT&=~CE;
P3OUT&=~DATA;
delay202us();
}
//**********************2401初始化********************
void nrf2401_init(void)
{
init_io();
nrf2401_on();
buildinitword();
initnrf2401();
//buildshockword();
}
|