From 033eec5c165d6291f5b2bcf93089565cf8baaf83 Mon Sep 17 00:00:00 2001 From: tianjialong Date: Sun, 12 Mar 2023 11:04:38 +0800 Subject: [PATCH] update --- usersrc/usermain.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/usersrc/usermain.c b/usersrc/usermain.c index ed12970..011fccb 100644 --- a/usersrc/usermain.c +++ b/usersrc/usermain.c @@ -7,13 +7,69 @@ #define READ_CMD 0x80 #define SPI_TIMEOUT_VALUE 1000 -#define SPI_CS_Enable() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_RESET) -#define SPI_CS_Disable() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_SET) +#define SPI_CS_1() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_SET) /* */ +#define SPI_CS_0() HAL_GPIO_WritePin(SPI_SEL_GPIO_Port, SPI_SEL_Pin, GPIO_PIN_RESET) /* */ +#define SPI_SCK_1() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_SET) /* SCK = 1 */ +#define SPI_SCK_0() HAL_GPIO_WritePin(CLK_GPIO_Port, CLK_Pin, GPIO_PIN_RESET) /* SCK = 0 */ +#define SPI_MOSI_1() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_SET) /* MOSI = 1 */ +#define SPI_MOSI_0() HAL_GPIO_WritePin(MOSI_GPIO_Port, MOSI_Pin, GPIO_PIN_RESET) /* MOSI = 0 */ +#define SPI_READ_MISO() GPIO_ReadInputDataBit(MISO_GPIO_Port, MISO_Pin) /* 读MISO口线状态 */ + +#define Dummy_Byte 0xFF // 读取时MISO发送的数据,可以为任意数据 + +// SPI可以同时读取和写入数据,因此一个函数即可满足要求 +uint8_t SPI_ReadWriteByte(uint8_t txData) +{ + uint8_t i; + uint8_t rxData = 0; + + for (i = 0; i < 8; i++) + { + SPI_SCK_0(); + delay_us(1); + // 数据发送 + if (txData & 0x80) + { + SPI_MOSI_1(); + } + else + { + SPI_MOSI_0(); + } + txData <<= 1; + delay_us(1); + + SPI_SCK_1(); + delay_us(1); + // 数据接收 + rxData <<= 1; + if (SPI_READ_MISO()) + { + rxData |= 0x01; + } + delay_us(1); + } + SPI_SCK_0(); + + return rxData; +} + +// uint8_t SPI_ReadByte(void) +// { +// return SPI_ReadWriteByte(Dummy_Byte); +// } + +// void SPI_WriteByte(uint8_t txData) +// { +// (void)SPI_ReadWriteByte(txData); +// } void user_main() { uint8_t temp; + SPI_CS_Disable(); + while (1) { // HAL_UART_Transmit(&huart1, &temp, 1, 100);