论坛元老
 
- 积分
- 4486
- 金钱
- 4486
- 注册时间
- 2013-4-22
- 在线时间
- 337 小时
|
发表于 2015-11-25 14:01:39
|
显示全部楼层
#define GPIO_SPI2_SCLK GPIOB
#define GPIO_Pin_SPI2_SCLK GPIO_Pin_13
#define GPIO_SPI2_MI GPIOB
#define GPIO_Pin_SPI2_MI GPIO_Pin_14
#define GPIO_SPI2_MO GPIOB
#define GPIO_Pin_SPI2_MO GPIO_Pin_15
#define GPIO_CS GPIOB
#define GPIO_Pin_CS GPIO_Pin_12
//------------------------------- SPI2 ????????±??¤????
#define SPI2_CS_LOW GPIO_ResetBits(GPIO_CS, GPIO_Pin_CS)
#define SPI2_CS_HIGH GPIO_SetBits(GPIO_CS, GPIO_Pin_CS)
void SPI2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
//spi?±??????·???sys.c??
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTB?±??????
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2?±??????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI2_SCLK;//CLK
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIO_SPI2_SCLK, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI2_MI;//MISO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIO_SPI2_MI, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SPI2_MO;//MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIO_SPI2_MO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CS;//????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIO_CS, &GPIO_InitStructure);
SPI2_CS_HIGH;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //?è??SPI???ò?ò?????ò??????????:SPI?è???????????ò?????¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //?è??SPI?¤×÷????:?è?????÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //?è??SPI???????ó??:SPI·???????8?????á??
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //?????????±????????×???????????
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //?????????±????????????±????¨?????ò??????????±????ù
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS???????????¨NSS???????????í???¨????SSI???????í:????NSS??????SSI??????
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //?¨???¨?????¤·???????:?¨?????¤·???????4 9M?÷??4·??? 2.25M
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //???¨??????????MSB??????LSB??????:??????????MSB??????
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?????????à????
SPI_Init(SPI2, &SPI_InitStructure); //?ù??SPI_InitStruct?????¨???????????????èSPIx?????÷
SPI_Cmd(SPI2, ENABLE);
}
这个初始化100%没有问题 |
|