gd32f103rct6将命令发送至AA01h的代码
时间: 2023-11-23 07:06:33 浏览: 60
GD32F103RCT6最小系统原理图
以下是使用GD32F103的SPI通信将命令发送至AA01h的示例代码:
```c
#include "gd32f10x_spi.h"
#include "gd32f10x_gpio.h"
#define SPIx SPI1
#define SPIx_CLK RCU_SPI1
#define SPIx_GPIO GPIOA
#define SPIx_GPIO_CLK RCU_GPIOA
#define SPIx_PIN_CS GPIO_PIN_4
#define SPIx_PIN_SCK GPIO_PIN_5
#define SPIx_PIN_MISO GPIO_PIN_6
#define SPIx_PIN_MOSI GPIO_PIN_7
#define SPIx_CS_GPIO GPIOA
#define SPIx_CS_GPIO_CLK RCU_GPIOA
void SPIx_GPIO_Config(void)
{
gpio_init(SPIx_GPIO, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, SPIx_PIN_SCK | SPIx_PIN_MOSI);
gpio_init(SPIx_GPIO, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, SPIx_PIN_MISO);
gpio_init(SPIx_CS_GPIO, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, SPIx_PIN_CS);
}
void SPIx_Config(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(SPIx_CLK);
spi_i2s_deinit(SPIx);
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_16;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPIx, &spi_init_struct);
spi_enable(SPIx);
}
void SPIx_SendByte(uint8_t byte)
{
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));
spi_i2s_data_transmit(SPIx, byte);
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_RBNE));
spi_i2s_data_receive(SPIx);
}
void send_command(uint8_t command)
{
gpio_bit_reset(SPIx_CS_GPIO, SPIx_PIN_CS); // 使能片选信号
SPIx_SendByte(command); // 发送命令
gpio_bit_set(SPIx_CS_GPIO, SPIx_PIN_CS); // 禁用片选信号
}
int main(void)
{
SPIx_GPIO_Config();
SPIx_Config();
send_command(0xAA); // 发送命令
while (1);
}
```
其中,`SPIx_GPIO_Config()`函数用于配置SPI通信的GPIO引脚,`SPIx_Config()`函数用于配置SPI通信的参数,`SPIx_SendByte()`函数用于发送一个字节的数据,`send_command()`函数则是针对本题的需求,将命令发送至AA01h。在`main()`函数中,调用`send_command(0xAA)`即可发送命令。
阅读全文