这是相关配置
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* SPI2 Periph clocks enable */
RCC_APB1PeriphClockCmd (RCC_APB1Periph_SPI2, ENABLE);
/* GPIOB clock enable */
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);
/* SCK, MISO and MOSI PB13=CLK,PB14=MISO,PB15=MOSI*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* PB12 作片选*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12); //预置为高
/* SPI1 configuration */
SPI_Cmd(SPI2, DISABLE); //必须先禁能,才能改变MODE
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //两线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //CPOL=0 时钟悬空低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //CPHA=0 数据捕获第1个
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件NSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //4分频=9M < 25M (LOW READ)
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
这是发送程序
uint8_t SPI_SendByte(uint8_t data)
{
while((SPI2->SR & SPI_I2S_FLAG_TXE)==RESET); /*等待发送寄存器空*/
SPI2->DR = data; /*发送一个字节*/
//SPI_I2S_SendData(SPI2,data);
while((SPI2->SR & SPI_I2S_FLAG_RXNE)==RESET); /* 等待接收寄存器有效*/
return(SPI2->DR);
}
在测主机mosi脚发送时,除了发送0x00之外,其他任何数据,引脚上电平一直为高。。
求解啊。。 |