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

📄 spi.c

📁 nordic semiconductor
💻 C
字号:
/*= spi.c =====================================================================
 *
 * Written by  Greg Hunter, University of Technology, Sydney, 2007 for sdcc compiler
 * See spi.h for details
 *
 *==============================================================================
*/

#include "regs24e1.h"
#include "spi.h"

//********************************************************
// SpiRadio
//  Initialise spi bus for radio
//********************************************************
void SpiRadio(void)
{
    SPICLK = 0;                     // Max SPI clock (XTAL/8)
    P0_DIR &= ~0x20;                // Set P0.5 as output
    P0 |= 0x20;                     // Set P0.5 high to disable spi netburner chip select
    SPI_CTRL = 0x02;                // Connect internal SPI controller to Radio
}

//********************************************************
// SpiReadWrite
//  Read and write byte on spi bus
//********************************************************
unsigned char SpiReadWrite(unsigned char b)
{
//Clear SPI interrupt
  EXIF &= ~0x20;                  
//Move byte to send to SPI data register
  SPI_DATA = b;                   
//Wait until SPI hs finished transmitting   
  while((EXIF & 0x20) == 0x00)  ;
//return data read
  return SPI_DATA;
}

//********************************************************
// SpiReadBuf
//  Read to a buffer from the spi bus
// Usage:
//  SpiReadBuf(&RXbuf,n);
//  where n is the number of bytes to be read from the spi bus
//********************************************************
void SpiReadBuf(unsigned char* buf, unsigned char n)
{
  unsigned char i;
  for (i=0;i<n;i++)
  {
    *buf = SpiReadWrite(0);
    buf++;
  }
}


//********************************************************
// SpiWriteBuf
//  Write to a buffer from the spi bus
// Usage:
//  SpiWriteBuf(&TXbuf,n);
//  where n is the number of bytes to be written to the spi bus
//********************************************************
void SpiWriteBuf(unsigned char* buf, unsigned char n)
{
  unsigned char i;
  for (i=0;i<n;i++)
  {
    SpiReadWrite(*buf);
    buf++;
  }
}

//********************************************************
// SpiReadWriteBuf
//  read and write to and from a buffer from the spi bus
// Usage:
//  SpiReadWriteBuf(&TXbuf,&RXbuf,n);
//  where n is the number of bytes to be read from and written to the spi bus
//********************************************************
void SpiReadWriteBuf(unsigned char* TXbuf, unsigned char* RXbuf, unsigned char n)
{
  unsigned char i;
  for (i=0;i<n;i++)
  {
    *RXbuf = SpiReadWrite(*TXbuf);
    RXbuf++;
    TXbuf++;
  }
}

⌨️ 快捷键说明

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