如題,希望能幫助到大家,
定時發送16個字節數據,SI4463無線發送與接收的源碼都有
0.png (5.49 KB, 下載次數: 62)
下載附件
2017-11-19 06:11 上傳
單片機源程序如下:
- #include <reg24le1.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <intrins.h>
- #include "hal_clk.h"
- #include "compiler_defs.h"
- #include "hardware_defs.h"
- #include "spi.h"
- #include "Si446x_B1_defs.h"
- #include "ezrp_next_api.h"
- #include "modem_params.h"
- // Set up modem parameters database; data is retrieved from modem_params.h header file which is
- // automatically generated by the WDS (Wireless Development Suite)
- SEG_CODE U8 ModemTrx1[] = {7, MODEM_MOD_TYPE_7};
- SEG_CODE U8 ModemTrx2[] = {5, MODEM_CLKGEN_BAND_5};
- SEG_CODE U8 ModemTrx3[] = {11, SYNTH_PFDCP_CPFF_11};
- SEG_CODE U8 ModemTrx4[] = {12, FREQ_CONTROL_INTE_12};
- SEG_CODE U8 ModemRx1[] = {11, MODEM_MDM_CTRL_11};
- SEG_CODE U8 ModemRx2[] = {14, MODEM_BCR_OSR_1_14};
- SEG_CODE U8 ModemRx3[] = {12, MODEM_AFC_GEAR_12};
- SEG_CODE U8 ModemRx4[] = {5, MODEM_AGC_CONTRL_5};
- SEG_CODE U8 ModemRx4_1[] = {7, MODEM_AGC_WINDOW_SIZE_7};
- SEG_CODE U8 ModemRx5[] = {9, MODEM_FSK4_GAIN1_9};
- SEG_CODE U8 ModemRx6[] = {8, MODEM_OOK_PDTC_8};
- SEG_CODE U8 ModemRx7[] = {8, MODEM_RAW_SEARCH_8};
- SEG_CODE U8 ModemRx8[] = {6, MODEM_ANT_DIV_MODE_6};
- SEG_CODE U8 ModemRx9[] = {13, MODEM_CHFLT_RX1_CHFLT_COE13_7_0_13};
- SEG_CODE U8 ModemRx10[] = {13, MODEM_CHFLT_RX1_CHFLT_COE4_7_0_13};
- SEG_CODE U8 ModemRx11[] = {13, MODEM_CHFLT_RX2_CHFLT_COE13_7_0_13};
- SEG_CODE U8 ModemRx12[] = {13, MODEM_CHFLT_RX2_CHFLT_COE4_7_0_13};
- SEG_CODE U8 ModemRx13[] = {5, MODEM_RSSI_COMP_5};
- /*****************************************************
- NRF24LE1軟件延時
- ******************************************************/
- void delay1ms(void)
- {
- unsigned int m;
- for(m=0;m<1260;m++) //m為1260時是1MS
- {
- _nop_();
- _nop_();
- }
- }
- void delayms(unsigned short dly) //ms延時
- {
- for(;dly>0;dly--)
- {
- delay1ms();
- }
- }
- void clk_init()
- {
- hal_clk_set_16m_source(HAL_CLK_XOSC16M); // Always run on 16MHz crystal oscillator
- hal_clklf_set_source(HAL_CLKLF_XOSC16M_SYNTH); // Synthesize 32 KHz from 16 MHz clock
- hal_clk_regret_xosc16m_on(true); // Keep XOSC16M on in register retention
- }
-
- void io_init(void)
- {
- P0DIR &=0xf0;
- P0CON = 0x60; // P0.0 - OK NSEL
- P0CON = 0x60; // P0.1 - OK SDN
- P0CON = 0x62; // P0.2 - OK LED1
- P0CON = 0x63; // P0.3 - OK LED2
- P0DIR |=0x40;
- P0CON = 0x56; // P0.6 - OK IRQ
- P1DIR &=0xCF;
-
- P1CON = 0x64; // P1.4 - SCK
- P1CON = 0x65; // P1.5 - MOSI
- P1DIR |=0x40;
- P1CON = 0x56; // P1.5 - MOSI
- }
- void main()
- {
- unsigned char i=0;
- SEGMENT_VARIABLE(wDelay, U16, SEG_XDATA);
- BIT fValidPacket;
-
- clk_init();
- while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M);
- io_init();
-
- EZRP_SDN = 1;
- // Wait ~300us (SDN pulse width)
- for(wDelay=0; wDelay<330; wDelay++);
- // Wake up the chip from SDN
- EZRP_SDN = 0;
-
- // Wait for POR (power on reset); ~5ms
- for(wDelay=0; wDelay<5500; wDelay++);
-
- // Start the radio
- abApi_Write[0] = CMD_POWER_UP; // Use API command to power up the radio IC
- abApi_Write[1] = 0x01; // Write global control registers
- abApi_Write[2] = 0x00; // Write global control registers
- bApi_SendCommand(3,abApi_Write); // Send command to the radio IC
- // Wait for boot
- if (vApi_WaitforCTS()) // Wait for CTS
- {
- while (1) {} // Stop if radio power-up error
- }
-
- // Read ITs, clear pending ones
- abApi_Write[0] = CMD_GET_INT_STATUS; // Use interrupt status command
- abApi_Write[1] = 0; // Clear PH_CLR_PEND
- abApi_Write[2] = 0; // Clear MODEM_CLR_PEND
- abApi_Write[3] = 0; // Clear CHIP_CLR_PEND
- bApi_SendCommand(4,abApi_Write); // Send command to the radio IC
- bApi_GetResponse(8, abApi_Read ); // Make sure that CTS is ready then get the response
-
- // Set TRX parameters of the radio IC; data retrieved from the WDS-generated modem_params.h header file
- bApi_SendCommand(ModemTrx1[0],&ModemTrx1[1]); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
- bApi_SendCommand(ModemTrx2[0],&ModemTrx2[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemTrx3[0],&ModemTrx3[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemTrx4[0],&ModemTrx4[1]);
- vApi_WaitforCTS();
-
- // Set Rx parameters of the radio IC
- bApi_SendCommand(ModemRx1[0],&ModemRx1[1]); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
- bApi_SendCommand(ModemRx2[0],&ModemRx2[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx3[0],&ModemRx3[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx4[0],&ModemRx4[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx4_1[0],&ModemRx4_1[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx5[0],&ModemRx5[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx6[0],&ModemRx6[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx7[0],&ModemRx7[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx8[0],&ModemRx8[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx9[0],&ModemRx9[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx10[0],&ModemRx10[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx11[0],&ModemRx11[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx12[0],&ModemRx12[1]);
- vApi_WaitforCTS();
- bApi_SendCommand(ModemRx13[0],&ModemRx13[1]);
- vApi_WaitforCTS();
-
- // Enable packet received and CRC interrupt only
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_INT_CTL_GROUP; // Select property group
- abApi_Write[2] = 4; // Number of properties to be written
- abApi_Write[3] = PROP_INT_CTL_ENABLE; // Specify property
- abApi_Write[4] = 0x01; // INT_CTL: PH interrupt enabled
- abApi_Write[5] = 0x18; // INT_CTL_PH: PH PACKET_RX interrupt enabled
- abApi_Write[6] = 0x00; // INT_CTL_MODEM: -
- abApi_Write[7] = 0x00; // INT_CTL_CHIP_EN: -
- bApi_SendCommand(8,abApi_Write); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Configure Fast response registers
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_FRR_CTL_GROUP; // Select property group
- abApi_Write[2] = 4; // Number of properties to be written
- abApi_Write[3] = PROP_FRR_CTL_A_MODE; // Specify property (1st)
- abApi_Write[4] = 0x04; // FRR A: PH IT pending
- abApi_Write[5] = 0x06; // FRR B: Modem IT pending
- abApi_Write[6] = 0x0A; // FRR C: Latched RSSI
- abApi_Write[7] = 0x00; // FRR D: disabled
- bApi_SendCommand(8,abApi_Write); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- //Set packet content
- //Set preamble length
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_PREAMBLE_GROUP; // Select property group
- abApi_Write[2] = 1; // Number of properties to be written
- abApi_Write[3] = PROP_PREAMBLE_CONFIG_STD_1; // Specify property
- abApi_Write[4] = 20; // 20 bits preamble detection threshold
- bApi_SendCommand(5,abApi_Write); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Set preamble pattern
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_PREAMBLE_GROUP; // Select property group
- abApi_Write[2] = 1; // Number of properties to be written
- abApi_Write[3] = PROP_PREAMBLE_CONFIG; // Specify property
- abApi_Write[4] = 0x31; // Use `1010` pattern, length defined in bytes
- bApi_SendCommand(5,abApi_Write); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Set sync word
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_SYNC_GROUP; // Select property group
- abApi_Write[2] = 3; // Number of properties to be written
- abApi_Write[3] = PROP_SYNC_CONFIG; // Specify property
- abApi_Write[4] = 0x01; // SYNC_CONFIG: 2 bytes sync word
- abApi_Write[5] = 0xB4; // SYNC_BITS_31_24: 1st sync byte: 0x2D; NOTE: LSB transmitted first!
- abApi_Write[6] = 0x2B; // SYNC_BITS_23_16: 2nd sync byte: 0xD4; NOTE: LSB transmitted first!
- bApi_SendCommand(7,abApi_Write); // Send command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // General packet config (set bit order)
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_PKT_GROUP; // Select property group
- abApi_Write[2] = 1; // Number of properties to be written
- abApi_Write[3] = PROP_PKT_CONFIG1; // Specify property
- abApi_Write[4] = 0x00; // Payload data goes MSB first
- bApi_SendCommand(5,abApi_Write); // Send command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Set packet fields (use only one field out of the available five)
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_PKT_GROUP; // Select property group
- abApi_Write[2] = 4; // Number of properties to be written
- abApi_Write[3] = PROP_PKT_FIELD_1_LENGTH_12_8; // Specify first property
- abApi_Write[4] = 0x00; // PKT_FIELD_1_LENGTH_12_8: 8 byte long packet field
- abApi_Write[5] = 0x10; // PKT_FIELD_1_LENGTH_7_0: 8 byte long packet field
- abApi_Write[6] = 0x00; // PKT_FIELD_1_CONFIG: No 4(G)FSK/Whitening/Manchester coding for this field
- abApi_Write[7] = 0x8A; // PKT_FIELD_1_CRC_CONFIG: Start CRC calc. from this field, check CRC at the end
- bApi_SendCommand(8,abApi_Write); // Send command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Configure CRC polynomial and seed
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_PKT_GROUP; // Select property group
- abApi_Write[2] = 1; // Number of properties to be written
- abApi_Write[3] = PROP_PKT_CRC_CONFIG; // Specify property
- abApi_Write[4] = 0x05; // CRC seed: all `0`s, poly: No. 5, 16bit; CCIT-16
- bApi_SendCommand(5,abApi_Write); // Send command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Set RSSI latch to sync word
- abApi_Write[0] = CMD_SET_PROPERTY; // Use property command
- abApi_Write[1] = PROP_MODEM_GROUP; // Select property group
- abApi_Write[2] = 1; // Number of properties to be written
- abApi_Write[3] = PROP_MODEM_RSSI_CONTROL; // Specify property
- abApi_Write[4] = 0x12; // RSSI average over 4 bits, latches at sync word detect
- bApi_SendCommand(5,abApi_Write); // Send API command to the radio IC
- vApi_WaitforCTS(); // Wait for CTS
-
- // Configure the GPIOs
- abApi_Write[0] = CMD_GPIO_PIN_CFG; // Use GPIO pin configuration command
- #ifdef ONE_SMA_WITH_RF_SWITCH
- // If RF switch is used
- // Select Tx state to GPIO2, Rx state to GPIO0
- abApi_Write[1] = 0x21; // Configure GPIO0 as Rx state
- abApi_Write[2] = 0x13; // Configure GPIO1 as Tx data
- abApi_Write[3] = 0x20; // Configure GPIO2 as Tx state
- abApi_Write[4] = 0x10; // Configure GPIO3 as Tx data CLK
- #else
-
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
定時發送16個字節數據.rar
(377.09 KB, 下載次數: 25)
2017-11-18 14:17 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|