最近项目中用到了2.4G无线模块的数据通信,经过无数次的摸索和实验,完成了该试验,由于项目不方便上传,我就把最初的实验工程分享给大家。该实验用的是STM32F103C8作为主控,nrf24l01作为无线模块,通过SPI协议进行数据传输。
该模块具有自动应答,自动重发机制,可以根据项目需求进行相关设置,该无线模块可以轻松配置为1对多模式。而且对于数据的收、发模式切换相当简单,只需修改一个寄存器便可完成:
发送模式:L01_SetTRMode( TX_MODE );
接受模式: L01_SetTRMode( RX_MODE );
在工程文件中,可以根据宏定义配置为接收模块或者发送模块:
设置为发送:#define ML01TX_MODE 屏蔽该句便是接收。
希望该贴对于初次接触2.4G无线模块的您有所帮助!
|
引脚定义
- /**
- ******************************************************************************
- * @file SPI/FullDuplex_SoftNSS/main.c
- * @author MCD Application Team
- * @version V3.5.0
- * @date 08-April-2011
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- *
© COPYRIGHT 2011 STMicroelectronics - ******************************************************************************
- */
- //该版本用于MM板,等待接收数据然后改为发送模式循环发送
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "platform_config.h"
- #include "board.h"
- #include "nRF24L01.h"
- #include
- #define ML01TX_MODE
- //#define ML01RX_MODE
- /** @addtogroup STM32F10x_StdPeriph_Examples
- * @{
- */
- /** @addtogroup SPI_FullDuplex_SoftNSS
- * @{
- */
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- SPI_InitTypeDef SPI_InitStructure;
- void RCC_Configuration(void);
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- #ifdef ML01TX_MODE
- int main(void)
- {
- u32 dly;
- u8 tmp,x,testbuffer[10] = {"00000"};
- u8 tx_couter = 0, itmp;
- /* System clocks configuration ---------------------------------------------*/
- RCC_Configuration();
- GPIO_Initial();
- SPI_Initial();
- L01_Init( );
- L01_SetTRMode( TX_MODE );
- L01_WriteHoppingPoint( 60 );
- L01_SetSpeed( SPD_250K );
- while( 1 )
- {
- u8 read;
- for( dly = 0; dly < 8000000; dly ++ );
- //LED_Off2();
- L01_FlushRX( );
- L01_FlushTX( );
- L01_WriteTXPayload_Ack( (u8*)"123rn", strlen( "123rn" ) );
- GPIO_SetBits( GPIOB, SPIML01_PIN_CE );
- while( GPIO_ReadInputDataBit( GPIOB, SPIML01_PIN_IRQ ) != 0 );
- while( ( tmp = L01_ReadIRQSource( ) ) == 0 );
- GPIO_ResetBits( GPIOB, SPIML01_PIN_CE );
- if( tmp & ( 1< {
- int i=0;
- i+=1;
- LED_Toggle( );
- }
- if( tmp & ( 1< {
- // LED_Toggle2( );
- L01_ClearIRQ( IRQ_ALL );
- //修改为接收模式
- L01_SetTRMode( RX_MODE );
- while( 1 )
- {
- L01_FlushRX( );
- L01_FlushTX( );
- GPIO_SetBits( GPIOB, SPIML01_PIN_CE );
- while( GPIO_ReadInputDataBit( GPIOB, SPIML01_PIN_IRQ ) != 0 );
- while( ( tmp = L01_ReadIRQSource( ) ) == 0 );
- GPIO_ResetBits( GPIOB, SPIML01_PIN_CE );
- if( tmp & ( 1< {
- // USART_SendStr( "????rn" );
- }
- else if( tmp & ( 1< {
- // USART_SendStr( "????rn" );
- }
- else if( tmp & ( 1< {
- for( tmp = 0; tmp < 32; tmp ++ )
- {
- testbuffer[tmp] = 0;
- }
- tmp = L01_ReadRXPayload( testbuffer );
- LED_Toggle2( );
- L01_SetTRMode( TX_MODE );
- L01_ClearIRQ( IRQ_ALL );
- break ;
- }
- L01_ClearIRQ( IRQ_ALL );
- }
- }
- L01_ClearIRQ( IRQ_ALL );
- }
- }
- #else // ML01RX_MODE
- int main(void)
- {
- u32 dly;
- u8 tmp,x,testbuffer[10] = {"00000"};
- u8 tx_couter = 0, itmp;
- /* System clocks configuration ---------------------------------------------*/
- RCC_Configuration();
- GPIO_Initial();
- SPI_Initial();
- L01_Init( );
- L01_SetTRMode( RX_MODE );
- L01_WriteHoppingPoint( 60 );
- L01_SetSpeed( SPD_250K );
- while( 1 )
- {
- L01_FlushRX( );
- L01_FlushTX( );
- GPIO_SetBits( GPIOB, SPIML01_PIN_CE );
- while( GPIO_ReadInputDataBit( GPIOB, SPIML01_PIN_IRQ ) != 0 );
- while( ( tmp = L01_ReadIRQSource( ) ) == 0 );
- if( tmp & ( 1< {
- // USART_SendStr( "????rn" );
- }
- else if( tmp & ( 1< {
- // USART_SendStr( "????rn" );
- }
- else if( tmp & ( 1< {
- for( tmp = 0; tmp < 32; tmp ++ )
- {
- testbuffer[tmp] = 0;
- }
- tmp = L01_ReadRXPayload( testbuffer );
- if( tmp == 5 && testbuffer[0] == '1' && testbuffer[1] == '2' && testbuffer[2] == '3' )
- {
- LED_Toggle( );
- // rx_conter ++;
- // itmp = rx_conter;
- // testbuffer[0] = ( itmp / 10000 ) + '0';
- // itmp %= 10000;
- // testbuffer[1] = ( itmp / 1000 ) + '0';
- // itmp %= 1000;
- // testbuffer[2] = ( itmp / 100 ) + '0';
- // itmp %= 100;
- // testbuffer[3] = ( itmp / 10 ) + '0';
- // itmp %= 10;
- // testbuffer[4] = itmp + '0';
- // testbuffer[5] = 0;
- // LCD_Dis_Str( 6, 80, (char*)testbuffer );
- }
- }
- L01_ClearIRQ( IRQ_ALL );
- }
- }
- #endif
- /**
- * @brief Configures the different system clocks.
- * @param None
- * @retval None
- */
- void RCC_Configuration(void)
- {
- /* PCLK2 = HCLK/2 */
- //RCC_PCLK2Config(RCC_HCLK_Div2);
- // RCC_DeInit();//RCC?????
- //
- // RCC_HSICmd(ENABLE);//??HSI
- // while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)//??HSI????
- // {
- // }
- //
- // if(1)
- // {
- // FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- // FLASH_SetLatency(FLASH_Latency_2);
- //
- // RCC_HCLKConfig(RCC_SYSCLK_Div1);
- // RCC_PCLK1Config(RCC_HCLK_Div2);
- // RCC_PCLK2Config(RCC_HCLK_Div1);
- //
- // RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_8);
- // RCC_PLLCmd(ENABLE);
- // while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- // {
- // }
- // RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- // while(RCC_GetSYSCLKSource() != 0x08)
- // {
- // }
- //
- //}
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- /* Enable SPIy Periph clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
- }
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
| 2.4G.zip
0
|
|
|
|