找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1943|回复: 0
收起左侧

STC8A8K64S4A12单片机驱动nRF24L01发送/接收/半双工收发

[复制链接]
ID:912806 发表于 2021-11-13 16:36 | 显示全部楼层 |阅读模式
连线方式如代码注释所示. 基于STC12的nRF24L01代码修改. 通过修改CURRENT_SCEN的值可以切换发送, 接收和发送接收半双工方式
寄存器初始化方法使用python工具生成, 工具地址 https://github.com/IOsetting/stcmx

main.c
  1. /*****************************************************************************/
  2. /**
  3. * \file        spi_nrf24l01_tx.c
  4. * \brief       Example code of SPI driving NRF24L01 module in all scenarios
  5. * \note        Pin connection:
  6. *              P12(SS, Ignored) => CSN,
  7. *              P13(MOSI)        => MOSI,
  8. *              P14(MISO)        => MISO,
  9. *              P15(SPCLK)       => CLK,
  10. *              P32(INT0)        => IRQ,
  11. *              P37(IO)          => CE,
  12. *
  13. * \version     v0.1
  14. * \ingroup     example
  15. * \remarks     test-board: Minimum System; test-MCU: STC8A8K64S4A12
  16. *              In my test STC12 cannot achive the best performance of nRF24L01,
  17. *              you have to put 20~40ms delay betwen each sending, or error rate
  18. *              will increase dramatically.
  19. ******************************************************************************/

  20. #include "stc8a.h"
  21. #include "uart.h"
  22. #include "nrf24l01.h"
  23. #include "util.h"
  24. #include "spi.h"

  25. const NRF24_SCEN CURRENT_SCEN = NRF24_SCEN_HALF_DUPLEX;
  26. extern uint8_t idata xbuf[NRF24_PLOAD_WIDTH + 1];

  27. void clock_init()
  28. {
  29.   // [  BAH,0,0x00]: 外设端口切换控制寄存器2,串口2/3/4,I2C,比较器
  30.   P_SW2      = 0x80;
  31.   // [FE01H,1,0x00]: 时钟分频寄存器,ISP可能写入预设值
  32.   CLKDIV     = 0x00;
  33.   // [  9FH,0,0x00]: IRC频率调整寄存器, ISP可能写入预设值, 0x75:24MHz
  34.   IRTRIM     = 0x75;
  35.   // [  9EH,0,0x00]: IRC频率微调寄存器, ISP可能写入预设值
  36.   LIRTRIM    = 0x00;
  37.   // [  BAH,0,0x00]: 外设端口切换控制寄存器2,串口2/3/4,I2C,比较器
  38.   P_SW2      = 0x00;
  39. }

  40. void timer_init()
  41. {
  42.   // [  D6H,0,0x00]: 定时器2高字节
  43.   T2H        = 0xFF;
  44.   // [  D7H,0,0x00]: 定时器2低字节
  45.   T2L        = 0xCB;
  46.   // [  87H,0,0x30]: 电源控制寄存器
  47.   PCON       = 0xB0;
  48.   // [  8EH,0,0x01]: 辅助寄存器
  49.   AUXR       = 0x15;
  50. }

  51. void uart_init()
  52. {
  53.   // [  98H,0,0x00]: 串口1控制寄存器
  54.   SCON       = 0x50;
  55.   // [  87H,0,0x30]: 电源控制寄存器
  56.   PCON       = 0xB0;
  57.   // [  8EH,0,0x01]: 辅助寄存器
  58.   AUXR       = 0x15;
  59. }

  60. void spi_init()
  61. {
  62.   // [  CEH,0,0x00]: SPI控制
  63.   SPCTL      = 0xD0;
  64. }

  65. void int_init()
  66. {
  67.   IT0 = 1; // 低电平触发
  68.   EX0 = 1; // 使能INT0中断
  69.   EA = 1; // 使能全局中断
  70. }

  71. INTERRUPT(int0_isr, 0)
  72. {
  73.   NRF24L01_handelIrqFlag(xbuf);
  74. }

  75. void main(void)
  76. {
  77.   uint8_t sta;
  78.   uint8_t code tmp[] = {
  79.     0x1F, 0x80, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  80.     0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28,
  81.     0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38,
  82.     0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x37, 0x48};

  83.   clock_init();
  84.   timer_init();
  85.   uart_init();
  86.   PrintString("UART Initialized\r\n");
  87.   spi_init();
  88.   PrintString("SPI Initialized\r\n");
  89.   
  90.   while (NRF24L01_check() == 1)
  91.   {
  92.     PrintString("Check failed\r\n");
  93.     Delay1000ms();
  94.   }
  95.   PrintString("NRF24L01 Checked\r\n");

  96.   switch (CURRENT_SCEN)
  97.   {
  98.   case NRF24_SCEN_TX:
  99.       NRF24L01_init(NRF24_MODE_TX);
  100.       PrintString("NRF24L01 Initialized\r\n");
  101.       //int_init();
  102.       while (1)
  103.       {
  104.           if (NRF24L01_writeFast(tmp) == 0)
  105.           {
  106.             PrintChar('E');
  107.           }
  108.           else
  109.           {
  110.             PrintChar('.');
  111.           }
  112.           Delay30ms();
  113.       }
  114.       break;

  115.   case NRF24_SCEN_RX:
  116.       NRF24L01_init(NRF24_MODE_RX);
  117.       int_init();
  118.       while(1);
  119.       break;

  120.   case NRF24_SCEN_HALF_DUPLEX:
  121.       NRF24L01_init(NRF24_MODE_RX);
  122.       int_init();
  123.       while (1)
  124.       {
  125.           NRF24L01_tx(tmp);
  126.           Delay1000ms();
  127.       }
  128.       break;

  129.   default:
  130.       PrintString("Unknown scen\r\n");
  131.       break;
  132.   }
  133. }
复制代码
51hei.png
完整Keil代码下载:
stc8a8k64s4_test02.zip (17 KB, 下载次数: 28)

评分

参与人数 1黑币 +30 收起 理由
admin + 30 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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