📄 an1069_spi.c
字号:
#include <p33fxxxx.h>
#include "AN1069.h"
/********************************************************************
* Function Name: LDByteWriteSPI *
* Parameters: address & Data *
* Description: Writes Data Byte to SPI sensor device *
* *
********************************************************************/
unsigned char LDByteWriteSPI(unsigned char LowAdd, unsigned char Data )
{
CS = 0; // Select Device
LowAdd = LowAdd | 128;
WriteSPI1 ( LowAdd ); // write address byte to sensor
WriteSPI1 ( Data ); // Write Byte to device
CS = 1; // Deselect device and initiate Write
SPI1STATbits.SPITBF = 0;
return ( 0 );
}
/********************************************************************
* Function Name: LDByteReadSPI *
* Parameters: address *
* Description: Reads data Byte from SPI sensor device. *
* *
********************************************************************/
unsigned char LDByteReadSPI(unsigned char LowAdd)
{
CS = 0; // Select Device
LowAdd = LowAdd & 127;
WriteSPI1( LowAdd ); // WRITE word address to device
ReadSPI1();
CS = 1; // Deselect Device
return ( 0 );
}
/********************************************************************
* Function Name : WriteSPI1 *
* Description : This routine writes a single byte/word to *
* the SPI bus. *
* Parameters : Single data byte/word for SPI bus *
* Return Value : None *
********************************************************************/
void WriteSPI1(unsigned int data_out)
{
SPI1CON1bits.MODE16 = 0;
SPI1BUF = data_out & 0xff; /* byte write */
while(SPI1STATbits.SPITBF);
data_out = SPI1BUF; //Avoiding overflow when reading
}
/******************************************************************************
* Function Name : ReadSPI1 *
* Description : This function will read single byte/ word from SPI *
* bus. If SPI is configured for byte communication *
* then upper byte of SPIBUF is masked. *
* Parameters : None *
* Return Value : contents of SPIBUF register *
******************************************************************************/
unsigned int ReadSPI1()
{
SPI1STATbits.SPIROV = 0;
SPI1BUF = 0x00; // initiate bus cycle
while(!SPI1STATbits.SPIRBF);
/* Check for Receive buffer full status bit of status register*/
if (SPI1STATbits.SPIRBF)
{
SPI1STATbits.SPIROV = 0;
SPI1CON1bits.MODE16 = 0;
return (SPI1BUF & 0xff); /* return byte read */
}
return -1; /* RBF bit is not set return error*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -