spi.c

来自「一个测试LCD的应用程序」· C语言 代码 · 共 41 行

C
41
字号
// ------------- Functions for simulating Serial Peripheral Interface ----------
// Send a bit to the SPI device
void spi_sendbit(bit bitx)
{
	SPI_SCL = 0;
	if (bitx == 0)
		SPI_SDA = 0;
	else
		SPI_SDA = 1;		
	SPI_SCL = 1;
}

// Send a byte to the SPI device
void spi_sendbyte(uint8 dByte)
{
	uint8 i;
	for (i = 0; i < 8; i++)
	{
		SPI_SCL = 0;
		SPI_SDA = (dByte<<i) & 0x80;
		SPI_SCL = 1;	// Transfer a bit at rising edge of SPI_SCL
	}
}

// Read a byte from the SPI device
uint8 spi_readbyte(void)
{
	uint8 i;
	uint8 Result;
	
	for (i = 0; i < 8; i++)
	{
		SPI_SCL = 0;
		Result <<= 1;
		SPI_SCL = 1;
		if (SPI_SDA == 1)
		Result |= 0x01;
	}
	return Result;
}

⌨️ 快捷键说明

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