打印
[DemoCode下载]

M051 SPI接口驱动NRF24L01P模块

[复制链接]
576|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wanduzi|  楼主 | 2020-3-29 11:58 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
EC_M051_SPI_NRF24L01P_V1.00.zip (1.41 MB)

使用特权

评论回复
沙发
wanduzi|  楼主 | 2020-3-29 12:01 | 只看该作者
/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V1.0
* $Revision: 2 $
* $Date: 8/20/19 11:44a $
* [url=home.php?mod=space&uid=247401]@brief[/url]    RF24L01 Sample Code
*
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#include "NRF_24L01.h"
/*---------------------------------------------------------------------------*/
/* Define                                                                    */
/*---------------------------------------------------------------------------*/
#define TEST_COUNT             64
/*---------------------------------------------------------------------------*/
/* Global variables                                                          */
/*---------------------------------------------------------------------------*/
uint32_t g_au32SourceData[TEST_COUNT];
uint32_t g_au32DestinationData[TEST_COUNT];
uint32_t u32TestRevCount = 0;
uint8_t u8RevFlag = 0;

void delay_ms(int32_t ms)
{
    int32_t i;

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

void TMR0_IRQHandler(void)
{
    /* Clear Timer0 time-out interrupt flag */
    TIMER_ClearIntFlag(TIMER0);

    if (u8RevFlag == 1)
    {
        u8RevFlag = 0;
    }
    else
    {
        rf_ChangeCH(1);
    }
}

void EINT0_IRQHandler(void)
{
    uint8_t u8Status = 0;

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

    if (u8Status & STA_MARK_RX)
    {
        SPI_Read_Buf(RD_RX_PLOAD, au8rx_buf, TX_PLOAD_WIDTH);

        if (au8rx_buf[0] == 0xAA)
        {
            u32TestRevCount++;
            LED = ~LED;
        }

        au8rx_buf[0] = 0;
        u8RevFlag = 1;
    }

    SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);
}

void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

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

    /* Waiting for clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

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

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

    /* Enable UART peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Enable SPI0 peripheral clock */
    CLK_EnableModuleClock(SPI0_MODULE);

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

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

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

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

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

    /* Lock protected registers */
    SYS_LockReg();

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

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

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

int main(void)
{

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

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

    /* Init SPI */
    SPI_Init();

    GPIO_SetMode(P0, BIT6, GPIO_PMD_OUTPUT);
    GPIO_SetMode(P3, BIT6, GPIO_PMD_OUTPUT);

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

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

    GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
    NVIC_EnableIRQ(EINT0_IRQn);

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

    delay_ms(100);
    init_nrf24l01_io();
    delay_ms(500);

    printf("Receive READY!\r\n");
    ifnnrf_CLERN_ALL();
    ifnnrf_rx_mode();

    u8sta = SPI_Read(RF_SETUP);
    u8sta = SPI_Read(RF_CH);
    u8sta = SPI_Read(EN_AA);
    u8sta = SPI_Read(RF_SETUP);
    ifnnrf_rx_mode();

    while (1)
    {
        u8sta = 0;
        delay_ms(1000);
        printf("u8CurCH=%d,u32TestRevCount=%d\n\r", u8CurCH, u32TestRevCount);

    }
}

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

使用特权

评论回复
板凳
wanduzi|  楼主 | 2020-3-29 12:04 | 只看该作者
/******************************************************************************
* @file     main.c
* @version  V1.0
* $Revision: 1 $
* $Date: 20/08/19 11:44a $
* @brief    RF24L01 Sample Code
*
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#include "NRF_24L01.h"
/*---------------------------------------------------------------------------*/
/* Define                                                                    */
/*---------------------------------------------------------------------------*/
#define TEST_COUNT             64
/*---------------------------------------------------------------------------*/
/* Global variables                                                          */
/*---------------------------------------------------------------------------*/
uint32_t g_au32SourceData[TEST_COUNT];
uint32_t g_au32DestinationData[TEST_COUNT];
uint8_t u8TestData = 10;
uint8_t u8SendDataState = 0;
/*---------------------------------------------------------------------------*/
/* Functions                                                                 */
/*---------------------------------------------------------------------------*/
void delay_ms(int32_t ms)
{
    int32_t i;

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

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

    SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);

    if (u8Status & STA_MARK_TX)
    {
        u8SendDataState = HAS_SENDSTATE;
    }
    else if (u8Status & STA_MARK_MX)
    {
        u8SendDataState = TIMEOUT_SENDSTATE;
    }
}

void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/

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

    /* Waiting for clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

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

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

    /* Enable UART peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);
    /* Enable SPI0 peripheral clock */
    CLK_EnableModuleClock(SPI0_MODULE);

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

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

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

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

    /* Lock protected registers */
    SYS_LockReg();

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

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

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

int main(void)
{

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

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

    /* Init SPI */
    SPI_Init();

    GPIO_SetMode(P0, BIT6, GPIO_PMD_OUTPUT);
    GPIO_SetMode(P3, BIT6, GPIO_PMD_OUTPUT);

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

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

    GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
    NVIC_EnableIRQ(EINT0_IRQn);

    u8SendDataState = 0;

    delay_ms(100);
    init_nrf24l01_io();
    delay_ms(500);

    printf("Send READY!\r\n");
    ifnnrf_CLERN_ALL();
    ifnnrf_rx_mode();

    u8sta = SPI_Read(RF_SETUP);
    u8sta = SPI_Read(RF_CH);
    u8sta = SPI_Read(EN_AA);
    u8sta = SPI_Read(RF_SETUP);

    SPI_RW_Reg(WRITE_REG + RF_STATUS, 0xff);

    au8tx_buf[0] = 0xAA;
    au8tx_buf[1] = 0x55;
    au8tx_buf[2] = 0x01;

    if (rf_CheckCD())
    {
        rf_ChangeCH(0);
    }

    ifnnrf_tx_mode();

    while (1)
    {
        if (u8SendDataState == HAS_SENDSTATE)
        {
            u8SendDataState = NOT_SENDSTATE;
            au8tx_buf[0] = 0xAA;
            au8tx_buf[1] = u8TestData++;
            au8tx_buf[2] = u8TestData;

            if (u8TestData == 0xff)
            {
                u8TestData = 0;
            }

            LED = ~LED;
            ifnnrf_tx_mode();
        }
        else if (u8SendDataState == TIMEOUT_SENDSTATE)
        {
            u8SendDataState = NOT_SENDSTATE;

            while (rf_CheckCD())
            {
                rf_ChangeCH(0);
            }

            ifnnrf_tx_mode();
        }
    }
}

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

使用特权

评论回复
地板
heisexingqisi| | 2020-3-29 13:20 | 只看该作者
没有这个模块,先看看。

使用特权

评论回复
5
磨砂| | 2020-4-6 15:46 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
6
晓伍| | 2020-4-6 15:47 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

144

主题

1752

帖子

3

粉丝