初级会员
- 积分
- 138
- 金钱
- 138
- 注册时间
- 2012-7-8
- 在线时间
- 0 小时
|
楼主 |
发表于 2015-9-26 22:30:57
|
显示全部楼层
void SPI1_Init(void)<br />
{<br />
GPIO_InitTypeDef GPIO_InitStructure;<br />
SPI_InitTypeDef SPI_InitStructure;<br />
<br />
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );<br />
RCC_APB1PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );<br />
<br />
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;<br />
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; <br />
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;<br />
GPIO_Init(GPIOA, &GPIO_InitStructure);<br />
<br />
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); <br />
<br />
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; <br />
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; <br />
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; <br />
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;<br />
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;<br />
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;<br />
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;<br />
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; <br />
SPI_InitStructure.SPI_CRCPolynomial = 7; <br />
SPI_Init(SPI1, &SPI_InitStructure); <br />
<br />
SPI_Cmd(SPI1, ENABLE); <br />
<br />
//SPI2_ReadWriteByte(0xff);<br />
<br />
<br />
} <br />
<br />
<br />
<br />
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)<br />
{<br />
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));<br />
SPI1->CR1&=0XFFC7;<br />
SPI1->CR1|=SPI_BaudRatePrescaler;<br />
SPI_Cmd(SPI1,ENABLE); <br />
//SPI2_ReadWriteByte(0xff); <br />
} <br />
<br />
u8 SPI1_ReadWriteByte(u8 TxData)<br />
{ <br />
u8 retry=0; <br />
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)<br />
{<br />
retry++;<br />
if(retry>20)return 0;<br />
} <br />
SPI_I2S_SendData(SPI1, TxData); <br />
retry=0;<br />
<br />
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)<br />
{<br />
retry++;<br />
if(retry>20)return 0;<br />
} <br />
return SPI_I2S_ReceiveData(SPI1); <br />
<br />
u8 NRF24L01_Check(void)<br />
{<br />
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};<br />
u8 i;<br />
SPI1_SetSpeed(SPI_BaudRatePrescaler_8);<br />
NRF24L01_Write_Buf(WRITE_REG_NRF+TX_ADDR,buf,5);<br />
NRF24L01_Read_Buf(TX_ADDR,buf,5);<br />
for(i=0;i<5;i++)if(buf!=0XA5)break; <br />
if(i!=5)return 1;<br />
return 0; <br />
} <br />
<br /> |
|