#include<stc15.h>
#include<intrins.h>
#include"user.h"
#include"spi.h"
#include"si4432.h"
U8 ItStatus1,ItStatus2;
void Delay_ms(U8 cnt) //@24.000MHz 不分频
{
unsigned char i, j,k;
for(k=0;k<cnt;k++)
{
i = 24;
j = 85;
do
{
while (--j);
}
while (--i);
}
}
/*
void Delay_us(U8 cnt) //@24.000MHz 不分频
{
U8 i;
for(i=0;i<cnt;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
*/
void Si4432_Init(void)
{
SDN=0;
Delay_ms(30); //刚上电复位,SDN要至少拉低30ms
/*进入初始化,首先通过读取中断状态寄存器清除中断标志*/
ItStatus1 = SpiReadRegister(0x03);
ItStatus2 = SpiReadRegister(0x04);
/*芯片复位,刚上电的单片机要对RFIC寄存器复位,以防出现不可知异常*/
SpiWriteRegister(0x07, 0x80);
/*等待RFIC上电复位中断来临,并清除中断标志*/
//while ( NIRQ == 1);
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ItStatus1 = SpiReadRegister(0x03);
ItStatus2 = SpiReadRegister(0x04);
/*等待RFIC就绪中断来临,并清除中断标志(或许不用)*/
// while ( NIRQ == 1);
// ItStatus1 = SpiReadRegister(0x03);
// ItStatus2 = SpiReadRegister(0x04);
SpiWriteRegister(0x75, 0x75);
SpiWriteRegister(0x76, 0xbb);
SpiWriteRegister(0x77, 0x80);
SpiWriteRegister(0x6e, 0x4e);
SpiWriteRegister(0x6f, 0xa5);
SpiWriteRegister(0x70, 0x2c);
SpiWriteRegister(0x1c, 0xa5);
SpiWriteRegister(0x20, 0xa1);
SpiWriteRegister(0x21, 0x20);
//这种设置就是以上都错,频率,波特率都错!!!!如何弄!!
SpiWriteRegister(0x22, 0x4e);
SpiWriteRegister(0x23, 0xa5);
SpiWriteRegister(0x24, 0x00);
SpiWriteRegister(0x25, 0x13);
SpiWriteRegister(0x1d, 0x40);
//SpiWriteRegister(0x1e, 0x02);
//SpiWriteRegister(0x2a, 0xff);
SpiWriteRegister(0x72, 0x1f);
SpiWriteRegister(0x34, 0x0c);
SpiWriteRegister(0x35, 0x28);
SpiWriteRegister(0x33, 0x02);
SpiWriteRegister(0x36, 0x2d);
SpiWriteRegister(0x37, 0xd4);
SpiWriteRegister(0x30, 0x8d);
SpiWriteRegister(0x32, 0x00);
SpiWriteRegister(0x71, 0x63);
SpiWriteRegister(0x0b, 0x12);
SpiWriteRegister(0x0c, 0x15);
SpiWriteRegister(0x5a, 0x7f);
SpiWriteRegister(0x58, 0x80);
SpiWriteRegister(0x59, 0x40);
SpiWriteRegister(0x6a, 0x0b);
SpiWriteRegister(0x68, 0x04);
SpiWriteRegister(0x1f, 0x03);
SpiWriteRegister(0x09, 0xD7);
SpiWriteRegister(0x6d, 0x1e);
SpiWriteRegister(0x07, 0x01);
//接收FIFO清零
SpiWriteRegister(0x08,0x02);
SpiWriteRegister(0x08,0x00);
/*使能接收状态,等待接收中断*/
//使能接收包有效中断'ipkval'
//使能CRC校验错误中断'icrcerror'
SpiWriteRegister(0x05, 0x03);
SpiWriteRegister(0x06, 0x00);
//读中断状态寄存器,清除中断使能寄存器值改变引发的中断
// while(NIRQ); //是否需要看单片机速率
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ItStatus1 = SpiReadRegister(0x03);
ItStatus2 = SpiReadRegister(0x04);
//设置工作模式为接收模式,由预备模式转入
SpiWriteRegister(0x07, 0x05);
}
void RX_Mode(void)
{
// SpiWriteRegister(0x07, 0x01);
//接收FIFO清零
// SpiWriteRegister(0x08,0x02);
// SpiWriteRegister(0x08,0x00);
//使能接收包有效中断'ipkval'
//使能CRC校验错误中断'icrcerror'
SpiWriteRegister(0x05, 0x03);
SpiWriteRegister(0x06, 0x00);
//读中断状态寄存器,清除中断使能寄存器值改变引发的中断
//while(NIRQ); //是否需要看单片机速率
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ItStatus1 = SpiReadRegister(0x03);
ItStatus2 = SpiReadRegister(0x04);
SpiWriteRegister(0x72, 0x1f);
//手动接收模式打开,由预备模式转入
SpiWriteRegister(0x07, 0x05);
}
void TX_Mode(void)
{
//使能数据包发送中断
SpiWriteRegister(0x05, 0x04);
SpiWriteRegister(0x06, 0x00);
//读中断状态寄存器,清除中断使能寄存器值改变引发的中断
// while(NIRQ); //是否需要看单片机速率
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ItStatus1 = SpiReadRegister(0x03);
ItStatus2 = SpiReadRegister(0x04);
//手动发射模式打开,由预备模式转入
SpiWriteRegister(0x07, 0x09);
}