这个问题已经困扰我一个礼拜了。。。想做一个SPI1通讯SPI2的实验
然后目前有这么几个问题:
①while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)== RESET){};
SPI_I2S_SendData(SPI2,0x01);
这里在调试的时候一直卡在这。
②不知道SPI1的CS和SP2的CS该怎么搞。
贴代码:一共三个 spi1.h spi2.h main.c
①spi1.h
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource4,GPIO_AF_SPI1); //PA4¸ SPI1 CS
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PA5¸ SPI1 SCK
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PA6¸ SPI1 MISO
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PA7¸ SPI1 MOSI
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //ê1ÄüSPIíaéè
SPI1_ReadWriteByte(0xff);
}
u8 SPI1_ReadWriteByte(u8 TxData)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}//这里一直卡死
SPI_I2S_SendData(SPI1, TxData); //这里也是
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){}
return SPI_I2S_ReceiveData(SPI1);
}
②spi2.h
和上面一样,管脚定义在
PB12 CS
PB13 SCK
PB14 MISO
PB15 MOSI
③main.c
int main(void)
{
delay_init(168);
uart_init(115200);
LED_Init();
SPI1_Init();
SPI2_Init();
//若SPI之间通讯成功,那么temp的值会到GPIOF的PF9上,灯亮
while(1)
{
u16 temp=0;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)== RESET){};
SPI_I2S_SendData(SPI2,0x01);
delay_ms(3000);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)== RESET){};
if(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)==0)
temp=SPI_I2S_ReceiveData(SPI1);
GPIOF->BSRRH=temp<<9;
}
}
求大神帮我看一看,附连接图,
其中我把两个CS都接地了,其他三个管脚对应SCK-SCK MISO-MISO MOSI-MOSI
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>