|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2013-6-4 18:30:12
|
显示全部楼层
lalapunk1983 发表于 2013-6-4 18:27 ![](static/image/common/back.gif)
上程序我给你看。用了很久的SI4432没。没发现你说的问题。
多谢了!程序如下:
//*************************************************************
//函数名:void RfChipInit(void)
//功能:芯片初始化
//参数: 无
//*************************************************************
void RfChipInit(void)
{
Si446x_enable;
_delay_ms(20);
//ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
//ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
//SpiRfWriteRegister(0x07, 0x80); //write 0x80 to the Operating & Function Control1 register
//_delay_ms(10);
ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
SpiRfWriteRegister(0x07, 0x80); //write 0x80 to the Operating & Function Control1 register
while ( Read_446x_IRQ == 1);
ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
while ( Read_446x_IRQ == 1);
ItStatus1 = SpiRfReadRegister(0x03);
ItStatus2 = SpiRfReadRegister(0x04);
/*设置RF 参数:中断频率(center frequency)、发射数据速率(transmit data rate)和发射偏差(transmit deviation)*/
/*set the center frequency to 433 MHz*/
SpiRfWriteRegister(0x75, 0x53);
SpiRfWriteRegister(0x76, 0x4b);
SpiRfWriteRegister(0x77, 0x00);
/*set the center frequency to 868 MHz*/
//SpiRfWriteRegister(0x75, 0x73);
//SpiRfWriteRegister(0x76, 0x64);
//SpiRfWriteRegister(0x77, 0x00);
/*set the desired TX data rate 4.8K*/
SpiRfWriteRegister(0x6E, 0x27);
SpiRfWriteRegister(0x6F, 0x52);
SpiRfWriteRegister(0x70, 0x24);
/*set the desired TX deviation */
SpiRfWriteRegister(0x72, 0x18);
/*接收的GFSK 调制数据配置调制解调器参数*/
SpiRfWriteRegister(0x1C, 0x1D);
SpiRfWriteRegister(0x20, 0xA1);
SpiRfWriteRegister(0x21, 0x20);
SpiRfWriteRegister(0x22, 0x4E);
SpiRfWriteRegister(0x23, 0xA5);
SpiRfWriteRegister(0x24, 0x00);
SpiRfWriteRegister(0x25, 0x1B);
SpiRfWriteRegister(0x1D,0x40);
/*set the TX power to MAX*/
SpiRfWriteRegister(0x6D, 0x0f); //20DBM
/*配置接收数据包处理器(packet handler)*/
SpiRfWriteRegister(0x35, 0x28); //write 0x28 to the Preamble Detection Control register
SpiRfWriteRegister(0x34, 0x0A); //write 0x0A to the Preamble Length register
/*禁止桢头字节(本示例中未用),设置同步字长度为两个字节*/
SpiRfWriteRegister(0x33, 0x02); //write 0x02 to the Header Control2 register
/*Disable the receive header filters*/
SpiRfWriteRegister(0x32, 0x00 ); //write 0x00 to the Header Control1 register
/*设置同步字样式(pattern)为0x2DD4*/
/*Set the sync word pattern to 0x2DD4 */
SpiRfWriteRegister(0x36, 0x2D); //write 0x2D to the Sync Word 3 register
SpiRfWriteRegister(0x37, 0xD4); //write 0xD4 to the Sync Word 2 register
/*Enable the receive packet handler and CRC-16 (IBM) check*/
SpiRfWriteRegister(0x30, 0x8D); //write 0x8D to the Data Access Control register
/*enable FIFO mode and GFSK modulation*/
SpiRfWriteRegister(0x71, 0x63);//write 0x63 to the Modulation Mode Control 2 register
/*set the GPIO's according the testcard type*/
SpiRfWriteRegister(0x0C, 0x12); //write 0x12 to the GPIO1 Configuration(set the TX state)
SpiRfWriteRegister(0x0D, 0x15); //write 0x15 to the GPIO2 Configuration(set the RX state)
/*设置AGC 和ADC 参数*/
SpiRfWriteRegister(0x6A, 0x0B); //write 0x0B to the AGC Override 2 register
SpiRfWriteRegister(0x68, 0x04);
SpiRfWriteRegister(0x1F, 0x03);
/*set Crystal Oscillator Load Capacitance register*/
SpiRfWriteRegister(0x09, 0x68); //write 0x68 to the CrystalOscillatorLoadCapacitance register
}
//*************************************************************
//函数名:void RFTransmitMessage(u8 *DataMessage,u8 Length)
//功能: 发送一个数据结构
//参数: MESSAGE 结构体
//*************************************************************
void RFTransmitMessage(unsigned char *DataMessage,unsigned char Length)
{
//SET THE CONTENT OF THE PACKET
SpiRfWriteRegister(0x3E, Length); //write 8 to the Transmit Packet Length register
//fill the payload into the transmit FIFO
SpiRfWriteBurst(0x7F, DataMessage, Length);
//Disable all other interrupts and enable the packet sent interrupt only.
//This will be used for indicating the successfull packet transmission for the MCU
SpiRfWriteRegister(0x05, 0x04); //write 0x04 to the Interrupt Enable 1 register
SpiRfWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register
//Read interrupt status regsiters. It clear all pending interrupts and the nIRQ pin goes back to high.
ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
//enable transmitter
//The radio forms the packet and send it automatically.
SpiRfWriteRegister(0x07, 0x09);//write 0x09 to the Operating Function Control 1 register
//enable the packet sent interupt only
SpiRfWriteRegister(0x05, 0x04); //write 0x04 to the Interrupt Enable 1 register
//wait for the packet sent interrupt
//The MCU just needs to wait for the 'ipksent' interrupt.
while(Read_446x_IRQ == 1);
//read interrupt status registers to release the interrupt flags
ItStatus1 = SpiRfReadRegister(0x03); //read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04); //read the Interrupt Status2 register
//wait a bit for showing the LED a bit longer
}
//*************************************************************
//函数名:void RFReceiveReady(void)
//功能: 进入接收数据状态
//参数: 无
//*************************************************************
void RFReceiveReady(void)
{
//enable receiver chain
SpiRfWriteRegister(0x07, 0x05);//write 0x05 to the Operating Function Control 1 register
//Enable two interrupts:
// a) one which shows that a valid packet received: 'ipkval'
// b) second shows if the packet received with incorrect CRC: 'icrcerror'
SpiRfWriteRegister(0x05, 0x03); //write 0x03 to the Interrupt Enable 1 register
SpiRfWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register
//read interrupt status registers to release all pending interrupts
ItStatus1 = SpiRfReadRegister(0x03);//read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04);//read the Interrupt Status2 register
}
//*************************************************************
//函数名:void RFGetBuffer(u8 * buff)
//功能: 对取buff 数据
//参数: 字符串指针
//*************************************************************
void RFGetBuffer(unsigned char *buff)
{
unsigned char k;
//wait for the packet sent interrupt
//The MCU just needs to wait for the 'ipksent' interrupt.
while(Read_446x_IRQ == 1);
//wait for the interrupt event
if( Read_446x_IRQ == 0 )
{
//read interrupt status registers
ItStatus1 = SpiRfReadRegister(0x03);//read the Interrupt Status1 register
ItStatus2 = SpiRfReadRegister(0x04);//read the Interrupt Status2 register
//packet received interrupt occurred
if( (ItStatus1 & 0x02) == 0x02 )
{
//disable the receiver chain
SpiRfWriteRegister(0x07, 0x01);//write 0x01 to the Operating Function Control 1 register
//Read the length of the received payload
k= SpiRfReadRegister(0x4B) ; //读取数据长度
//Get the received payload from the RX FIFO
SpiRfReadBurest(0x7F, buff, k); //读取fifo 数据
if(buff[0]==0x55){} //确定是不是对方发出来的
//{ P_2_LED = ~P_2_LED; }
}
}
} |
|