#include "SPI.h"
/*****************************************************************/
void SPIx_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//RCC->APB2ENR|=1<<2; //PORTA时钟使能
//RCC->APB2ENR|=1<<12; //SPI1时钟使能
RCC_APB2PeriphClockCmd(
RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1, ENABLE );
//这里只针对SPI口初始化
//GPIOA->CRL&=0X000FFFFF;
//GPIOA->CRL|=0XBBB00000;//PA5.6.7复用
//GPIOA->ODR|=0X7<<5; //PA5.6.7上拉
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure I/O for Flash Chip select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //SPI CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
GPIO_SetBits(GPIOA,GPIO_Pin_2);
//SPI1->CR1|=0<<10;//全双工模式
//SPI1->CR1|=1<<9; //软件nss管理
//SPI1->CR1|=1<<8;
//SPI1->CR1|=1<<2; //SPI主机
//SPI1->CR1|=0<<11;//8bit数据格式
//SPI1->CR1|=1<<1; //空闲模式下SCK为1 CPOL=1
//SPI1->CR1|=1<<0; //数据采样从第二个时间边沿开始,CPHA=1
//SPI1->CR1|=7<<3; //Fsck=Fcpu/256
//SPI1->CR1|=0<<7; //MSBfirst
/* SPI1 configuration */
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_Hard;//SPI_NSS_Soft;
//NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
//定义波特率预分频的值:波特率预分频值为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7;
//CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
//SPI1->CR1|=1<<6; //SPI设备使能
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
SPIx_ReadWriteByte(0xff);//启动传输
}
//SPI 速度设置函数
//SpeedSet:
//SPI_SPEED_2 2分频 (SPI 36M@sys 72M)
//SPI_SPEED_8 8分频 (SPI 9M@sys 72M)
//SPI_SPEED_16 16分频 (SPI 4.5M@sys 72M)
//SPI_SPEED_256 256分频 (SPI 281.25K@sys 72M)
void SPIx_SetSpeed(u8 SpeedSet)
{
SPI1->CR1&=0XFFC7;//Fsck=Fcpu/256
if(SpeedSet==SPI_SPEED_2)//二分频
{
SPI1->CR1|=0<<3;//Fsck=Fpclk/2=36Mhz
}else if(SpeedSet==SPI_SPEED_8)//八分频
{
SPI1->CR1|=2<<3;//Fsck=Fpclk/8=9Mhz
}else if(SpeedSet==SPI_SPEED_16)//十六分频
{
SPI1->CR1|=3<<3;//Fsck=Fpclk/16=4.5Mhz
}else
//256分频
{
SPI1->CR1|=7<<3; //Fsck=Fpclk/256=281.25Khz 低速模式
}
//SPI1->CR1|=1<<6; //SPI设备使能
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
}
/*******************************************************************************/
void Spi1_GpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStruction;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA口时钟
//配置SPI1的MISO(PA.6)为浮空输入
//GPIO_InitStruction.GPIO_Pin = GPIO_Pin_6;
//GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_Init(GPIOA,&GPIO_InitStruction);
//配置SPI1的MOSI(PA.7、PA.5)为复用开漏输出
GPIO_InitStruction.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruction);
}
void SPI_Flash_config(void)
{
SPI_InitTypeDef SPI_InitStruction;
GPIO_InitTypeDef GPIO_InitStruction;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1,ENABLE); //使能GPIOA口时钟
/* Configure I/O for Flash Chip select */
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_2; //SPI CS
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_Out_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStruction);
/* Deselect the FLASH: Chip Select high */
GPIO_SetBits(GPIOA,GPIO_Pin_2);
Spi1_GpioInit();
//这里用函数和直接执行的效果居然不一样
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
SPI_InitStruction.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置为全双工模式
SPI_InitStruction.SPI_Mode = SPI_Mode_Master;
//设置为主模式
SPI_InitStruction.SPI_DataSize = SPI_DataSize_8b;
//设置数据帧为8位
SPI_InitStruction.SPI_CPOL = SPI_CPOL_High;
//空闲状态为高电平
SPI_InitStruction.SPI_CPHA = SPI_CPHA_2Edge;
//在时钟的第二个边缘开始读取数据
SPI_InitStruction.SPI_NSS = SPI_NSS_Hard;
//启动软件片选
SPI_InitStruction.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //设置波特率为时钟频率16分频
SPI_InitStruction.SPI_FirstBit = SPI_FirstBit_MSB;
//最高位先发送
SPI_InitStruction.SPI_CRCPolynomial = 7;
//CRC循环冗余为7(—_—!!!)
SPI_Init(SPI1,&SPI_InitStruction);
//设置SPI1
//SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Reset);
SPI_Cmd(SPI1,ENABLE);
SPIx_ReadWriteByte(0xff);//启动传输
}
void Spi2_GpioInit(void)
{
GPIO_InitTypeDef GPIO_InitStruction;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //使能GPIOA口时钟
//配置SPI1的MISO(PB.13,PB.15,PB.12)为浮空输入
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13 |GPIO_Pin_15;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStruction);
//配置SPI1的MOSI(PB.14)为复用开漏输出
GPIO_InitStruction.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruction);
}
void Spi2_Init(void)
{
SPI_InitTypeDef SPI_InitStruction;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
SPI_InitStruction.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置为全双工模式
SPI_InitStruction.SPI_Mode = SPI_Mode_Slave;
//设置为主模式
SPI_InitStruction.SPI_DataSize = SPI_DataSize_8b;
//设置数据帧为8位
SPI_InitStruction.SPI_CPOL = SPI_CPOL_High;
//空闲状态为高电平
SPI_InitStruction.SPI_CPHA = SPI_CPHA_2Edge;
//在时钟的第二个边缘开始读取数据
SPI_InitStruction.SPI_NSS = SPI_NSS_Hard;
//启动软件片选
SPI_InitStruction.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //设置波特率为时钟频率16分频
SPI_InitStruction.SPI_FirstBit = SPI_FirstBit_MSB;
//最高位先发送
SPI_InitStruction.SPI_CRCPolynomial = 7;
//CRC循环冗余为7(—_—!!!)
SPI_Init(SPI2,&SPI_InitStruction);
//设置SPI1
SPI_Cmd(SPI2,ENABLE);
//使能spi1
SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
}
u8 SPIx_ReadWriteByte(u8 data)
{
u8 retry=0;
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI1,data);//写入要发送的数据
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
if(retry>200)return 0;
}
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
//**********等待接收寄存器就收到数据*******************
}
Spi2_GpioInit()这个函数里面有用的语句就是这
//配置SPI1的MOSI(PA.7、PA.5)为复用开漏输出
GPIO_InitStruction.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStruction.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruction.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruction);
四句。
但是在上面的地方放函数和直接放这四句执行情况居然不一样。
直接执行的没问题,放函数编译都通不过,这是什么世道啊!~