新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 基于CC1101與STC89C52無線收發(fā)程序(發(fā)射程序)

基于CC1101與STC89C52無線收發(fā)程序(發(fā)射程序)

作者: 時間:2016-11-27 來源:網(wǎng)絡 收藏

//-----------------------------------------------/
//函數(shù)功能:CC1101初始化/
//輸入?yún)?shù):無/
//輸出參數(shù):無/
//--------------------------------------------/
void CC1101_Init(void)
{
CSN=0;
SCLK=0;
CSN=1;
delay_ms(10);
POWER_UP_RESET();
Write_Config();
SPI_Write_Burst(PATABLE,P_Config,8);
SPI_Write_Strobe(SCAL);
SPI_Write_Strobe(SIDLE);
SPI_Write_Strobe(SFTX);
//for(i=0;i<100;i++)
//SPI_Write_Strobe(SNOP);
//SPI_Write_Strobe(SIDLE);
//SPI_Write_Strobe(SRX);
}
//-----------------------------------------------/
//函數(shù)功能:SPI寫命令/
//輸入?yún)?shù):Strobe/
//輸出參數(shù):無/
//---------------------------------------------/
void SPI_Write_Strobe(uchar strobe)
{
CSN=0;
while(SO);
SPI_WR_Byte(strobe);
CSN=1;
}

uchar SPI_Read_Byte(uchar addr)
{
uchar value;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0x80);
value=SPI_WR_Byte(0);
CSN=1;
return value;
}
void SPI_Write_Byte(uchar addr,uchar value)
{
CSN=0;
while(SO);
SPI_WR_Byte(addr);
SPI_WR_Byte(value);
CSN=1;
}

void SPI_Write_Burst(uchar addr,uchar *buffer,uchar count)
{
uchar i;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0x40);
for(i=0;i{
SPI_WR_Byte(buffer[i]);
delay_us(10);
}
CSN=1;
}

void SPI_Read_Burst(uchar addr,uchar *buffer,uchar count)
{
uchar i;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0xc0);
for(i=0;ibuffer[i]=SPI_WR_Byte(0);
CSN=1;
}

uchar SPI_Read_Status(uchar addr)
{
uchar value;
CSN=0;
while(SO);
SPI_WR_Byte(addr|0xc0);//
value=SPI_WR_Byte(0);
CSN=1;
return value;
}

void SPI_Write_Packet(uchar *TX_Buffer,uchar size)
{
//uchar j;

SPI_Write_Byte(TX_FIFO,size);
SPI_Write_Burst(TX_FIFO,TX_Buffer,size);
SPI_Write_Strobe(STX);
while(!GDO0);
LED=1;
while(GDO0);
LED=0;
SPI_Write_Strobe(SIDLE);
SPI_Write_Strobe(SFTX);

}

void Write_Config(void)
{
SPI_Write_Byte(IOCFG2,0x0b);//

SPI_Write_Byte(IOCFG0,0x06);//

SPI_Write_Byte(FIFOTHR,0x07);//
//SPI_Write_Byte(SYNC1,0xd3);//
//SPI_Write_Byte(SYNC0,0x91);//
SPI_Write_Byte(PKTLEN,0xff);//
SPI_Write_Byte(PKTCTRL1, 0x04);//
SPI_Write_Byte(PKTCTRL0, 0x05);//
//SPI_Write_Byte(ADDR,0x01);//
//SPI_Write_Byte(CHANNR,0x00);//
SPI_Write_Byte(FSCTRL1,0x06);//
SPI_Write_Byte(FSCTRL0,0x00);//
SPI_Write_Byte(FREQ2,0x10);//
SPI_Write_Byte(FREQ1,0xb0);//
SPI_Write_Byte(FREQ0,0x71);//
SPI_Write_Byte(MDMCFG4,0x2d);//
SPI_Write_Byte(MDMCFG3,0x3b);//
SPI_Write_Byte(MDMCFG2,0x71);//
SPI_Write_Byte(MDMCFG1,0xa2);//
SPI_Write_Byte(MDMCFG0,0xf8);//
SPI_Write_Byte(DEVIATN,0x15);//
//SPI_Write_Byte(MCSM2 ,0x07);//
SPI_Write_Byte(MCSM1 ,0x30);//
SPI_Write_Byte(MCSM0 ,0x18);//

SPI_Write_Byte(FOCCFG,0x16);//
SPI_Write_Byte(BSCFG,0x6c);//
SPI_Write_Byte(AGCCTRL2, 0x03);//
//SPI_Write_Byte(AGCCTRL1, 0x00);//
SPI_Write_Byte(AGCCTRL0, 0x91);//
//SPI_Write_Byte(WOREVT1 , 0x87);//
//SPI_Write_Byte(WOREVT0 , 0x6b);//
//SPI_Write_Byte(WORCTRL,0xf8);//
SPI_Write_Byte(FREND1,0x56);//
SPI_Write_Byte(FREND0,0x10);//



SPI_Write_Byte(FSCAL3,0xe9);//
SPI_Write_Byte(FSCAL2,0x0a);//
//SPI_Write_Byte(FSCAL1,0x00);//
SPI_Write_Byte(FSCAL0,0x1f);//
//SPI_Write_Byte(RCCTRL1 , 0x41);//
//SPI_Write_Byte(RCCTRL0 , 0x00);//
SPI_Write_Byte(FSTEST,0x59);//
//SPI_Write_Byte(PTEST ,0x7f);//
SPI_Write_Byte(TEST2,0x81);//
SPI_Write_Byte(TEST1,0x35);//
SPI_Write_Byte(TEST0,0x09);//


}

uchar SPI_Read_Packet(uchar *RX_Buffer,uchar length)
{
uchar status[2];
uchar size=0;
SPI_Write_Strobe(SRX);
delay_ms(1);
if(SPI_Read_Status(RXBYTES)==0) return 0;
while(GDO0)
{
SPI_Write_Strobe(SIDLE);
return 0;
}
size=SPI_Read_Byte(RX_FIFO);

if(size==length)
{
SPI_Read_Burst(RX_FIFO,RX_Buffer,size);
SPI_Read_Burst(RX_FIFO,status,2);
SPI_Write_Strobe(SFRX);
return(status[1]&0x80);
}
else
{
SPI_Write_Strobe(SIDLE);
SPI_Write_Strobe(SFRX);
return 0;
}

}

void main(void)
{
CC1101_Init();
delay_ms(100);
while(1)
{
SPI_Write_Packet(tx_buffer,sizeof(tx_buffer));
}
}


上一頁 1 2 下一頁

評論


技術專區(qū)

關閉