/** ******************************** STM32F10x ********************************* * @文件名 : spi.c * @作者 : sun * @库版本 : V3.5.0 * @文件版本 : V1.0.0 * @日期 : 2016年05月09日 * @摘要 : SPI源文件 ******************************************************************************/ /*---------------------------------------------------------------------------- 更新日志: 2016-05-09 V1.0.0:初始版本 ----------------------------------------------------------------------------*/ /* 包含的头文件 --------------------------------------------------------------*/ #include "spi.h" /************************************************ 函数名称 : SPI_GPIO_Configuration 功 能 : SPI引脚配置 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void SPI_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE ); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_4); //GPIO_SetBits(GPIOA, GPIO_Pin_4|GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7); } /************************************************ 函数名称 : SPI_Configuration 功 能 : SPI配置 SPI为主模式,时钟线平时为低,上升沿采集数据 8位数据格式,软件控制片选,数据高位在前 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 72/8 MHz SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); SPI_SSOutputCmd(SPI1, ENABLE); } /************************************************ 函数名称 : SPI_Initializes 功 能 : SPI初始化 参 数 : 无 返 回 值 : 无 作 者 : sun *************************************************/ void SPI_Initializes(void) { SPI_GPIO_Configuration(); SPI_Configuration(); } /************************************************ 函数名称 : SPI_WriteReadByte 功 能 : 对SPI写读字节数据 参 数 : TxData --- 发送的字节数据 返 回 值 : 读回来的字节数据 作 者 : sun *************************************************/ uint8_t SPI_WriteReadByte(uint8_t TxData) { while((SPI1->SR & SPI_I2S_FLAG_TXE) == (uint16_t)RESET); SPI1->DR = TxData; while((SPI1->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET); return SPI1->DR; } /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/