找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3244|回复: 0
打印 上一主题 下一主题
收起左侧

NRF24LE1做主控控制SI4463无线模块的收发代码

[复制链接]
跳转到指定楼层
楼主
如题,希望能帮助到大家,
定时发送16个字节数据,SI4463无线发送与接收的源码都有

单片机源程序如下:
  1. #include <reg24le1.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <intrins.h>
  6. #include "hal_clk.h"

  7. #include "compiler_defs.h"
  8. #include "hardware_defs.h"
  9. #include "spi.h"
  10. #include "Si446x_B1_defs.h"
  11. #include "ezrp_next_api.h"
  12. #include "modem_params.h"

  13. // Set up modem parameters database; data is retrieved from modem_params.h header file which is
  14. // automatically generated by the WDS (Wireless Development Suite)
  15. SEG_CODE U8 ModemTrx1[] = {7, MODEM_MOD_TYPE_7};
  16. SEG_CODE U8 ModemTrx2[] = {5, MODEM_CLKGEN_BAND_5};
  17. SEG_CODE U8 ModemTrx3[] = {11, SYNTH_PFDCP_CPFF_11};
  18. SEG_CODE U8 ModemTrx4[] = {12, FREQ_CONTROL_INTE_12};

  19. SEG_CODE U8 ModemRx1[] = {11, MODEM_MDM_CTRL_11};
  20. SEG_CODE U8 ModemRx2[] = {14, MODEM_BCR_OSR_1_14};
  21. SEG_CODE U8 ModemRx3[] = {12, MODEM_AFC_GEAR_12};
  22. SEG_CODE U8 ModemRx4[] = {5, MODEM_AGC_CONTRL_5};
  23. SEG_CODE U8 ModemRx4_1[] = {7, MODEM_AGC_WINDOW_SIZE_7};
  24. SEG_CODE U8 ModemRx5[] = {9, MODEM_FSK4_GAIN1_9};
  25. SEG_CODE U8 ModemRx6[] = {8, MODEM_OOK_PDTC_8};
  26. SEG_CODE U8 ModemRx7[] = {8, MODEM_RAW_SEARCH_8};
  27. SEG_CODE U8 ModemRx8[] = {6, MODEM_ANT_DIV_MODE_6};
  28. SEG_CODE U8 ModemRx9[] = {13, MODEM_CHFLT_RX1_CHFLT_COE13_7_0_13};
  29. SEG_CODE U8 ModemRx10[] = {13, MODEM_CHFLT_RX1_CHFLT_COE4_7_0_13};
  30. SEG_CODE U8 ModemRx11[] = {13, MODEM_CHFLT_RX2_CHFLT_COE13_7_0_13};
  31. SEG_CODE U8 ModemRx12[] = {13, MODEM_CHFLT_RX2_CHFLT_COE4_7_0_13};
  32. SEG_CODE U8 ModemRx13[] = {5, MODEM_RSSI_COMP_5};



  33. /*****************************************************
  34. NRF24LE1软件延时
  35. ******************************************************/
  36. void delay1ms(void)
  37. {
  38.         unsigned int m;
  39.         for(m=0;m<1260;m++)                //m为1260时是1MS
  40.         {
  41.            _nop_();
  42.            _nop_();
  43.         }
  44. }

  45. void delayms(unsigned short dly)       //ms延时
  46. {
  47.         for(;dly>0;dly--)
  48.         {
  49.            delay1ms();
  50.         }
  51. }

  52. void clk_init()
  53. {
  54.     hal_clk_set_16m_source(HAL_CLK_XOSC16M);            // Always run on 16MHz crystal oscillator
  55.     hal_clklf_set_source(HAL_CLKLF_XOSC16M_SYNTH);      // Synthesize 32 KHz from 16 MHz clock  
  56.     hal_clk_regret_xosc16m_on(true);                    // Keep XOSC16M on in register retention
  57. }
  58.         
  59. void io_init(void)
  60. {
  61.         P0DIR &=0xf0;

  62.         P0CON = 0x60;                        // P0.0 - OK NSEL
  63.         P0CON = 0x60;                        // P0.1 - OK SDN
  64.         P0CON = 0x62;                        // P0.2 - OK LED1
  65.         P0CON = 0x63;                        // P0.3 - OK LED2

  66.         P0DIR |=0x40;
  67.         P0CON = 0x56;                        // P0.6 - OK IRQ

  68.         P1DIR &=0xCF;
  69.         
  70.         P1CON = 0x64;                        // P1.4 - SCK
  71.         P1CON = 0x65;                        // P1.5 - MOSI

  72.         P1DIR |=0x40;
  73.         P1CON = 0x56;                        // P1.5 - MOSI               
  74. }


  75. void main()
  76. {        
  77.         unsigned char i=0;        
  78.         SEGMENT_VARIABLE(wDelay, U16, SEG_XDATA);
  79.         BIT fValidPacket;
  80.         
  81.         clk_init();
  82.         while(hal_clk_get_16m_source() != HAL_CLK_XOSC16M);
  83.         io_init();
  84.                
  85.         EZRP_SDN = 1;
  86.         // Wait ~300us (SDN pulse width)
  87.         for(wDelay=0; wDelay<330; wDelay++);
  88.         // Wake up the chip from SDN
  89.         EZRP_SDN = 0;
  90.         
  91.         // Wait for POR (power on reset); ~5ms
  92.         for(wDelay=0; wDelay<5500; wDelay++);
  93.         
  94.         // Start the radio
  95.         abApi_Write[0] = CMD_POWER_UP;          // Use API command to power up the radio IC
  96.         abApi_Write[1] = 0x01;              // Write global control registers
  97.         abApi_Write[2] = 0x00;              // Write global control registers
  98.         bApi_SendCommand(3,abApi_Write);        // Send command to the radio IC
  99.         // Wait for boot
  100.         if (vApi_WaitforCTS())                // Wait for CTS
  101.         {
  102.                 while (1) {}    // Stop if radio power-up error
  103.         }
  104.         
  105.         // Read ITs, clear pending ones
  106.         abApi_Write[0] = CMD_GET_INT_STATUS;      // Use interrupt status command
  107.         abApi_Write[1] = 0;               // Clear PH_CLR_PEND
  108.         abApi_Write[2] = 0;               // Clear MODEM_CLR_PEND
  109.         abApi_Write[3] = 0;               // Clear CHIP_CLR_PEND
  110.         bApi_SendCommand(4,abApi_Write);        // Send command to the radio IC
  111.         bApi_GetResponse(8, abApi_Read );         // Make sure that CTS is ready then get the response
  112.         
  113.         // Set TRX parameters of the radio IC; data retrieved from the WDS-generated modem_params.h header file
  114.         bApi_SendCommand(ModemTrx1[0],&ModemTrx1[1]);     // Send API command to the radio IC
  115.         vApi_WaitforCTS();                    // Wait for CTS
  116.         bApi_SendCommand(ModemTrx2[0],&ModemTrx2[1]);
  117.         vApi_WaitforCTS();
  118.         bApi_SendCommand(ModemTrx3[0],&ModemTrx3[1]);
  119.         vApi_WaitforCTS();
  120.         bApi_SendCommand(ModemTrx4[0],&ModemTrx4[1]);
  121.         vApi_WaitforCTS();
  122.         
  123.         // Set Rx parameters of the radio IC
  124.         bApi_SendCommand(ModemRx1[0],&ModemRx1[1]);       // Send API command to the radio IC
  125.         vApi_WaitforCTS();                    // Wait for CTS
  126.         bApi_SendCommand(ModemRx2[0],&ModemRx2[1]);
  127.         vApi_WaitforCTS();
  128.         bApi_SendCommand(ModemRx3[0],&ModemRx3[1]);
  129.         vApi_WaitforCTS();
  130.         bApi_SendCommand(ModemRx4[0],&ModemRx4[1]);
  131.         vApi_WaitforCTS();
  132.         bApi_SendCommand(ModemRx4_1[0],&ModemRx4_1[1]);
  133.         vApi_WaitforCTS();
  134.         bApi_SendCommand(ModemRx5[0],&ModemRx5[1]);
  135.         vApi_WaitforCTS();
  136.         bApi_SendCommand(ModemRx6[0],&ModemRx6[1]);
  137.         vApi_WaitforCTS();
  138.         bApi_SendCommand(ModemRx7[0],&ModemRx7[1]);
  139.         vApi_WaitforCTS();
  140.         bApi_SendCommand(ModemRx8[0],&ModemRx8[1]);
  141.         vApi_WaitforCTS();
  142.         bApi_SendCommand(ModemRx9[0],&ModemRx9[1]);
  143.         vApi_WaitforCTS();
  144.         bApi_SendCommand(ModemRx10[0],&ModemRx10[1]);
  145.         vApi_WaitforCTS();
  146.         bApi_SendCommand(ModemRx11[0],&ModemRx11[1]);
  147.         vApi_WaitforCTS();
  148.         bApi_SendCommand(ModemRx12[0],&ModemRx12[1]);
  149.         vApi_WaitforCTS();
  150.         bApi_SendCommand(ModemRx13[0],&ModemRx13[1]);
  151.         vApi_WaitforCTS();
  152.         
  153.         // Enable packet received and CRC interrupt only
  154.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  155.         abApi_Write[1] = PROP_INT_CTL_GROUP;      // Select property group
  156.         abApi_Write[2] = 4;               // Number of properties to be written
  157.         abApi_Write[3] = PROP_INT_CTL_ENABLE;     // Specify property
  158.         abApi_Write[4] = 0x01;              // INT_CTL: PH interrupt enabled
  159.         abApi_Write[5] = 0x18;              // INT_CTL_PH: PH PACKET_RX interrupt enabled
  160.         abApi_Write[6] = 0x00;              // INT_CTL_MODEM: -
  161.         abApi_Write[7] = 0x00;              // INT_CTL_CHIP_EN: -
  162.         bApi_SendCommand(8,abApi_Write);        // Send API command to the radio IC
  163.         vApi_WaitforCTS();                // Wait for CTS
  164.         
  165.         // Configure Fast response registers
  166.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  167.         abApi_Write[1] = PROP_FRR_CTL_GROUP;      // Select property group
  168.         abApi_Write[2] = 4;               // Number of properties to be written
  169.         abApi_Write[3] = PROP_FRR_CTL_A_MODE;     // Specify property (1st)
  170.         abApi_Write[4] = 0x04;              // FRR A: PH IT pending
  171.         abApi_Write[5] = 0x06;              // FRR B: Modem IT pending
  172.         abApi_Write[6] = 0x0A;              // FRR C: Latched RSSI
  173.         abApi_Write[7] = 0x00;              // FRR D: disabled
  174.         bApi_SendCommand(8,abApi_Write);        // Send API command to the radio IC
  175.         vApi_WaitforCTS();                // Wait for CTS
  176.         
  177.         //Set packet content
  178.         //Set preamble length
  179.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  180.         abApi_Write[1] = PROP_PREAMBLE_GROUP;     // Select property group
  181.         abApi_Write[2] = 1;               // Number of properties to be written
  182.         abApi_Write[3] = PROP_PREAMBLE_CONFIG_STD_1;  // Specify property
  183.         abApi_Write[4] = 20;              // 20 bits preamble detection threshold
  184.         bApi_SendCommand(5,abApi_Write);        // Send API command to the radio IC
  185.         vApi_WaitforCTS();                // Wait for CTS
  186.         
  187.         // Set preamble pattern
  188.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  189.         abApi_Write[1] = PROP_PREAMBLE_GROUP;     // Select property group
  190.         abApi_Write[2] = 1;               // Number of properties to be written
  191.         abApi_Write[3] = PROP_PREAMBLE_CONFIG;      // Specify property
  192.         abApi_Write[4] = 0x31;              // Use `1010` pattern, length defined in bytes
  193.         bApi_SendCommand(5,abApi_Write);        // Send API command to the radio IC
  194.         vApi_WaitforCTS();                // Wait for CTS
  195.         
  196.         // Set sync word
  197.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  198.         abApi_Write[1] = PROP_SYNC_GROUP;       // Select property group
  199.         abApi_Write[2] = 3;               // Number of properties to be written
  200.         abApi_Write[3] = PROP_SYNC_CONFIG;        // Specify property
  201.         abApi_Write[4] = 0x01;              // SYNC_CONFIG: 2 bytes sync word
  202.         abApi_Write[5] = 0xB4;              // SYNC_BITS_31_24: 1st sync byte: 0x2D; NOTE: LSB transmitted first!
  203.         abApi_Write[6] = 0x2B;              // SYNC_BITS_23_16: 2nd sync byte: 0xD4; NOTE: LSB transmitted first!
  204.         bApi_SendCommand(7,abApi_Write);        // Send command to the radio IC
  205.         vApi_WaitforCTS();                // Wait for CTS
  206.         
  207.         // General packet config (set bit order)
  208.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  209.         abApi_Write[1] = PROP_PKT_GROUP;        // Select property group
  210.         abApi_Write[2] = 1;               // Number of properties to be written
  211.         abApi_Write[3] = PROP_PKT_CONFIG1;        // Specify property
  212.         abApi_Write[4] = 0x00;              // Payload data goes MSB first
  213.         bApi_SendCommand(5,abApi_Write);        // Send command to the radio IC
  214.         vApi_WaitforCTS();                // Wait for CTS
  215.         
  216.         // Set packet fields (use only one field out of the available five)
  217.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  218.         abApi_Write[1] = PROP_PKT_GROUP;        // Select property group
  219.         abApi_Write[2] = 4;               // Number of properties to be written
  220.         abApi_Write[3] = PROP_PKT_FIELD_1_LENGTH_12_8;  // Specify first property
  221.         abApi_Write[4] = 0x00;              // PKT_FIELD_1_LENGTH_12_8: 8 byte long packet field
  222.         abApi_Write[5] = 0x10;              // PKT_FIELD_1_LENGTH_7_0: 8 byte long packet field
  223.         abApi_Write[6] = 0x00;              // PKT_FIELD_1_CONFIG: No 4(G)FSK/Whitening/Manchester coding for this field
  224.         abApi_Write[7] = 0x8A;              // PKT_FIELD_1_CRC_CONFIG: Start CRC calc. from this field, check CRC at the end
  225.         bApi_SendCommand(8,abApi_Write);        // Send command to the radio IC
  226.         vApi_WaitforCTS();                // Wait for CTS
  227.         
  228.         // Configure CRC polynomial and seed
  229.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  230.         abApi_Write[1] = PROP_PKT_GROUP;        // Select property group
  231.         abApi_Write[2] = 1;               // Number of properties to be written
  232.         abApi_Write[3] = PROP_PKT_CRC_CONFIG;     // Specify property
  233.         abApi_Write[4] = 0x05;              // CRC seed: all `0`s, poly: No. 5, 16bit; CCIT-16
  234.         bApi_SendCommand(5,abApi_Write);        // Send command to the radio IC
  235.         vApi_WaitforCTS();                // Wait for CTS
  236.         
  237.         // Set RSSI latch to sync word
  238.         abApi_Write[0] = CMD_SET_PROPERTY;        // Use property command
  239.         abApi_Write[1] = PROP_MODEM_GROUP;        // Select property group
  240.         abApi_Write[2] = 1;               // Number of properties to be written
  241.         abApi_Write[3] = PROP_MODEM_RSSI_CONTROL;   // Specify property
  242.         abApi_Write[4] = 0x12;              // RSSI average over 4 bits, latches at sync word detect
  243.         bApi_SendCommand(5,abApi_Write);        // Send API command to the radio IC
  244.         vApi_WaitforCTS();                // Wait for CTS
  245.         
  246.         // Configure the GPIOs
  247.         abApi_Write[0] = CMD_GPIO_PIN_CFG;        // Use GPIO pin configuration command
  248. #ifdef ONE_SMA_WITH_RF_SWITCH
  249.   // If RF switch is used
  250.   // Select Tx state to GPIO2, Rx state to GPIO0
  251.         abApi_Write[1] = 0x21;              // Configure GPIO0 as Rx state
  252.         abApi_Write[2] = 0x13;              // Configure GPIO1 as Tx data
  253.         abApi_Write[3] = 0x20;              // Configure GPIO2 as Tx state
  254.         abApi_Write[4] = 0x10;              // Configure GPIO3 as Tx data CLK
  255. #else
  256.         
  257. ……………………

  258. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
定时发送16个字节数据.rar (377.09 KB, 下载次数: 25)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表