[DemoCode下载] M051 SPI接口驱动NRF24L01P模块

[复制链接]
 楼主| wanduzi 发表于 2020-3-29 11:58 | 显示全部楼层 |阅读模式
EC_M051_SPI_NRF24L01P_V1.00.zip (1.41 MB, 下载次数: 8)
324415e801cf16ed7e.png
 楼主| wanduzi 发表于 2020-3-29 12:01 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.0
  4. * $Revision: 2 $
  5. * $Date: 8/20/19 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    RF24L01 Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"
  13. #include "NRF_24L01.h"
  14. /*---------------------------------------------------------------------------*/
  15. /* Define                                                                    */
  16. /*---------------------------------------------------------------------------*/
  17. #define TEST_COUNT             64
  18. /*---------------------------------------------------------------------------*/
  19. /* Global variables                                                          */
  20. /*---------------------------------------------------------------------------*/
  21. uint32_t g_au32SourceData[TEST_COUNT];
  22. uint32_t g_au32DestinationData[TEST_COUNT];
  23. uint32_t u32TestRevCount = 0;
  24. uint8_t u8RevFlag = 0;

  25. void delay_ms(int32_t ms)
  26. {
  27.     int32_t i;

  28.     for (i = 0; i < ms; i++)
  29.     {
  30.         CLK_SysTickDelay(1000);/* SysTick to generate the delay time and the UNIT is in us.  */
  31.     }
  32. }

  33. void TMR0_IRQHandler(void)
  34. {
  35.     /* Clear Timer0 time-out interrupt flag */
  36.     TIMER_ClearIntFlag(TIMER0);

  37.     if (u8RevFlag == 1)
  38.     {
  39.         u8RevFlag = 0;
  40.     }
  41.     else
  42.     {
  43.         rf_ChangeCH(1);
  44.     }
  45. }

  46. void EINT0_IRQHandler(void)
  47. {
  48.     uint8_t u8Status = 0;

  49.     /* For P3.2, clear the INT flag */
  50.     GPIO_CLR_INT_FLAG(P3, BIT2);
  51.     u8Status = SPI_Read(RF_STATUS);

  52.     if (u8Status & STA_MARK_RX)
  53.     {
  54.         SPI_Read_Buf(RD_RX_PLOAD, au8rx_buf, TX_PLOAD_WIDTH);

  55.         if (au8rx_buf[0] == 0xAA)
  56.         {
  57.             u32TestRevCount++;
  58.             LED = ~LED;
  59.         }

  60.         au8rx_buf[0] = 0;
  61.         u8RevFlag = 1;
  62.     }

  63.     SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);
  64. }

  65. void SYS_Init(void)
  66. {
  67.     /* Unlock protected registers */
  68.     SYS_UnlockReg();

  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Init System Clock                                                                                       */
  71.     /*---------------------------------------------------------------------------------------------------------*/

  72.     /* Enable external 12MHz XTAL */
  73.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  74.     /* Waiting for clock ready */
  75.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  76.     /* Switch HCLK clock source to HXT and HCLK source divide 1 */
  77.     CLK_SetCoreClock(FREQ_50MHZ);

  78.     /* Select HXT as the clock source of UART0 */
  79.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  80.     /* Select HCLK as the clock source of SPI0 */
  81.     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);

  82.     /* Enable UART peripheral clock */
  83.     CLK_EnableModuleClock(UART0_MODULE);

  84.     /* Enable SPI0 peripheral clock */
  85.     CLK_EnableModuleClock(SPI0_MODULE);

  86.     /* Select HXT as the clock source of TIME0 */
  87.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
  88.     /* Enable TIME peripheral clock */
  89.     CLK_EnableModuleClock(TMR0_MODULE);

  90.     /*---------------------------------------------------------------------------------------------------------*/
  91.     /* Init I/O Multi-function                                                                                 */
  92.     /*---------------------------------------------------------------------------------------------------------*/

  93.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  94.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  95.     /* Setup SPI0 multi-function pins */
  96.     SYS->P1_MFP = SYS_MFP_P14_SPISS0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0;

  97.     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  98.     SYS->P3_MFP &= ~(SYS_MFP_P32_Msk);
  99.     SYS->P3_MFP |= (SYS_MFP_P32_INT0);

  100.     /* Lock protected registers */
  101.     SYS_LockReg();

  102.     /* Update System Core Clock */
  103.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
  104.     SystemCoreClockUpdate();
  105. }

  106. void SPI_Init(void)
  107. {
  108.     /* Configure as a master, clock idle low, 32-bit transaction,
  109.      * drive output on falling clock edge and latch input on rising edge.
  110.      */
  111.     /* Set IP clock divider. SPI clock rate = 2MHz */
  112.     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, 1000000);

  113.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  114.     SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
  115.     SPI_DisableAutoSS(SPI0);
  116. }

  117. int main(void)
  118. {

  119.     /* Init System, IP clock and multi-function I/O.
  120.      *   In the end of SYS_Init() will issue SYS_LockReg()
  121.      *   to lock protected register. If user want to write
  122.      *   protected register, please issue SYS_UnlockReg()
  123.      *   to unlock protected register if necessary.
  124.     */
  125.     SYS_Init();

  126.     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
  127.     UART_Open(UART0, 115200);

  128.     /* Init SPI */
  129.     SPI_Init();

  130.     GPIO_SetMode(P0, BIT6, GPIO_PMD_OUTPUT);
  131.     GPIO_SetMode(P3, BIT6, GPIO_PMD_OUTPUT);

  132.     /* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
  133.     GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);

  134.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 * 10 KHz clock */
  135.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_HCLK, GPIO_DBCLKSEL_32);
  136.     GPIO_ENABLE_DEBOUNCE(P3, BIT2);

  137.     GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
  138.     NVIC_EnableIRQ(EINT0_IRQn);

  139.     /* Open Timer0 frequency to 0.5 Hz in periodic mode, and enable interrupt */
  140.     TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 20);     //50ms
  141.     TIMER_EnableInt(TIMER0);
  142.     NVIC_EnableIRQ(TMR0_IRQn);
  143.     TIMER_Start(TIMER0);

  144.     delay_ms(100);
  145.     init_nrf24l01_io();
  146.     delay_ms(500);

  147.     printf("Receive READY!\r\n");
  148.     ifnnrf_CLERN_ALL();
  149.     ifnnrf_rx_mode();

  150.     u8sta = SPI_Read(RF_SETUP);
  151.     u8sta = SPI_Read(RF_CH);
  152.     u8sta = SPI_Read(EN_AA);
  153.     u8sta = SPI_Read(RF_SETUP);
  154.     ifnnrf_rx_mode();

  155.     while (1)
  156.     {
  157.         u8sta = 0;
  158.         delay_ms(1000);
  159.         printf("u8CurCH=%d,u32TestRevCount=%d\n\r", u8CurCH, u32TestRevCount);

  160.     }
  161. }

  162. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/

 楼主| wanduzi 发表于 2020-3-29 12:04 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     main.c
  3. * @version  V1.0
  4. * $Revision: 1 $
  5. * $Date: 20/08/19 11:44a $
  6. * @brief    RF24L01 Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"
  13. #include "NRF_24L01.h"
  14. /*---------------------------------------------------------------------------*/
  15. /* Define                                                                    */
  16. /*---------------------------------------------------------------------------*/
  17. #define TEST_COUNT             64
  18. /*---------------------------------------------------------------------------*/
  19. /* Global variables                                                          */
  20. /*---------------------------------------------------------------------------*/
  21. uint32_t g_au32SourceData[TEST_COUNT];
  22. uint32_t g_au32DestinationData[TEST_COUNT];
  23. uint8_t u8TestData = 10;
  24. uint8_t u8SendDataState = 0;
  25. /*---------------------------------------------------------------------------*/
  26. /* Functions                                                                 */
  27. /*---------------------------------------------------------------------------*/
  28. void delay_ms(int32_t ms)
  29. {
  30.     int32_t i;

  31.     for (i = 0; i < ms; i++)
  32.     {
  33.         CLK_SysTickDelay(1000);/* SysTick to generate the delay time and the UNIT is in us.  */
  34.     }
  35. }

  36. void EINT0_IRQHandler(void)
  37. {
  38.     uint8_t u8Status = 0;
  39.     /* For P3.2, clear the INT flag */
  40.     GPIO_CLR_INT_FLAG(P3, BIT2);
  41.     u8Status = SPI_Read(RF_STATUS);

  42.     SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);

  43.     if (u8Status & STA_MARK_TX)
  44.     {
  45.         u8SendDataState = HAS_SENDSTATE;
  46.     }
  47.     else if (u8Status & STA_MARK_MX)
  48.     {
  49.         u8SendDataState = TIMEOUT_SENDSTATE;
  50.     }
  51. }

  52. void SYS_Init(void)
  53. {
  54.     /* Unlock protected registers */
  55.     SYS_UnlockReg();

  56.     /*---------------------------------------------------------------------------------------------------------*/
  57.     /* Init System Clock                                                                                       */
  58.     /*---------------------------------------------------------------------------------------------------------*/

  59.     /* Enable external 12MHz XTAL */
  60.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  61.     /* Waiting for clock ready */
  62.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  63.     /* Switch HCLK clock source to HXT and HCLK source divide 1 */
  64.     CLK_SetCoreClock(FREQ_50MHZ);

  65.     /* Select HXT as the clock source of UART0 */
  66.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  67.     /* Select HCLK as the clock source of SPI0 */
  68.     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);

  69.     /* Enable UART peripheral clock */
  70.     CLK_EnableModuleClock(UART0_MODULE);
  71.     /* Enable SPI0 peripheral clock */
  72.     CLK_EnableModuleClock(SPI0_MODULE);

  73.     /*---------------------------------------------------------------------------------------------------------*/
  74.     /* Init I/O Multi-function                                                                                 */
  75.     /*---------------------------------------------------------------------------------------------------------*/

  76.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  77.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  78.     /* Setup SPI0 multi-function pins */
  79.     SYS->P1_MFP = SYS_MFP_P14_SPISS0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0;

  80.     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  81.     SYS->P3_MFP &= ~(SYS_MFP_P32_Msk);
  82.     SYS->P3_MFP |= (SYS_MFP_P32_INT0);

  83.     /* Lock protected registers */
  84.     SYS_LockReg();

  85.     /* Update System Core Clock */
  86.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CycylesPerUs automatically. */
  87.     SystemCoreClockUpdate();
  88. }

  89. void SPI_Init(void)
  90. {
  91.     /* Configure as a master, clock idle low, 32-bit transaction,
  92.      * drive output on falling clock edge and latch input on rising edge.
  93.      */
  94.     /* Set IP clock divider. SPI clock rate = 2MHz */
  95.     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, 1000000);

  96.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  97.     SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
  98.     SPI_DisableAutoSS(SPI0);
  99. }

  100. int main(void)
  101. {

  102.     /* Init System, IP clock and multi-function I/O.
  103.      *   In the end of SYS_Init() will issue SYS_LockReg()
  104.      *   to lock protected register. If user want to write
  105.      *   protected register, please issue SYS_UnlockReg()
  106.      *   to unlock protected register if necessary.
  107.      */
  108.     SYS_Init();

  109.     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
  110.     UART_Open(UART0, 115200);

  111.     /* Init SPI */
  112.     SPI_Init();

  113.     GPIO_SetMode(P0, BIT6, GPIO_PMD_OUTPUT);
  114.     GPIO_SetMode(P3, BIT6, GPIO_PMD_OUTPUT);

  115.     /* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
  116.     GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);

  117.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 * 10 KHz clock */
  118.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_HCLK, GPIO_DBCLKSEL_32);
  119.     GPIO_ENABLE_DEBOUNCE(P3, BIT2);

  120.     GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
  121.     NVIC_EnableIRQ(EINT0_IRQn);

  122.     u8SendDataState = 0;

  123.     delay_ms(100);
  124.     init_nrf24l01_io();
  125.     delay_ms(500);

  126.     printf("Send READY!\r\n");
  127.     ifnnrf_CLERN_ALL();
  128.     ifnnrf_rx_mode();

  129.     u8sta = SPI_Read(RF_SETUP);
  130.     u8sta = SPI_Read(RF_CH);
  131.     u8sta = SPI_Read(EN_AA);
  132.     u8sta = SPI_Read(RF_SETUP);

  133.     SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);

  134.     au8tx_buf[0] = 0xAA;
  135.     au8tx_buf[1] = 0x55;
  136.     au8tx_buf[2] = 0x01;

  137.     if (rf_CheckCD())
  138.     {
  139.         rf_ChangeCH(0);
  140.     }

  141.     ifnnrf_tx_mode();

  142.     while (1)
  143.     {
  144.         if (u8SendDataState == HAS_SENDSTATE)
  145.         {
  146.             u8SendDataState = NOT_SENDSTATE;
  147.             au8tx_buf[0] = 0xAA;
  148.             au8tx_buf[1] = u8TestData++;
  149.             au8tx_buf[2] = u8TestData;

  150.             if (u8TestData == 0xff)
  151.             {
  152.                 u8TestData = 0;
  153.             }

  154.             LED = ~LED;
  155.             ifnnrf_tx_mode();
  156.         }
  157.         else if (u8SendDataState == TIMEOUT_SENDSTATE)
  158.         {
  159.             u8SendDataState = NOT_SENDSTATE;

  160.             while (rf_CheckCD())
  161.             {
  162.                 rf_ChangeCH(0);
  163.             }

  164.             ifnnrf_tx_mode();
  165.         }
  166.     }
  167. }

  168. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/

heisexingqisi 发表于 2020-3-29 13:20 | 显示全部楼层
没有这个模块,先看看。
磨砂 发表于 2020-4-6 15:46 | 显示全部楼层
非常感谢楼主分享
晓伍 发表于 2020-4-6 15:47 | 显示全部楼层
非常感谢楼主分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

154

主题

1857

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部