新手上路
- 积分
- 34
- 金钱
- 34
- 注册时间
- 2015-9-17
- 在线时间
- 0 小时
|
5金钱
<span></span><span>//初始化24L01的IO口</span><br />
<span></span><br />
<span></span><br />
<span>void NRF24L01_Init(void)</span><br />
<span>{ GPIO_InitTypeDef GPIO_InitStruct;</span><br />
<span>SPI_InitTypeDef SPI_InitStruct;</span><br />
<br />
<span>RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB ,ENABLE);</span><br />
<span>RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE); //spi2时钟</span><br />
<br />
<span>GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;</span><br />
<span>GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;</span><br />
<span>GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;</span><br />
<span> </span><br />
<span>GPIO_Init(GPIOB, &GPIO_InitStruct);</span><br />
<span>// GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15上拉</span><br />
<br />
<span> </span><br />
<span>RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);</span><br />
<span>/*CE CSN Initial*/</span><br />
<span>GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 ;</span><br />
<span>GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;</span><br />
<span>GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;</span><br />
<span>GPIO_Init(GPIOE, &GPIO_InitStruct); </span><br />
<br />
<span>GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 ;</span><br />
<span>GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;</span><br />
<span>GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;</span><br />
<span>GPIO_Init(GPIOE, &GPIO_InitStruct); </span><br />
<br />
<br />
<span>/*IRQ CSN Initial*/</span><br />
<span>GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;</span><br />
<span>GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;</span><br />
<span>GPIO_Init(GPIOE, &GPIO_InitStruct); </span><br />
<br />
<br />
<br />
<br />
<span>SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;</span><br />
<span>SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex;</span><br />
<span>SPI_InitStruct.SPI_Mode = SPI_Mode_Master;</span><br />
<span>SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;</span><br />
<span>SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;</span><br />
<span>SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;</span><br />
<span>SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;</span><br />
<span>SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;</span><br />
<span>SPI_InitStruct.SPI_CRCPolynomial = 7;</span><br />
<span>SPI_Init(SPI2, &SPI_InitStruct);</span><br />
<br />
<span>SPI_Cmd(SPI2, ENABLE);</span><br />
<br />
<br />
<span>// SPI1_Init(); //初始化SPI</span><br />
<span>NRF24L01_CE=0; //使能24L01</span><br />
<span>NRF24L01_CSN=1; //SPI片选取消 </span><br />
<span>}</span><br />
<span></span><br />
<span>//========================================spi的代码</span><br />
<span></span><br />
<span></span><span>void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)</span><br />
<span>{</span><br />
<span> assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));</span><br />
<span>SPI2->CR1&=0XFFC7;</span><br />
<span>SPI2->CR1|=SPI_BaudRatePrescaler; //设置SPI2速度 </span><br />
<span>SPI_Cmd(SPI2,ENABLE); </span><br />
<br />
<span>} </span><br />
<br />
<span>//SPIx 读写一个字节</span><br />
<span>//TxData:要写入的字节</span><br />
<span>//返回值:读取到的字节</span><br />
<span>u8 SPI1_ReadWriteByte(u8 TxData)</span><br />
<span>{ </span><br />
<span>u8 retry=0; </span><br />
<span>while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位</span><br />
<span>{</span><br />
<span>retry++;</span><br />
<span>if(retry>200)return 0;</span><br />
<span>} </span><br />
<span>SPI_I2S_SendData(SPI2, TxData); //通过外设SPIx发送一个数据</span><br />
<span>retry=0;</span><br />
<br />
<span>while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位</span><br />
<span>{</span><br />
<span>retry++;</span><br />
<span>if(retry>200)return 0;</span><br />
<span>} </span><br />
<span>return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据 </span><br />
<span>}</span><br />
<div>
//======================================================主程序<br />
<br />
<br />
int main(void)<br />
{ <br />
RCC_Configuratio(); // 使能各模块时钟<br />
delay_init(); //初始化延时系统<br />
GPIO_Configuration(); //初始化IO口<br />
ADC1_Configuration(); //初始化AD<br />
DMA_Configuration();<br />
ADC_SoftwareStartConvCmd(ADC1, ENABLE);<br />
DMA_Cmd(DMA1_Channel1, ENABLE); //启动DMA通道<br />
<br />
NRF24L01_Init(); //初始化NRF24L01 <br />
<br />
while(NRF24L01_Check());//检测不到24L01 //一直卡在这里,不能跳出循环<br />
<div>
<br />
</div>
<br />
</div>
<span></span> |
|