GD32F405配置spi速率
时间: 2024-05-17 21:09:38 浏览: 14
GD32F405是一款高性能的MCU,支持多种外设,包括SPI模块。要配置SPI的时钟速率,可以使用SPI_BR寄存器,该寄存器的值决定了SPI时钟速率,其计算公式为:
SPI时钟速率 = 外设时钟频率 / (分频系数 × (1 + 规定的待机时间))
下面提供两种不同的方法来配置SPI速率:
1.使用库函数来配置SPI速率
```c
#include "gd32f4xx.h"
void spi_init(void) {
/* 使能 SPI 外设时钟 */
rcu_periph_clock_enable(RCU_SPI0);
/* 配置 SPI 相关 IO 口 */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
/* 配置 SPI 参数 */
spi_parameter_struct spi_init_struct;
spi_i2s_deinit(SPI0);
spi_struct_para_init(&spi_init_struct);
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.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_256;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI0, &spi_init_struct);
/* 使能 SPI */
spi_enable(SPI0);
}
int main(void) {
spi_init();
return 0;
}
```
SPI_PSC_256表示SPI时钟源频率的分频系数为256,可以根据具体需要进行调整。
2.直接使用寄存器来配置SPI速率
```c
#include "gd32f4xx.h"
void spi_init(void) {
/* 使能 SPI 外设时钟 */
rcu_periph_clock_enable(RCU_SPI0);
/* 配置 SPI 相关 IO 口 */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
/* 配置 SPI 参数 */
SPI_CTL0(SPI0) &= ~(SPI_CTL0_BIDIMODE | SPI_CTL0_BIDIOE | SPI_CTL0_CRCEN);
SPI_CTL0(SPI0) |= SPI_CTL0_SWNSS | SPI_CTL0_MSTMOD;
SPI_CTL0(SPI0) |= SPI_CTL0_CKPL | SPI_CTL0_CKPH;
SPI_CTL0(SPI0) &= ~SPI_CTL0_LSBFE;
SPI_CFG(SPI0) = SPI_CFG_MASTER | SPI_CFG_MANUALNSS | SPI_CFG_NSSSEL | SPI_CFG_FRAME8BIT;
SPI_BR(SPI0) = SPI_PSC_256;
/* 使能 SPI */
SPI_CTL0(SPI0) |= SPI_CTL0_SPIEN;
}
int main(void) {
spi_init();
return 0;
}
```
SPI_PSC_256表示SPI时钟源频率的分频系数为256,可以根据具体需要进行调整。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)