⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_readwrite.c

📁 该程序是单片机ATMEGA103通过GPIO口模拟SPI总线协议的SPI读写函数
💻 C
字号:
/**************************************************************************************
/*	 SPI_ReadWrite: SPI read and write
/*   Description:
/*      Writes one byte to nRF24L01, and return the byte read
/*      from nRF24L01 during write, according to SPI protocol
/*   parameters: 
/*      DataSend: the data which will be send to nRF24L01
/*   return value: data read from nRF24L01
**************************************************************************************/

UINT8 SPI_ReadWrite(UINT8 DataWrite) 
{ 
   UINT8 ByteWidth = 8;
   UINT8 DataRead  = 0;
    
   for(; ByteWidth>0; ByteWidth--) 
   { 
      if (DataWrite&0x80)    //ready for sending MSBit
         SetMOSI;            //MOSI = 1,PORTC3 set one 
      else
         ClearMOSI;	     //MOSI = 0
      DataWrite = DataWrite<<1;

      Delay(5);              //延时的长度由具体的芯片时钟和从机的时钟决定  
      SetSCK;                //SCK = 1; nRF24L01 CLOCK

      Delay(5);              //延时的长度由具体的芯片时钟和从机的时钟决定      
      DataRead = DataRead<<1;
      DataRead = DataRead|MISO; // read one bit once     
      ClearSCK;                 //SCK = 0; nRF24L01 CLOCK
      
   }
    
   return(DataRead); 
} 

/**************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -