×
单片机 > 单片机程序设计 > 详情

nRF24L01p+AVR单片机ATmage88射频收发程序

发布时间:2020-08-27 发布时间:
|

单片机源程序如下:

#define _nRF24L01_C_

#include "nRF24L01.h"


INT8U CE_Status = 0;

/*

================================================================================

Function : L01_GetCEStatus( )

Description : Get the status of the CE PIN

Input : NONE

Output: 1:CE=1, 0:CE=0

================================================================================

*/

INT8U L01_GetCEStatus( void )

{

        return CE_Status;

}

/*

================================================================================

Function : L01_SetCE( )

Description : Set the CE PIN as 1 or 0

Input : -status, 1: CE=1, 0: CE=0

Output: None

================================================================================

*/

void L01_SetCE( INT8U status )

{

        CE_Status = status;

        if( status == 0 )  { L01_CE_LOW( ); }

        else               { L01_CE_HIGH( ); }

}

/*

================================================================================

Function : L01_ReadSingleReg( )

Description : Read a single register of nRF24L01

Input : -Addr, The address of the register

Output: The value read from the register

================================================================================

*/

INT8U L01_ReadSingleReg( INT8U Addr )

{

    INT8U btmp;

    L01_CSN_LOW( );//PB2输出低电平

    SPI_ExchangeByte( R_REGISTER | Addr );

    btmp = SPI_ExchangeByte( 0xFF );

    L01_CSN_HIGH( );//PB2输出高电平

    return btmp;

}

/*

================================================================================

Function : L01_ReadMultiReg( )

Description : Read several registers of nRF24L01

Input : -StartAddr, The start address of the registers

        -nBytes, How many registers do you want to read

        -pBuff, The buffer to save the values

Output: None

================================================================================

*/

/*void L01_ReadMultiReg( INT8U StartAddr, INT8U nBytes, INT8U *pBuff )

{

    INT8U btmp;

    L01_CSN_LOW( );

    SPI_ExchangeByte( R_REGISTER | StartAddr );

    for( btmp = 0; btmp < nBytes; btmp ++ )

    {

        *( pBuff + btmp ) = SPI_ExchangeByte( 0xFF );

    }

    L01_CSN_HIGH( );

}


================================================================================

Function : L01_WriteSingleReg( )

Description : Write a single byte to a register

Input : -Addr, The address of the register

        -Value, The value to be written

Output: None

================================================================================

*/

void L01_WriteSingleReg( INT8U Addr, INT8U Value )

{

        INT8U tmp = L01_GetCEStatus( );

        L01_SetCE( 0 );

    L01_CSN_LOW( );

    SPI_ExchangeByte( W_REGISTER | Addr );

    SPI_ExchangeByte( Value );

    L01_CSN_HIGH( );

        L01_SetCE( tmp );

}

/*

================================================================================

Function : L01_WriteMultiReg( )

Description : Read several registers of nRF24L01

Input : -StartAddr, The start address of the registers

        -pBuff, The buffer store the values

        -Length, How many registers do you want to write

Output: None

================================================================================

*/

void L01_WriteMultiReg( INT8U StartAddr, INT8U *pBuff, INT8U Length )

{

    INT8U i;

        INT8U tmp = L01_GetCEStatus( );

        L01_SetCE( 0 );

    L01_CSN_LOW( );

    SPI_ExchangeByte( W_REGISTER | StartAddr );

    for( i = 0; i < Length; i ++ )

    {

        SPI_ExchangeByte( *( pBuff + i ) );

    }

    L01_CSN_HIGH( );

        L01_SetCE( tmp );

}

/*

================================================================================

Function : L01_FlushTX( )

Description : Flush the TX buffer

Input : None

Output: None

================================================================================

*/

void L01_FlushTX( void )

{

    L01_CSN_LOW( );

    SPI_ExchangeByte( FLUSH_TX );

    L01_CSN_HIGH( );

}

/*

================================================================================

Function : L01_FlushRX( )

Description : Flush the RX buffer

Input : None

Output: None

================================================================================

*/

void L01_FlushRX( void )

{

    L01_CSN_LOW( );

    SPI_ExchangeByte( FLUSH_RX );

    L01_CSN_HIGH( );

}

/*

================================================================================

Function : L01_ReuseTXPayload( )

Description : Reuse the last transmitted payload

Input : None

Output: None

================================================================================

*/

void L01_ReuseTXPayload( void )

{

    L01_CSN_LOW( );

    SPI_ExchangeByte( REUSE_TX_PL );

    L01_CSN_HIGH( );

}

/*

================================================================================

Function : L01_Nop( )

Description : nop operation of nRF24L01

Input : None

Output: None

================================================================================

*/

void L01_Nop( void )

{

    L01_CSN_LOW( );

    SPI_ExchangeByte( L01_NOP );

    L01_CSN_HIGH( );

}

/*

================================================================================

Function : L01_ReadStatusReg( )

Description : Read statu register of nRF24L01

Input : None

Output: Statu register of nRF24L01

================================================================================

*/

INT8U L01_ReadStatusReg( void )

{

    INT8U Status;

    L01_CSN_LOW( );

    Status = SPI_ExchangeByte( R_REGISTER + L01REG_STATUS );

    L01_CSN_HIGH( );

    return Status;

}

/*

================================================================================

Function : L01_ClearIRQ( )

Description : Clear IRQ cuased by nRF24L01

Input : None

Output: None

================================================================================

*/

void L01_ClearIRQ( INT8U IRQ_Source )

{

    INT8U btmp = 0;


    IRQ_Source &= ( 1<

    btmp = L01_ReadStatusReg( );

    L01_CSN_LOW( );

        L01_WriteSingleReg( L01REG_STATUS, IRQ_Source | btmp );

    L01_CSN_HIGH( );

    L01_ReadStatusReg( );

}

/*

================================================================================

Function : L01_ReadIRQSource( )

Description : Read the IRQ source of nRF24L01+

Input : None

Output: IRQ source mask code

================================================================================

*/

INT8U L01_ReadIRQSource( void )

{

    return ( L01_ReadStatusReg( ) & ( ( 1<

}

/*

================================================================================

Function : L01_ReadTopFIFOWidth( )

Description : Read the payload width of the top buffer of FIFO

Input : None

Output: The width of the pipe buffer

================================================================================

*/

INT8U L01_ReadTopFIFOWidth( void )

{

    INT8U btmp;

    L01_CSN_LOW( );

    SPI_ExchangeByte( R_RX_PL_WID );

    btmp = SPI_ExchangeByte( 0xFF );

    L01_CSN_HIGH( );

    return btmp;

}

/*

================================================================================

Function : L01_ReadRXPayload( )

Description : Read the RX payload from internal buffer

Input : -pBuff, buffer to store the data

Output: The length of data read

================================================================================

*/

INT8U L01_ReadRXPayload( INT8U *pBuff )

{

    INT8U width, PipeNum;

    PipeNum = ( L01_ReadSingleReg( L01REG_STATUS ) >> 1 ) & 0x07;

    width = L01_ReadTopFIFOWidth( );


    L01_CSN_LOW( );

    SPI_ExchangeByte( R_RX_PAYLOAD );

    for( PipeNum = 0; PipeNum < width; PipeNum ++ )

    {

        *( pBuff + PipeNum ) = SPI_ExchangeByte( 0xFF );

    }

    L01_CSN_HIGH( );

    L01_FlushRX( );

    return width;

}

/*

================================================================================

Function : L01_WriteTXPayload( )

Description : Write TX payload to a pipe and prx will return ack back

Input : -PipeNum, number of the pipe

        -pBuff, A buffer stores the data

        -nBytes, How many bytes to be wrote to

Output: None

================================================================================

*/

void L01_WriteTXPayload_Ack( INT8U *pBuff, INT8U nBytes )

{

    INT8U btmp;

    INT8U length = ( nBytes > 32 ) ? 32 : nBytes;


    L01_FlushTX( );

    L01_CSN_LOW( );

    SPI_ExchangeByte( W_TX_PAYLOAD );

    for( btmp = 0; btmp < length; btmp ++ )

    {

        SPI_ExchangeByte( *( pBuff + btmp ) );

    }

    L01_CSN_HIGH( );

}

/*

================================================================================

Function : L01_WritePayload_NoAck( )

Description : write data in tx mode, and prx won't return ack back

Input : -Data, A buffer stores the address data

        -Data_Length, How many bytes of the data buff

Output: None

================================================================================

*/

void L01_WriteTXPayload_NoAck( INT8U *Data, INT8U Data_Length )

{

    if( Data_Length > 32 || Data_Length == 0 )

    {

        return ;


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
AVR熔丝位操作时的要点和需要注意的相关事项