金牌会员
- 积分
- 1523
- 金钱
- 1523
- 注册时间
- 2016-7-20
- 在线时间
- 155 小时
|
发表于 2017-8-10 15:10:14
|
显示全部楼层
void SPI3_Init(u8 speed)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// 使能复用端口时钟,并使用重映射功能禁用 JTAG。可使用 SWD 仿真
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);// SPI3 时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);// GPIO 时钟使能
/* Configure SPI_MASTER pins: SCK and MOSI */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);// 关闭JTAG开启SW-DP
//AFIO->MAPR = (0x00FFFFFF & AFIO->MAPR)|0x04000000;//关闭JTAG
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;// PB3,4,5复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_SetBits(GPIOB, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5); // PB3,4,5上拉
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;// SPI_Direction_1Line_Tx;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;
if(speed == 1)
{
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//18M
}else
{
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;//140K(150K-400K)
}
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE);
}
SPI3初始化 |
|