初级会员
- 积分
- 56
- 金钱
- 56
- 注册时间
- 2015-11-21
- 在线时间
- 3 小时
|
5金钱
第一个问题<br />
void NRF24L01_Init(void)<br />
{ <br />
GPIO_InitTypeDef GPIO_InitStructure;<br />
SPI_InitTypeDef SPI_InitStructure;<br />
<br />
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOG, ENABLE); //使能PB,D,G端口时钟<br />
<br />
<br />
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PB12上拉 防止W25X的干扰<br />
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出<br />
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;<br />
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化指定IO<br />
GPIO_SetBits(GPIOB,GPIO_Pin_12);//上拉 <br />
<br />
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PD2推挽输出上拉 禁止SD卡的干扰<br />
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出<br />
GPIO_SetBits(GPIOD,GPIO_Pin_2);//初始化指定IO<br />
<br />
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //PG6 7 推挽 <br />
GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化指定IO<br />
<br />
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; <br />
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PG8 输入 <br />
GPIO_Init(GPIOG, &GPIO_InitStructure);<br />
<br />
GPIO_ResetBits(GPIOG,GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8);//PG6,7,8上拉 <br />
<br />
<span style="background-color:#009900;"> </span><span style="background-color:#009900;"> SPI2_Init(); //初始化SPI </span><br />
<span style="background-color:#DFC5A4;"> </span><br />
<span style="background-color:#DFC5A4;"> SPI_Cmd(SPI2, DISABLE); // SPI外设不使能</span><br />
<br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI设置为双线双向全双工</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //SPI主机</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //发送接收8位帧结构</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟悬空低</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据捕获于第1个时钟沿</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由软件控制</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //定义波特率预分频的值:波特率预分频值为16</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从MSB位开始</span><br />
<span style="background-color:#E53333;"><span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的</span><span style="background-color:#DFC5A4;"></span><span style="background-color:#DFC5A4;">多项式</span></span><br />
<span style="background-color:#DFC5A4;"> SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器</span><br />
<span style="background-color:#009900;"><span style="background-color:#DFC5A4;"> </span><span style="background-color:#DFC5A4;"></span></span><br />
<span style="background-color:#009900;"><span style="background-color:#DFC5A4;"> SPI_Cmd(SPI2, ENABLE); //使能SPI外</span>设</span><br />
<span style="background-color:#E53333;"> </span><br />
NRF24L01_CE=0; //使能24L01<br />
NRF24L01_CSN=1; //SPI片选取消 <br />
<br />
}<br />
为什么 SPI内容初始化之后 又对SPI进行配置?<br />
<br />
第二个问题:<br />
<br />
void SPI2_Init(void)<br />
{<br />
GPIO_InitTypeDef GPIO_InitStructure;<br />
SPI_InitTypeDef SPI_InitStructure;<br />
<br />
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTB时钟使能 <br />
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能 <br />
<br />
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;<br />
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出 <br />
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;<br />
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB<br />
<br />
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15上拉<br />
<br />
<span style="background-color:#DFC5A4;">SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始</span><br />
<span style="background-color:#DFC5A4;"> SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式</span><br />
<span style="background-color:#DFC5A4;"> SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器</span><br />
<br />
SPI_Cmd(SPI2, ENABLE); //使能SPI外设<br />
<br />
SPI2_ReadWriteByte(0xff);//启动传输 <br />
<br />
一个工程里的文件为什么两个SPI配置的程序还不一样,他们的CPLO CPHA设置的不一样。<br />
<br />
ps:这是例程里的文件。<br />
<br />
<br />
<br /> |
|