你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32---NRF24L01(临时代码)

[复制链接]
STMCU小助手 发布时间:2023-1-4 21:13
  1. #include<stm32f10x.h>
  2. #define NRF_READ_REG    0x00          // ¶Á¼Ä´æÆ÷Ö¸Áî
  3. #define NRF_WRITE_REG   0x20         // д¼Ä´æÆ÷Ö¸Áî
  4. #define ACTIVATE                0x50          // follow with 0x73 to activate feature register
  5. #define R_RX_PL_WID           0x60        // ¶Á½ÓÊÕ»º³åÇøµÄ³¤¶È
  6. #define RD_RX_PLOAD     0x61          // ¶ÁÈ¡½ÓÊÕÊý¾ÝÖ¸Áî
  7. #define WR_TX_PLOAD     0xA0          // д´ý·¢Êý¾ÝÖ¸Áî
  8. #define W_ACK_PAYLOAD        0xA8        // Used in RX mode.
  9. #define FLUSH_TX        0xE1         // ³åÏ´·¢ËÍ FIFOÖ¸Áî
  10. #define FLUSH_RX        0xE2          // ³åÏ´½ÓÊÕ FIFOÖ¸Áî
  11. #define REUSE_TX_PL     0xE3          // ¶¨ÒåÖØ¸´×°ÔØÊý¾ÝÖ¸Áî
  12. #define NOP             0xFF          // ±£Áô
  13. /******************************************************************************
  14.                                                         NRF24L01¼Ä´æÆ÷µØÖ·
  15. *******************************************************************************/
  16. #define CONFIG          0x00          // ÅäÖÃÊÕ·¢×´Ì¬£¬CRCУÑéģʽÒÔ¼°ÊÕ·¢×´Ì¬ÏìÓ¦·½Ê½
  17. #define EN_AA           0x01          // ×Ô¶¯Ó¦´ð¹¦ÄÜÉèÖÃ
  18. #define EN_RXADDR       0x02          // ¿ÉÓÃÐŵÀÉèÖÃ
  19. #define SETUP_AW        0x03          // ÊÕ·¢µØÖ·¿í¶ÈÉèÖÃ
  20. #define SETUP_RETR      0x04          // ×Ô¶¯ÖØ·¢¹¦ÄÜÉèÖÃ
  21. #define RF_CH           0x05          // ¹¤×÷ƵÂÊÉèÖÃ
  22. #define RF_SETUP        0x06          // ·¢ÉäËÙÂÊ¡¢¹¦ºÄ¹¦ÄÜÉèÖÃ
  23. #define NRFRegSTATUS    0x07          // ״̬¼Ä´æÆ÷
  24. #define OBSERVE_TX      0x08          // ·¢Ëͼà²â¹¦ÄÜ
  25. #define CD              0x09          // µØÖ·¼ì²â           
  26. #define RX_ADDR_P0      0x0A          // ƵµÀ0½ÓÊÕÊý¾ÝµØÖ·
  27. #define RX_ADDR_P1      0x0B          // ƵµÀ1½ÓÊÕÊý¾ÝµØÖ·
  28. #define RX_ADDR_P2      0x0C          // ƵµÀ2½ÓÊÕÊý¾ÝµØÖ·
  29. #define RX_ADDR_P3      0x0D          // ƵµÀ3½ÓÊÕÊý¾ÝµØÖ·
  30. #define RX_ADDR_P4      0x0E          // ƵµÀ4½ÓÊÕÊý¾ÝµØÖ·
  31. #define RX_ADDR_P5      0x0F          // ƵµÀ5½ÓÊÕÊý¾ÝµØÖ·
  32. #define TX_ADDR         0x10          // ·¢Ë͵ØÖ·¼Ä´æÆ÷
  33. #define RX_PW_P0        0x11          // ½ÓÊÕÆµµÀ0½ÓÊÕÊý¾Ý³¤¶È
  34. #define RX_PW_P1        0x12          // ½ÓÊÕÆµµÀ1½ÓÊÕÊý¾Ý³¤¶È
  35. #define RX_PW_P2        0x13          // ½ÓÊÕÆµµÀ2½ÓÊÕÊý¾Ý³¤¶È
  36. #define RX_PW_P3        0x14          // ½ÓÊÕÆµµÀ3½ÓÊÕÊý¾Ý³¤¶È
  37. #define RX_PW_P4        0x15          // ½ÓÊÕÆµµÀ4½ÓÊÕÊý¾Ý³¤¶È
  38. #define RX_PW_P5        0x16          // ½ÓÊÕÆµµÀ5½ÓÊÕÊý¾Ý³¤¶È
  39. #define FIFO_STATUS     0x17          // FIFOÕ»ÈëÕ»³ö״̬¼Ä´æÆ÷ÉèÖÃ

  40. #define DYNPD                    0x1C          // per pipe DPL control
  41. #define FEATURE                    0x1D          // ¡°Feature¡± register address

  42. #define NRF_CE_GPIO GPIOB
  43. #define NRF_CE_Pin GPIO_Pin_2

  44. #define NRF_CSN_GPIO GPIOA
  45. #define NRF_CSN_Pin  GPIO_Pin_4

  46. #define NRF_IRQ_GPIO GPIOB
  47. #define NRF_IRQ_Pin  GPIO_Pin_0

  48. #define NRF_CE_H  NRF_CE_GPIO->BSRR = NRF_CE_Pin
  49. #define NRF_CE_L        NRF_CE_GPIO->BRR  = NRF_CE_Pin

  50. #define NRF_CSN_H NRF_CSN_GPIO->BSRR = NRF_CSN_Pin
  51. #define NRF_CSN_L NRF_CSN_GPIO->BRR  = NRF_CSN_Pin

  52. #define NRF_IRQ_Read        NRF_IRQ_GPIO->IDR & NRF_IRQ_Pin

  53. char a_temp;
  54. void PrintString(char *a)
  55. {
  56.         while(*a != '\0')
  57.         {
  58.                 a_temp = (char)*a ;
  59.                 a++;
  60.                 USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
  61.         }
  62. }

  63. void SPI1_Init(void)
  64. {
  65.         SPI_InitTypeDef SPI_InitStructure;
  66.         GPIO_InitTypeDef GPIO_InitStructure;
  67.         
  68.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  69.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  70.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  71.         //ÅäÖÃSCK,MISO,MOSIÒý½Å  
  72.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  73.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  74.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //ÍÆÍ츴ÓÃ
  75.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  76.         //ÅäÖÃCEÒý½Å
  77.         GPIO_InitStructure.GPIO_Pin = NRF_CE_Pin;
  78.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  79.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
  80.         GPIO_Init(NRF_CE_GPIO, &GPIO_InitStructure);
  81.         //ÅäÖÃCSNÒý½Å
  82.         GPIO_InitStructure.GPIO_Pin = NRF_CSN_Pin;
  83.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  84.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
  85.         GPIO_Init(NRF_CSN_GPIO, &GPIO_InitStructure);        
  86.         //ÅäÖÃIRQÒý½Å
  87.         GPIO_InitStructure.GPIO_Pin = NRF_IRQ_Pin;
  88.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀ­ÊäÈë
  90.         GPIO_Init(NRF_IRQ_GPIO, &GPIO_InitStructure);        
  91.         NRF_CSN_H;        //½ûÖ¹NRFÆ÷¼þ
  92.         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //Ë«Ïßȫ˫¹¤
  93.         SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //Ö÷ģʽ
  94.         SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //Êý¾Ý´óС8λ
  95.         SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //ʱÖÓ¼«ÐÔ£¬¿ÕÏÐʱΪµÍ
  96.         SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //µÚ1¸ö±ßÑØÓÐЧ£¬ÉÏÉýÑØÎª²ÉÑùʱ¿Ì
  97.         SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;  //NSSÐźÅÓÉÈí¼þ²úÉú
  98.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //8·ÖƵ£¬9MHz
  99.         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //¸ßλÔÚǰ
  100.         SPI_InitStructure.SPI_CRCPolynomial = 7;
  101.         SPI_Init(SPI1, &SPI_InitStructure);
  102.            
  103.         SPI_Cmd(SPI1, ENABLE);//ʹÄÜ SPI1
  104.         
  105.         PrintString("\r\n SPI1 ³õʼ»¯Íê³É£¡");
  106. }

  107. uint8_t SPI_RW(uint8_t data)
  108. {
  109.         while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);
  110.         SPI_I2S_SendData(SPI1,data);
  111.         while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == RESET);
  112.         return SPI_I2S_ReceiveData(SPI1);
  113. }

  114. uint8_t NRF_Write_Reg(uint8_t reg,uint8_t value)
  115. {
  116.         uint8_t status;
  117.         NRF_CSN_L;
  118.         status = SPI_RW(reg);
  119.         SPI_RW(value);
  120.         NRF_CSN_H;
  121.         return status;
  122. }

  123. uint8_t NRF_Read_Reg(uint8_t reg)
  124. {
  125.         uint8_t reg_val;
  126.         NRF_CSN_L;
  127.         SPI_RW(reg);
  128.         reg_val = SPI_RW(0);
  129.         NRF_CSN_H;
  130.         return reg_val;
  131. }

  132. uint8_t NRF_Write_Buf(uint8_t reg,uint8_t *pBuf,uint8_t uchars)
  133. {
  134.         uint8_t i;
  135.         uint8_t status;
  136.         NRF_CSN_L;
  137.         status = SPI_RW(reg);
  138.         for(i=0;i<uchars;i++)
  139.         {
  140.                 SPI_RW(pBuf[i]);
  141.         }
  142.         NRF_CSN_H;
  143.         return status;
  144. }

  145. uint8_t NRF_Read_Buff(uint8_t reg,uint8_t *pBuf,uint8_t uchars)
  146. {
  147.         uint8_t i;
  148.         uint8_t  status;
  149.         NRF_CSN_L;
  150.         status = SPI_RW(reg);
  151.         for(i=0;i<uchars;i++)
  152.         {
  153.                         pBuf[i] = SPI_RW(0);
  154.         }
  155.         NRF_CSN_H;
  156.         return status;
  157. }

  158. uint8_t NRF24L01_RXDATA[32];
  159. uint8_t NRF34L01_TXDATA[32];
  160. static uint8_t TX_ADDRESS[5] = {0X1A,0X3B,0X5C,0X7D,0X9E};
  161. static uint8_t RX_ADDRESS[5] = {0X1A,0X3B,0X5C,0X7D,0X9E};
  162. #define TX 1
  163. #define RX 2
  164. void NRF24L01_Init(uint8_t Chanal,uint8_t Mode)
  165. {
  166.         NRF_CE_L;
  167.         NRF_Write_Reg(FLUSH_TX,0xff);
  168.         NRF_Write_Reg(FLUSH_RX,0xff);
  169.         NRF_Write_Buf(NRF_WRITE_REG + TX_ADDR, TX_ADDRESS,5);
  170.         NRF_Write_Buf(NRF_WRITE_REG + RX_ADDR_P0,RX_ADDRESS,5);
  171.         
  172.         NRF_Write_Reg(NRF_WRITE_REG + EN_AA, 0X01);
  173.         NRF_Write_Reg(NRF_WRITE_REG + EN_RXADDR, 0X01);
  174.         NRF_Write_Reg(NRF_WRITE_REG + SETUP_RETR,0X1A);
  175.         
  176.         NRF_Write_Reg(NRF_WRITE_REG + RF_CH, Chanal);
  177.         NRF_Write_Reg(NRF_WRITE_REG + RX_PW_P0, 32);
  178.         NRF_Write_Reg(NRF_WRITE_REG + RF_SETUP,0X0F);
  179.         
  180.         if(Mode == TX)
  181.                 NRF_Write_Reg(NRF_WRITE_REG + CONFIG,0X0E);
  182.         else if(Mode == RX)
  183.                 NRF_Write_Reg(NRF_WRITE_REG + CONFIG, 0X0F);
  184.         NRF_CE_H;
  185. }


  186. void uart_init()
  187. {
  188.         GPIO_InitTypeDef gpio_usart;
  189.         USART_InitTypeDef usart;
  190.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);

  191.         gpio_usart.GPIO_Pin = GPIO_Pin_9;
  192.         gpio_usart.GPIO_Speed = GPIO_Speed_50MHz;
  193.         gpio_usart.GPIO_Mode = GPIO_Mode_AF_PP;
  194.         GPIO_Init(GPIOA,&gpio_usart);
  195.         
  196.         gpio_usart.GPIO_Pin = GPIO_Pin_10;
  197.         gpio_usart.GPIO_Speed = GPIO_Speed_50MHz;
  198.         gpio_usart.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  199.         GPIO_Init(GPIOA,&gpio_usart);
  200.         
  201.         usart.USART_BaudRate = 115200;
  202.         usart.USART_WordLength = USART_WordLength_8b;
  203.         usart.USART_StopBits = USART_StopBits_1;
  204.         usart.USART_Parity = USART_Parity_No;
  205.         usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  206.         usart.USART_Mode = USART_Mode_Tx;
  207.         USART_Init(USART1,&usart);
  208.         
  209.         USART_Cmd(USART1,ENABLE);
  210.         USART_ClearFlag(USART1,USART_FLAG_TC);
  211. }

  212. void nvic_init()
  213. {
  214.         NVIC_InitTypeDef nvic;
  215.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  216.         nvic.NVIC_IRQChannel = USART1_IRQn;
  217.         nvic.NVIC_IRQChannelPreemptionPriority = 0;
  218.         nvic.NVIC_IRQChannelSubPriority= 0;
  219.         nvic.NVIC_IRQChannelCmd = ENABLE;
  220.         NVIC_Init(&nvic);
  221. }


  222. void NRF24L01_Check(void)
  223. {
  224.         uint8_t buf[5];
  225.         uint8_t i;
  226.         
  227.         NRF_Write_Buf(NRF_WRITE_REG + TX_ADDR,TX_ADDRESS,5);
  228.         NRF_Read_Buff(TX_ADDR,buf,5);
  229.         
  230.         for(i=0;i<5;i++)
  231.         {
  232.                 if(buf[i] != TX_ADDRESS[i])
  233.                 break;
  234.         }
  235.         if(i == 5)
  236.                 PrintString("\r\nNRF24L01 SUCCESS!");
  237.         else
  238.                 PrintString("\r\nNRF24L01 ERROR!");
  239. }


  240. #define RX_DR 6
  241. #define TX_DS 5
  242. #define MAX_RT 4
  243. #define TX_FULL 0

  244. static void NRF24L01_Set_TX(void)
  245. {
  246.         NRF_CE_L;
  247.         NRF_Write_Reg(NRF_WRITE_REG + CONFIG,0x0E);//·¢ËÍ
  248.         NRF_CE_H;
  249. }

  250. static void NRF24L01_Set_RX(void)
  251. {
  252.         NRF_CE_L;
  253.         NRF_Write_Reg(NRF_WRITE_REG + CONFIG,0x0F);//½ÓÊÕ
  254.         NRF_CE_H;
  255. }

  256. void NRF_Send_TX(uint8_t *tx_buf, uint8_t len)
  257. {
  258.         NRF24L01_Set_TX();
  259.         NRF_Write_Buf(WR_TX_PLOAD,tx_buf,len);
  260.         NRF_CE_H;
  261.         while(NRF_IRQ_Read);
  262.         uint8_t status = NRF_Read_Reg(NRF_READ_REG + NRFRegSTATUS);
  263.         NRF_Write_Reg(NRF_WRITE_REG + NRFRegSTATUS,status);
  264.         if(status & (1<< TX_FULL))
  265.         {
  266.                 if(status & (1 << TX_FULL))
  267.                 {
  268.                         NRF_Write_Reg(FLUSH_TX,0xff);
  269.                 }
  270.         }
  271.         if(status & (1 << TX_DS))
  272.         {
  273.                 NRF24L01_Set_RX();
  274.         }
  275. }
  276. static uint8_t data[32] = {'

  277. ,'M','>'};
  278. static void Delay(uint16_t n)
  279. {
  280.         uint16_t i,j;
  281.         for(i=0 ; i<n;i++)
  282.         for(j=0;j<8500;j++);
  283. }
  284. int main()
  285. {
  286.         Delay(5000);
  287.         uart_init();
  288.         nvic_init();
  289.         SPI1_Init();
  290.         NRF24L01_Init(40,TX);
  291.         NRF24L01_Check();
  292.         while(1)
  293.         {
  294.                 PrintString("\r\ning");
  295.                 NRF_Send_TX(data,32);
  296.                 Delay(1000);
  297.         }
  298. }
  299. void USART1_IRQHandler(void)
  300. {
  301.         if(USART1->SR & USART_SR_TC)
  302.         {
  303.                 USART1->DR = a_temp;
  304.                 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);  
  305.                 USART1->CR1 &= ~USART_CR1_TXEIE;
  306.         }
  307. }

复制代码

转载自:家安
收藏 评论0 发布时间:2023-1-4 21:13

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版