论坛大神
- 积分
- 3321
- 金钱
- 3321
- 注册时间
- 2013-7-19
- 在线时间
- 195 小时
|
发表于 2014-3-20 17:41:36
|
显示全部楼层
回复【6楼】fzhYou:
---------------------------------
#define Dummy_Byte 0xFF
/******************************************************************************************
*????????SPI_Init()
* ??????void
* ·???????void
* ??????SPIFLASH?????????????????÷??
*********************************************************************************************/
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA ,ENABLE);
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_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_I2S_DeInit(SPI1);
//SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //?????¤
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8??????????
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //??????????SCK??1
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //???????ù????2???±??±???????
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS?í?????í
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //?¨????
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //?ó??????
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?à????
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //?÷?ú????
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
/******************************************************************************************
*????????SPI_FLASH_SendByte()
* ??????u8 byte ??????????
* ·???????u8 8??????
* ??????SPIFLASH????????×??????????????÷??
*********************************************************************************************/
u8 SPI_SendByte(u8 byte)
{
/*????SPI·???????*/
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/*·???8?????? */
SPI_I2S_SendData(SPI1, byte);
/* ????????8?????? */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* ·?????????8?????? */
return SPI_I2S_ReceiveData(SPI1);
}
/******************************************************************************************
*????????SPI_FLASH_ReadByte()
* ??????void
* ·???????u8 8??????
* ??????SPIFLASH??????×??????????????÷??
*********************************************************************************************/
u8 SPI_ReadByte(void)
{
return (SPI_SendByte(Dummy_Byte));
}
/******************************************************************************************
*????????SPI_FLASH_SendHalfWord()
* ??????u16 HalfWord ??????16??????
* ·???????u16 16??????
* ??????SPIFLASH????16?????????????????÷??
*********************************************************************************************/
u16 SPI_SendHalfWord(u16 HalfWord)
{
/*????SPI·???????*/
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/*·???16?????? */
SPI_I2S_SendData(SPI1, HalfWord);
/* ????????16?????? */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/* ·?????????16?????? */
return SPI_I2S_ReceiveData(SPI1);
} |
|