spi.c

来自「ADS1240转换程序」· C语言 代码 · 共 84 行

C
84
字号
#include <hpa449lib.h>
/*****************************************************************************
**	[File]
**	spi.c
** 	[Purpose]
**	SPI routines
**	[Changes]
**	
*/

void SPI_Init(unsigned int BaudRateDivider)
{
	UCTL0 |= SWRST;
	ME1 |= USPIE0;           	// Enable USART0 SPI mode
	UTCTL0 =SSEL1+SSEL0+STC;        // SMCLK, 3-pin mode
	UCTL0 = CHAR+SYNC+MM+SWRST;
	UBR00 = BaudRateDivider & 0xff;            // UCLK/4 
	UBR10 = (BaudRateDivider >> 8) & 0xff;     // 0
	UMCTL0 = 0x00;                  // no modulation
	P3SEL |= 0x0E;                  // P3.1-3 SPI option select
	P3DIR |= 0x01;                  // P3.0 output direction
	UCTL0 &= ~SWRST;
	SPI_ReadByte();			// clear data eventually present in the receive register
}

void SPI_SendByte(unsigned char Byte2Send)
{
	// wait for transmitter to become free
	while ((IFG1 & UTXIFG0) == 0)
		;
	// write byte to send
	TXBUF0 = Byte2Send;
}

unsigned char SPI_ReadByte(void)
{
	return RXBUF0;
}

unsigned char SPI_ReadByteWait(void)
{
	// wait for byte received
	while ((IFG1 & URXIFG0) == 0)
		;	
	return RXBUF0;

}

unsigned char SPI_ReceiveByte(void)
{
	unsigned char Dummy;
	// wait for transmitter to become free
	while ((IFG1 & UTXIFG0) == 0)
		;
	// write 0 
	TXBUF0 = 0;
	// clear receiver
	while (IFG1 & URXIFG0)
		Dummy = RXBUF0;
	// wait for byte received
	while ((IFG1 & URXIFG0) == 0)
		;
	Dummy = RXBUF0;	
	return Dummy;
}

unsigned char SPI_TransferByte(unsigned char Byte2Send)
{
	unsigned char Dummy;
	// wait for transmitter to become free
	while ((IFG1 & UTXIFG0) == 0)
		;
	// write byte to send
	TXBUF0 = Byte2Send;
	// clear receiver
	while (IFG1 & URXIFG0)
		Dummy = RXBUF0;
	// wait for byte received
	while ((IFG1 & URXIFG0) == 0)
		;
	Dummy = RXBUF0;	
	return Dummy;
}

⌨️ 快捷键说明

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