59 lines
2.7 KiB
C
59 lines
2.7 KiB
C
#include "stm32_sys.h" // Device header
|
||
#include "spi1.h"
|
||
void MySPI_Init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
||
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
GPIO_SetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); // PB13/14/15上拉
|
||
|
||
SPI_InitTypeDef SPI_InitStructure;
|
||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // 设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
|
||
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; // 设置SPI工作模式:设置为主SPI
|
||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // 设置SPI的数据大小:SPI发送接收8位帧结构
|
||
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // 串行同步时钟的空闲状态为高电平
|
||
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // 串行同步时钟的第二个跳变沿(上升或下降)数据被采样
|
||
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
|
||
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // 定义波特率预分频的值:波特率预分频值为256
|
||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // 指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
|
||
SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC值计算的多项式
|
||
SPI_Init(SPI1, &SPI_InitStructure);
|
||
|
||
SPI_Cmd(SPI1, ENABLE);
|
||
MySPI_SwapByte(0xff);
|
||
}
|
||
|
||
void MySPI_SetSpeed(uint8_t SPI_BaudRatePrescaler)
|
||
{
|
||
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
|
||
SPI1->CR1 &= 0XFFC7;
|
||
SPI1->CR1 |= SPI_BaudRatePrescaler; // 设置SPI2速度
|
||
SPI_Cmd(SPI1, ENABLE);
|
||
}
|
||
uint8_t MySPI_SwapByte(uint8_t ByteSend)
|
||
{
|
||
u8 retry = 0;
|
||
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) // 检查指定的SPI标志位设置与否:发送缓存空标志位
|
||
{
|
||
retry++;
|
||
if (retry > 200)
|
||
return 0;
|
||
}
|
||
SPI_I2S_SendData(SPI1, ByteSend); // 通过外设SPIx发送一个数据
|
||
retry = 0;
|
||
|
||
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) // 检查指定的SPI标志位设置与否:接受缓存非空标志位
|
||
{
|
||
retry++;
|
||
if (retry > 200)
|
||
return 0;
|
||
}
|
||
return SPI_I2S_ReceiveData(SPI1); // 返回通过SPIx最近接收的数据
|
||
}
|