在线时间1114 小时
UID366506
ST金币12242
蝴蝶豆0
注册时间2012-6-25
该用户从未签到
论坛元老
- 最后登录
- 2020-7-17
|
楼主 |
发表于 2014-1-23 15:22:54
|
显示全部楼层
回复:【ST MCU实战经验】栏目开通!应用问题,官方解答,欢迎下载!
回复第 2 楼 于2014-01-23 15:10:56发表:
楼主,我想问一下,STM32F4 SPI1和SPI2自通信问题,SPI1为主模式,SPI2为从模式,可是我在设置波特率时,必须按二分频,SPI2才收得到SPI1发来的数据,如果设置为其他分频情况,将卡在while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET); 这句上,还有就是SPI1和SPI2的波特率是不是必须为相同的,才能正确通信
以下是全部代码:
#include"stm32F4xx.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void SPI_Configuration(void);
void Delay(int nCount);
int main(void)
{ RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
while(1)
{ int data=0;
SPI_SendData(SPI1,0x55);
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET);
data=SPI_ReceiveData(SPI2);
if(data==0x55)
{ while(1)
{ GPIO_SetBits(GPIOA,GPIO_Pin_4);
Delay(0xfffff);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
Delay(0xfffff);
};
}
else while(1)
{ GPIO_SetBits(GPIOA,GPIO_Pin_4);
//Delay(0xfffff);
//GPIO_ResetBits(GPIOA,GPIO_Pin_5);
//Delay(0xfffff);
};
}
}
void RCC_Configuration()
{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
}
void GPIO_Configuration()
{ GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=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_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void SPI_Configuration()
{ SPI_InitTypeDef SPI_InitStructure;
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
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_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
}
void Delay(int nCount)
{ int c1=nCount;
int c2=nCount;
for(;c1>0;c1--)
{
for(;c2>0;c2--);
};
}
先谢谢了~~
楼上的问题,看我帖子给出的提示哦~ |
|