新手入门
- 积分
- 26
- 金钱
- 26
- 注册时间
- 2014-3-21
- 在线时间
- 0 小时
|
<h2><b>各位大侠:<br />
我用STM32的硬件SPI同时驱动</b>驱动SD卡和W25X16和NRF24L01,SD卡和W25X16可以工作,SPI初始化函数如下:</h2>
<div style="text-align:center;"> void SPIx_Init(void)</div>
<div style="text-align:center;">{<span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>RCC->APB2ENR|=1<<2; //PORTA时钟使能 <span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>RCC->APB2ENR|=1<<12; //SPI1时钟使能 </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>//这里只针对SPI口初始化</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>GPIOA->CRL&=0X000FFFFF; </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>GPIOA->CRL|=0XBBB00000;//PA5.6.7复用 <span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>GPIOA->ODR|=0X7<<5; //PA5.6.7上拉</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<10;//全双工模式<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<9; //软件nss管理</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<8; </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<2; //SPI主机</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<11;//8bit数据格式<span class="Apple-tab-span" style="white-space:pre;"> </span></div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<1; //空闲模式下SCK为1 CPOL=1</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<0; //数据采样从第二个时间边沿开始,CPHA=1 </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=7<<3; //Fsck=Fcpu/256</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<7; //MSBfirst </div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<6; //SPI设备使能</div>
<div style="text-align:center;"><span class="Apple-tab-span" style="white-space:pre;"> </span>SPIx_ReadWriteByte(0xff);//启动传输<span class="Apple-tab-span" style="white-space:pre;"> </span> </div>
<div style="text-align:center;">} <br />
<br />
<h2 style="text-align:left;"> NRF24L01的SPI初始化用这个才能通过:</h2>
<h2 style="text-align:center;"> void NRF_SPIx_Init(void)</h2>
<h2>{<span class="Apple-tab-span" style="white-space:pre;"> </span> </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>RCC->APB2ENR|=1<<2; //PORTA时钟使能 <span class="Apple-tab-span" style="white-space:pre;"> </span> </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>RCC->APB2ENR|=1<<12; //SPI1时钟使能 </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span> </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>//这里只针对SPI口初始化</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>GPIOA->CRL&=0X000FFFFF; </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>GPIOA->CRL|=0XBBB00000;//PA5.6.7复用 <span class="Apple-tab-span" style="white-space:pre;"> </span> </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>GPIOA->ODR|=0X7<<5; //PA5.6.7上拉</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span></h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<10;//全双工模式<span class="Apple-tab-span" style="white-space:pre;"> </span></h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<9; //软件nss管理</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<8; </h2>
<h2><br />
</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<2; //SPI主机</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<11;//8bit数据格式<span class="Apple-tab-span" style="white-space:pre;"> </span></h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>//对24L01要设置 CPHA=0;CPOL=0;</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<1; //CPOL=0时空闲模式下SCK为0 <span class="Apple-tab-span" style="white-space:pre;"> </span> </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<0; //第一个时钟的下降沿,CPHA=1 CPOL=1<span class="Apple-tab-span" style="white-space:pre;"> </span> </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=7<<3; //Fsck=Fcpu/256</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=0<<7; //MSBfirst </h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPI1->CR1|=1<<6; //SPI设备使能</h2>
<h2><span class="Apple-tab-span" style="white-space:pre;"> </span>SPIx_ReadWriteByte(0xff);//启动传输<span class="Apple-tab-span" style="white-space:pre;"> </span></h2>
<h2>} </h2>
<h2 style="text-align:left;"> 但是三个同时工作的话无线模块就不能工作了,请各位大侠告之,如何才能兼容这三个SPI器件?多谢啊!<br />
</h2>
<br />
<br />
</div> |
|