spi_readwrite.c

来自「该程序是单片机ATMEGA103通过GPIO口模拟SPI总线协议的SPI读写函数」· C语言 代码 · 共 37 行

C
37
字号
/**************************************************************************************
/*	 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 + =
减小字号Ctrl + -
显示快捷键?