基于51单片机驱动SI4432无线模块收发C语言程序Word格式.docx
《基于51单片机驱动SI4432无线模块收发C语言程序Word格式.docx》由会员分享,可在线阅读,更多相关《基于51单片机驱动SI4432无线模块收发C语言程序Word格式.docx(11页珍藏版)》请在冰豆网上搜索。
unsignedcharData_Len;
typedefenum_RF_ENUM
{
RF_OK
=
0x00,
//functionresponseparameters
RF_ERROR_TIMING
0x01,
RF_ERROR_PARAMETER=0x02,
RF_PACKET_RECEIVED
=0x03,
RF_NO_PACKET
=
0x04,
RF_CRC_ERROR=0x05,
}RF_ENUM;
//----------------------------RF4432配置寄存器地址--------------------------------
#defineDEVICE_TYPE0x00
#defineEVICE_VERSION
0x01
//版本号
#defineDEVICE_STATUS
0x02
//设备状态
#defineINTERRUPT_STATUS_1
0x03
#defineINTERRUPT_STATUS_2
0x04
#defineINTERRUPT_ENABLE_10x05
#defineINTERRUPT_ENABLE_20x06
#defineOPERATING_FUNCTION_CONTROL_1
0x07
//工作模式和功能控制1
#defineOPERATING_FUNCTION_CONTROL_20x08
//工作模式和功能控制2
#defineCRYSTAL_OSCILLATOR_LOAD_CAPACITANCE
0x09
//晶振负载电容设置
#defineMICROCONTROLLER_OUTPUT_CLOCK0x0A
#defineGPIO0_CONFIGURATION0x0B
//GPIO0功能设置寄存器见英文文档第105页
#defineGPIO1_CONFIGURATION
0x0C
#defineGPIO2_CONFIGURATION0x0D
#defineIO_PORT_CONFIGURATION0x0E
#defineADC_CONFIGURATION
0x0F
#defineADC_SENSOR_AMPLIFIER_OFFSET
0x10
#defineADC_VALUE0x11
#defineTEMPERATURE_SENSOR_CONTROL
0x12
//温度传感器校准
#defineTEMPERATURE_VALUE_OFFSET0x13
#defineWAKE_UP_TIMER_PERIOD_1
0x14
#defineWAKE_UP_TIMER_PERIOD_2
0x15
#defineWAKE_UP_TIMER_PERIOD_3
0x16
#defineWAKE_UP_TIMER_VALUE_10x17
#defineWAKE_UP_TIMER_VALUE_20x18
#defineLOW_DUTY_CYCLE_MODE_DURATION0x19
#defineLOW_BATTERY_DETECTOR_THRESHOLD
0x1A
//低压检测阈值寄存器
#defineBATTERY_VOLTAGE_LEVEL
0x1B
#defineIF_FILTER_BANDWIDTH
0x1C
//中频滤波器带宽寄存器
#defineAFC_LOOP_GEARSHIFT_OVERRIDE0x1D
#defineAFC_TIMING_CONTROL
0x1E
#defineCLOCK_RECOVERY_GEARSHIFT_OVERRIDE
0x1F
#defineCLOCK_RECOVERY_OVERSAMPLING_RATIO0x20
#defineCLOCK_RECOVERY_OFFSET_20x21
#defineCLOCK_RECOVERY_OFFSET_1
0x22
#defineCLOCK_RECOVERY_OFFSET_00x23
#defineCLOCK_RECOVERY_TIMING_LOOP_GAIN_1
0x24
#defineCLOCK_RECOVERY_TIMING_LOOP_GAIN_00x25
#defineRECEIVED_SIGNAL_STRENGTH_INDICATOR0x26
#defineRSSI_THRESHOLD_FOR_CLEAR_CHANNEL_INDICATOR0x27
#defineANTENNA_DIVERSITY_REGISTER_10x28
#defineANTENNA_DIVERSITY_REGISTER_20x29
#defineDATA_ACCESS_CONTROL0x30
#defineEZMAC_STATUS0x31
#defineHEADER_CONTROL_10x32
//Header起始码设置
#defineHEADER_CONTROL_20x33
#definePREAMBLE_LENGTH
0x34
//前导码长度
#definePREAMBLE_DETECTION_CONTROL
0x35
//前导码检测设置
#defineSYNC_WORD_30x36
//同步字节
#defineSYNC_WORD_20x37
#defineSYNC_WORD_1
0x38
#defineSYNC_WORD_00x39
#defineTRANSMIT_HEADER_3
0x3A
#defineTRANSMIT_HEADER_2
0x3B
#defineTRANSMIT_HEADER_1
0x3C
#defineTRANSMIT_HEADER_0
0x3D
#defineTRANSMIT_PACKET_LENGTH
0x3E
//发送数据包长度,详细请看P125页
#defineCHECK_HEADER_3
0x3F
#defineCHECK_HEADER_2
0x40
#defineCHECK_HEADER_10x41
#defineCHECK_HEADER_0
0x42
#defineHEADER_ENABLE_3
0x43
#defineHEADER_ENABLE_2
0x44
#defineHEADER_ENABLE_1
0x45
#defineHEADER_ENABLE_0
0x46
#defineRECEIVED_HEADER_3
0x47
#defineRECEIVED_HEADER_2
0x48
#defineRECEIVED_HEADER_1
0x49
#defineRECEIVED_HEADER_0
0x4A
#defineRECEIVED_PACKET_LENGTH
0x4B
//接收数据包长度,详细请看P134页
#defineANALOG_TEST_BUS
0x50
#defineDIGITAL_TEST_BUS_ENSCTEST_
0x51
#defineTX_RAMP_CONTROL
0x52
#definePLL_TUNE_TIME
0x53
//锁相环切换时间
#defineCALIBRATION_CONTROL
0x55
#defineMODEM_TEST
0x56
#defineCHARGEPUMP_TEST
0x57
#defineCHARGEPUMP_CURRENT_TRIMMING_OVERRIDE
0x58
#defineDIVIDER_CURRENT_TRIMMING
0x59
#defineVCO_CURRENT_TRIMMING
0x5A
#defineVCO_CALIBRATION_OVERRIDE
0x5B
#defineSYNTHESIZER_TEST
0x5C
#defineBLOCK_ENABLE_OVERRIDE_1
0x5D
#defineBLOCK_ENABLE_OVERRIDE_2
0x5E
#defineBLOCK_ENABLE_OVERRIDE_3
0x5F
#defineCHANNEL_FILTER_COEFFICIENT_ADDRESS
0x60
#defineCHANNEL_FILTER_COEFFICIENT_VALUE
0x61
#defineCRYSTAL_OSCILLATOR_CONTROL_TEST
0x62
#defineRC_OSCILLATOR_COARSE_CALIBRATION_OVERRIDE
0x63
#defineRC_OSCILLATOR_FINE_CALIBRATION_OVERRIDE
0x64
#defineLDO_CONTROL_OVERRIDE_ENSPOR
0x65
#defineLDO_LEVEL_SETTING
0x66
#defineDELTASIGMA_ADC_TUNING_1
0x67
#defineDELTASIGMA_ADC_TUNING_2
0x68
#defineAGC_OVERRIDE_1
0x69
#defineAGC_OVERRIDE_2
0x6A
#defineGFSK_FIR_FILTER_COEFFICIENT_ADDRESS
0x6B
#defineGFSK_FIR_FILTER_COEFFICIENT_VALUE
0x6C
#defineTX_POWER
0x6D
//发射功率设置,详细请见P153
#defineTX_DATA_RATE_1
0x6E
//数据发送波特率设置寄存器1
#defineTX_DATA_RATE_0
0x6F
//数据发送波特率设置寄存器0
#defineMODULATION_MODE_CONTROL_1
0x70
//调制方式控制,详细请见P155
#defineMODULATION_MODE_CONTROL_2
0x71
#defineFREQUENCY_DEVIATION
0x72
#defineFREQUENCY_OFFSET_1
0x73
#defineFREQUENCY_OFFSET_2
0x74
#defineFREQUENCY_BAND_SELECT
0x75
//频段选择,详细请见P157
#defineNOMINAL_CARRIER_FREQUENCY_1
0x76
//基准载波频率
#defineNOMINAL_CARRIER_FREQUENCY_0
0x77
#defineFREQUENCY_HOPPING_CHANNEL_SELECT
0x79
//跳频频道选择,详细请见P158
#defineFREQUENCY_HOPPING_STEP_SIZE
0x7A
//跳频频道间隔
#defineTX_FIFO_CONTROL_1
0x7C
#defineTX_FIFO_CONTROL_2
0x7D
#defineRX_FIFO_CONTROL
0x7E
#defineFIFO_ACCESS
0x7F
//FIFO读写方式设置,
//-------------------------------RF4432控制指令---------------------------------------------
#defineRR
0x00
#defineWR
0x80
//------------------------------------------------延时------------------------------
voiddelay_10us(charn)
inti;
while(n--)
for(i=0;
i<
5;
i++);
}
voiddelay_ms(intnum)
intx,y;
for(y=0;
y<
num;
y++)
{
for(x=0;
x<
500;
x)
x++;
}
//------------------------------------SPI单字节读取函数-------------------------------------
unsignedcharSPI_Read(void)
{
unsignedchari,rxdata;
rxdata=0x00;
for(i=0;
i<
8;
i++)
rxdata=rxdata<
<
1;
RF4432_SCLK=1;
RF4432_SDO=1;
if(RF4432_SDO==1)
//读取最高位,保存至最末尾,通过左移位完成整个字节
rxdata|=0x01;
else
rxdata&
=~0x01;
delay_10us
(2);
RF4432_SCLK=0;
returnrxdata;
//--------------------------SPI单字节写入函数----------------------------------------------
voidSPI_Write(unsignedchartxdata)
unsignedchari;
if(txdata&
0x80)
//总是发送最高位
RF4432_SDI=1;
RF4432_SDI=0;
txdata=txdata<
//---------------------RF4432寄存器读取函数---------------------------------------------------
void
RF4432_ReadReg(unsignedchar
addr,unsignedchar*RegisterData)
RF4432_SEL=0;
SPI_Write(addr|RR);
*RegisterData=SPI_Read();
RF4432_SEL=1;
//----------------RF4432寄存器写入函数----------------------------------------------------------
RF4432_WriteReg(unsignedchar
addr,unsignedcharvalue)
SPI_Write(addr|WR);
SPI_Write(value);
//-----------------------RF4432寄存器读取函数---------------------------------------------
voidRF4432_ReadBurestReg(unsignedchar
addr,unsignedchar
*p,unsignedchar
count)
for(i=0;
count;
p[i]=SPI_Read();
//---------------------------------RF4432射频芯片初始化函数------------------------------------
voidRF4432_Init(void)