完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
主程序
void main(void) { u8 i,dieT,saveflag,DieSum,ledflash; u8 diestate; u16 gametimes; #ifdef DEBUG debug(); #endif RCC_Configuration(); GPIO_Configuration(); NRF24L01_SPI_Init(); NVIC_Configuration(); g_SysPad1=0; while (g_SysPad1<10); CommPort_Init(); AdcInit(); L01_Init(); L01_SetTRMode(RX_MODE); L01_WriteHoppingPoint(0); L01_FlushRX( ); L01_FlushTX( ); EXTI_Configuration(); Rf_24L01_Send_End=1; L01_MISO_HIGH(); systemstartup_init(); g_GameState=0; gameTimes=0; diestate=0; g_EXTI4_OverT=1; g_Txbuffer[0]=0xF2;//询问状态 g_Txbuffer[1]=0xf2; g_Txbuffer[2]=0xba; nRF24L01_Send(g_Txbuffer,3); while(1) { if(Rf_24L01_Send_End==0x00) { g_Txbuffer[0]=0xf2; g_Txbuffer[1]=0xf4; g_Txbuffer[2]=0xba; nRF24L01_Send(g_Txbuffer,3); } else { Rf_24L01_Send_End=0; nRF24L01_Send(g_Txbuffer,2); } 中断 void Exti9_5Handler() { u8 tmp; tmp=L01_ReadStatusReg(); if( tmp & ( 1< L01_CE_LOW();// 发射完毕,CE = 0,省电 L01_FlushTX( );//清TX FIFO Rf_24L01_Send_End=1; L01_SetTRMode(RX_MODE); } if( tmp & ( 1< //发射失败,再发送一次 L01_SetTRMode(RX_MODE); L01_FlushRX( ); L01_FlushTX( ); Rf_24L01_Send_End=1; } if( tmp & ( 1< // g_ACKcnt=0;//发送完成等待接收指令,一段时间内不回指令,则重新发送该条指令 tmp = L01_ReadRXPayload( Rf_Rec_Buf ); L01_SetTRMode(RX_MODE); //L01_WriteHoppingPoint(0); L01_FlushRX( ); L01_FlushTX( ); Rf_ReceiveEnd=1; } EXTI_ClearITPendingBit(EXTI_Line5); } spi void NRF24L01_SPI_Init(void) { SPI_InitTypeDef SPI_InitStructure; SPI_Cmd(SPI1, DISABLE); //失能SPI外设 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //工作模式:主SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;// 8位帧结构 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //空闲状态为低电平 SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//第1个跳变沿数据被采样 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号软件管理 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;//预分频值为256 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//数据传输从MSB位开始 SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式 SPI_Init(SPI1, &SPI_InitStructure); //初始化外设SPIx寄存器 SPI_Cmd(SPI1, ENABLE); //使能SPI外设 } 中断 void EXTI_Configuration(void) { EXTI_InitTypeDef EXTI_InitStructure; //EXTI初始化结构定义 // GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);//管脚选择 // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//事件选择 // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//触发模式 // EXTI_InitStructure.EXTI_Line = EXTI_Line4 ; //线路选择 // EXTI_InitStructure.EXTI_LineCmd = ENABLE;//启动中断 // EXTI_Init(&EXTI_InitStructure);//初始化 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);//管脚选择 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//事件选择 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//触发模式 EXTI_InitStructure.EXTI_Line = EXTI_Line5 ; //线路选择 EXTI_InitStructure.EXTI_LineCmd = ENABLE;//启动中断 EXTI_Init(&EXTI_InitStructure);//初始化 } /***************************外部中断2.4g_IRQ**************************************************/ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; //通道 无线枪 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //启动 NVIC_Init(&NVIC_InitStructure); //初始化 24L01程序 #define _nRF24L01_C_ #include "nRF24L01.h" INT8U CE_Status = 0; INT8U L01_GetCEStatus( void ) { return CE_Status; } unsigned char SPI_RW(unsigned char dat) { u8 retry=0; while((SPI1->SR&1<<1)==0)//等待发送区空 { retry++; if(retry>200)return 0; } SPI1->DR=dat; //发送一个byte retry=0; while((SPI1->SR&1<<0)==0) //等待接收完一个byte { retry++; if(retry>200)return 0; } return SPI1->DR; //返回收到的数据 } void L01_SetCE( unsigned char status ) { CE_Status = status; if( status == 0 ) { L01_CE_LOW( ); } else { L01_CE_HIGH( ); } } INT8U L01_ReadSingleReg( INT8U Addr ) { INT8U btmp; L01_CSN_LOW( ); SPI_RW( R_REGISTER | Addr ); btmp = SPI_RW( 0xFF ); L01_CSN_HIGH( ); 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_RW( W_REGISTER | Addr ); SPI_RW( Value ); L01_CSN_HIGH( ); L01_SetCE( tmp ); } void L01_WriteMultiReg( INT8U StartAddr, INT8U *pBuff, INT8U Length ) { INT8U i; INT8U tmp = L01_GetCEStatus( ); L01_SetCE( 0 ); L01_CSN_LOW( ); SPI_RW( W_REGISTER | StartAddr ); for( i = 0; i < Length; i ++ ) { SPI_RW( *( pBuff + i ) ); } L01_CSN_HIGH( ); L01_SetCE( tmp ); } void L01_FlushTX( void ) { L01_CSN_LOW( ); SPI_RW( FLUSH_TX ); L01_CSN_HIGH( ); } void L01_FlushRX( void ) { L01_CSN_LOW( ); SPI_RW( FLUSH_RX ); L01_CSN_HIGH( ); } INT8U L01_ReadStatusReg( void ) { INT8U Status; L01_CSN_LOW( ); Status = SPI_RW( R_REGISTER + L01REG_STATUS ); L01_CSN_HIGH( ); return Status; } void L01_ClearIRQ( INT8U IRQ_Source ) { INT8U btmp = 0; IRQ_Source &= ( 1< L01_CSN_LOW( ); L01_WriteSingleReg( L01REG_STATUS, IRQ_Source | btmp ); L01_CSN_HIGH( ); L01_ReadStatusReg( ); } INT8U L01_ReadTopFIFOWidth( void ) { INT8U btmp; L01_CSN_LOW( ); SPI_RW( R_RX_PL_WID ); btmp = SPI_RW( 0xFF ); L01_CSN_HIGH( ); return btmp; } INT8U L01_ReadRXPayload( INT8U *pBuff ) { INT8U width, PipeNum; PipeNum = ( L01_ReadSingleReg( L01REG_STATUS ) >> 1 ) & 0x07; width = L01_ReadTopFIFOWidth( ); g_Rf_rec_num=width; L01_CSN_LOW( ); SPI_RW( R_RX_PAYLOAD ); for( PipeNum = 0; PipeNum < width; PipeNum ++ ) { *( pBuff + PipeNum ) = SPI_RW( 0xFF ); } L01_CSN_HIGH( ); L01_FlushRX( ); return width; } void L01_WriteTXPayload_Ack( INT8U *pBuff, INT8U nBytes ) { INT8U btmp; INT8U length = ( nBytes > 32 ) ? 32 : nBytes; L01_FlushTX( ); L01_CSN_LOW( ); SPI_RW( W_TX_PAYLOAD ); for( btmp = 0; btmp < length; btmp ++ ) { SPI_RW( *( pBuff + btmp ) ); } L01_CSN_HIGH( ); } void L01_SetTXAddr( INT8U *pAddr, INT8U Addr_Length ) { INT8U Length = ( Addr_Length > 5 ) ? 5 : Addr_Length; L01_WriteMultiReg( L01REG_TX_ADDR, pAddr, Length ); } void L01_SetRXAddr( INT8U PipeNum, INT8U *pAddr, INT8U Addr_Length ) { INT8U Length = ( Addr_Length > 5 ) ? 5 : Addr_Length; INT8U pipe = ( PipeNum > 5 ) ? 5 : PipeNum; L01_WriteMultiReg( L01REG_RX_ADDR_P0 + pipe, pAddr, Length ); } void L01_WriteHoppingPoint( INT8U FreqPoint ) { L01_WriteSingleReg( L01REG_RF_CH, FreqPoint & 0x7F ); } void L01_SetTRMode( L01MD mode ) { INT8U controlreg = L01_ReadSingleReg( L01REG_CONFIG ); if( mode == TX_MODE ) { controlreg &= ~( 1< L01_WriteSingleReg( L01REG_CONFIG, controlreg ); } void L01_SetPowerDown( void ) { INT8U controlreg = L01_ReadSingleReg( L01REG_CONFIG ); L01_WriteSingleReg( L01REG_CONFIG, controlreg & ( ~( 1< void L01_Init( void ) { INT8U addr[5] = {INIT_ADDR}; INT8U tmp = L01_GetCEStatus( ); L01_SetCE( 0 ); L01_SetPowerDown( ); L01_ClearIRQ( IRQ_ALL ); #if DYNAMIC_PACKET == 1 //dynamic payload length L01_WriteSingleReg( L01REG_DYNPD, ( 1<<0 ) );//Enable pipe 0 dynamic payload length L01_WriteSingleReg( L01REG_FEATRUE, 0x06 ); L01_ReadSingleReg( L01REG_DYNPD ); L01_ReadSingleReg( L01REG_FEATRUE ); #elif DYNAMIC_PACKET == 0 //Fixed packet length L01_WriteSingleReg( L01REG_RX_PW_P0, FIXED_PACKET_LEN ); #endif//DYNAMIC_PACKET L01_WriteSingleReg( L01REG_CONFIG,/* ( 1< L01_WriteSingleReg( L01REG_RETR, (((REPEAT_TIME/250)&0X0F)<<4) | ( REPEAT_CNT & 0x0F ) ); //repeat SETTING L01_WriteSingleReg( L01REG_RF_CH, 0x0 ); //Initial channel L01_WriteSingleReg( L01REG_RF_SETUP, 0x27 ); L01_SetTXAddr( &addr[4], 5 ); //Set TX address L01_SetRXAddr( 0, &addr[4], 5 ); //Set RX address //L01_SetPowerUp( ); } void nRF24L01_Send(INT8U *pBuff, INT8U nBytes) { if(Rf_24L01_Send_End) { L01_SetTRMode(TX_MODE); L01_WriteHoppingPoint(0); //L01_FlushRX( ); //L01_FlushTX( ); L01_CE_LOW( ); L01_FlushRX( );//清RX FIFO L01_FlushTX( );//清TX FIFO L01_WriteTXPayload_Ack( pBuff,nBytes); L01_CE_HIGH( ); // CE = 1,启动发射 Rf_24L01_Send_End=0; //g_ACKcnt=1; //Rf_ReceiveEnd=0; } } /* ================================================================================ ====================================End of file================================= ================================================================================ */ |
|
相关推荐
1个回答
|
|
给你一个参考,这个我是我移植的一个,在很多的MCU上都调试通过的。。。。
思想很重要,希望你能理解。 https://bbs.elecfans.com/forum.p ... d=515514&extra= |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1085 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1041 浏览 2 评论
2145 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1240 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1660 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 22:19 , Processed in 0.517227 second(s), Total 75, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号