完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include <STM32f10x.h>
#include #include #include #include void led_configuration(void) { GPIO_InitTypeDef io; io.GPIO_Pin=GPIO_Pin_4; io.GPIO_Mode=GPIO_Mode_Out_PP; io.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&io); GPIO_Init(GPIOE,&io); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE); } void usart_configuration(void) { USART_InitTypeDef usart1_init; GPIO_InitTypeDef io; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE); USART_DeInit(USART1); //USART1_RX PA.9 io.GPIO_Pin = GPIO_Pin_9; //PA.9 io.GPIO_Speed = GPIO_Speed_50MHz; io.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_Init(GPIOA, &io); //³õʼ»¯PA9 //USART1_RX PA.10 io.GPIO_Pin = GPIO_Pin_10;//PA.10 io.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë GPIO_Init(GPIOA, &io); //³õʼ»¯PA10 usart1_init.USART_BaudRate=9600; usart1_init.USART_WordLength=USART_WordLength_8b; usart1_init.USART_StopBits=USART_StopBits_1; usart1_init.USART_Parity=USART_Parity_No; usart1_init.USART_HardwareFlowControl=USART_HardwareFlowControl_None; usart1_init.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; USART_Init(USART1,&usart1_init); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_Cmd(USART1,ENABLE); USART_ClearFlag(USART1,USART_FLAG_TC); } uint8_t dat,i; uint16_t num; int main(void) { uint16_t t; led_configuration(); i=0; usart_configuration(); delay_init(); NRF24L01_init(); SPI_Cmd(SPI2, ENABLE); delay_ms(5); NRF24L01_RX_Mode(); while(1) { NRF24L01_RxPacket(&dat); pwm_use(dat); USART_SendData(USART1,dat); i++; if(i==2)i=0; t++; delay_ms(300); if(t==10000) t=0; } } #include #include const uint8_t RxAddress[RX_ADR_WIDTH]={0x34,0x43,0x10,0x10,0x01}; void NRF24L01_init(void) { GPIO_InitTypeDef io; GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOG,ENABLE); io.GPIO_Mode=GPIO_Mode_Out_PP; io.GPIO_Speed=GPIO_Speed_50MHz; io.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6|GPIO_Pin_8; GPIO_Init(GPIOG,&io); GPIO_ResetBits(GPIOG,GPIO_Pin_7|GPIO_Pin_6);//PG6:CE ;PG7:CSN io.GPIO_Mode=GPIO_Mode_IPD; io.GPIO_Pin=GPIO_Pin_8; GPIO_Init(GPIOG,&io); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTBʱÖÓʹÄÜ RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2ʱÖÓʹÄÜ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15¸´ÓÃÍÆÍìÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIOB GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15ÉÏÀ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹ SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //´®ÐÐͬ²½Ê±ÖӵĿÕÏÐ״̬Ϊ¸ßµçƽ SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //´®ÐÐͬ²½Ê±Öӵĵڶþ¸öÌø±äÑØ£¨ÉÏÉý»òϽµ£©Êý¾Ý±»²ÉÑù SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼ SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ SPI_Init(SPI2, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷ SPI_Cmd(SPI2,DISABLE); } uint8_t SPI2_WriteReadByte(uint8_t dat)//ÓÃSPIÐÒéÔÀíÏòNRF24L01¼Ä´æÆ÷·¢ËͺͽÓÊÕÊý¾Ý { u8 retry=0; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //¼ì²éÖ¸¶¨µÄSPI·¢ËÍ»º´æ¼Ä´æÆ÷ÊÇ·ñΪ¿Õ.£¨Îª¿ÕÔò»º´æ¼Ä´æÆ÷ÖеÄֵΪ¿Õ£¬¼´Êý¾ÝÒÑ·¢ËÍÍ꣬¿ÉÒÔ·¢ËÍеÄÊý¾Ý£© { retry++; if(retry>200)return 0; } SPI_I2S_SendData(SPI2, dat); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÄãÒª·¢Ë͵ÄÐÂÊý¾Ý retry=0; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)//¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±ê־λ { retry++; if(retry>200)return 0; } retry=0; return SPI_I2S_ReceiveData(SPI2); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý } u8 NRF24L01_Read_Reg(u8 reg)//ÓÃÀ´¶ÁÈ¡NRF24L01µÄ״̬¼Ä´æÆ÷µÄ״ֵ̬ { u8 reg_val; GPIO_ResetBits(GPIOG,GPIO_Pin_7); //ʹÄÜSPI´«Êä SPI2_WriteReadByte(reg); //·¢ËͼĴæÆ÷ºÅ reg_val=SPI2_WriteReadByte(0XFF);//¶ÁÈ¡¼Ä´æÆ÷ÄÚÈÝ GPIO_SetBits(GPIOG,GPIO_Pin_7); //½ûÖ¹SPI´«Êä return(reg_val); //·µ»Ø״ֵ̬ } void NRF24L01_write_InputAddress(uint8_t TxAddress_reg,uint8_t *point,uint8_t len) { uint8_t i; GPIO_ResetBits(GPIOG,GPIO_Pin_7); SPI2_WriteReadByte(TxAddress_reg); for(i=0;i SPI2_WriteReadByte(*point++); } GPIO_SetBits(GPIOG,GPIO_Pin_7); } void NRF24L01_Write_Comman(uint8_t Address,uint8_t value) { GPIO_ResetBits(GPIOG,GPIO_Pin_7); SPI2_WriteReadByte(Address); SPI2_WriteReadByte(value); GPIO_SetBits(GPIOG,GPIO_Pin_7); } void NRF24L01_ReadDat(u8 reg,u8 *pBuf,u8 len)//¶ÁÈ¡NRF24L01Öдæ·ÅÊý¾ÝµÄ¼Ä´æÆ÷µÄÊý¾ÝÖµ { uint8_t i; GPIO_ResetBits(GPIOG,GPIO_Pin_7); SPI2_WriteReadByte(reg); for(i=0;i GPIO_SetBits(GPIOG,GPIO_Pin_7); } void NRF24L01_RxPacket(u8 *rxbuf) { u8 sta; sta=NRF24L01_Read_Reg(STATUS); NRF24L01_Write_Comman(WRITE_REG_NRF+STATUS,sta);//Çå³ýTX_DS»òMAX_RTÖжϱêÖ¾ //NRF24L01_ReadDat(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//¶ÁÈ¡Êý¾Ý //if(sta&RX_OK)//½ÓÊÕµ½Êý¾Ý //{ NRF24L01_ReadDat(RD_RX_PLOAD,rxbuf,RX_PLOAD_WIDTH);//¶ÁÈ¡Êý¾Ý NRF24L01_Write_Comman(FLUSH_RX,0xff);//Çå³ýRX FIFO¼Ä´æÆ÷ //} } void NRF24L01_RX_Mode(void) { GPIO_ResetBits(GPIOG,GPIO_Pin_6); NRF24L01_write_InputAddress(WRITE_REG_NRF+RX_ADDR_P0,(u8*)RxAddress,RX_ADR_WIDTH); //ÉèÖÃRX½ÚµãµØÖ· NRF24L01_Write_Comman(WRITE_REG_NRF+EN_AA,0x01); //ʹÄÜͨµÀ0µÄ×Ô¶¯Ó¦´ð NRF24L01_Write_Comman(WRITE_REG_NRF+EN_RXADDR,0x01); //ʹÄÜͨµÀ0µÄ½ÓÊÕµØÖ· NRF24L01_Write_Comman(WRITE_REG_NRF+RF_CH,40); //ÉèÖÃRFͨµÀΪ40 NRF24L01_Write_Comman(WRITE_REG_NRF+RX_PW_P0,RX_PLOAD_WIDTH);//Ñ¡ÔñͨµÀ0µÄÓÐЧÊý¾Ý¿í¶È NRF24L01_Write_Comman(WRITE_REG_NRF+RF_SETUP,0x0f); //ÉèÖÃTX·¢Éä²ÎÊý,0dbÔöÒæ,2Mbps,µÍÔëÉùÔöÒ濪Æô NRF24L01_Write_Comman(WRITE_REG_NRF+CONFIG,0x0f); //ÅäÖûù±¾¹¤×÷ģʽµÄ²ÎÊý;PWR_UP,EN_CRC,16BIT_CRC,½ÓÊÕģʽ,¿ªÆôËùÓÐÖÐ¶Ï GPIO_SetBits(GPIOG,GPIO_Pin_6); }
|
|
相关推荐
|
|
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
304 浏览 0 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1043 浏览 1 评论
LL库F030进行3个串口收发,2个串口为232,一个为485,长时间后,会出现串口1停止运行,另外两个正常,只有重启复原
1523 浏览 1 评论
526 浏览 0 评论
1059 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-6 07:46 , Processed in 0.622822 second(s), Total 81, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号