#include "stm32f10x_gpio.h"#include "stm32f10x_spi.h" #define FLASH_CS (1<<6)#define SPI_FLASH_CS(x) GPIOB->ODR=(GPIOB->ODR&~FLASH_CS)|(x ? FLASH_CS:0) //命令片选 // SPI总线速度设置#define SPI_SPEED_2 0#define SPI_SPEED_8 1#define SPI_SPEED_16 2#define SPI_SPEED_256 3 //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){ SPI3->CR1 &= 0XFFC7;//Fsck=Fcpu/256 if(SpeedSet == SPI_SPEED_2) //二分频 { SPI3->CR1 |= 0 << 3;//Fsck=Fpclk/2=36Mhz } else if(SpeedSet == SPI_SPEED_8) //八分频 { SPI3->CR1 |= 2 << 3;//Fsck=Fpclk/8=9Mhz } else if(SpeedSet == SPI_SPEED_16) //十六分频 { SPI3->CR1 |= 3 << 3;//Fsck=Fpclk/16=4.5Mhz } else //256分频 { SPI3->CR1 |= 7 << 3; //Fsck=Fpclk/256=281.25Khz 低速模式 } SPI3->CR1 |= 1 << 6; //SPI设备使能} //SPI口初始化//这里针是对SPI3的初始化 void SPIx_Init(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_2;//SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI3, &SPI_InitStructure); SPI_Cmd(SPI3, ENABLE); SPI3->SR |= SPI_I2S_FLAG_TXE; SPIx_SetSpeed(SPI_SPEED_2);} |