新手上路
- 积分
- 34
- 金钱
- 34
- 注册时间
- 2013-10-16
- 在线时间
- 2 小时
|
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">用的是STM32 107VCT的最小系统,用SPI1驱动NRF24l01的时候查看返回的STATUS寄存器是0x0E,在初始状态下这个是正确的,但是用SPI2进行操作,就遇到问题了,不管怎么操作,读取的返回值永远是0x1E,以为是NRF怀了,换了一个能用的还是有问题,然后用SPI1 SPI2通信的例程测试了下SPI2能收到正确的数据,说明还是我的配置有问题,但是实在找不出哪里来了。。。。。。。</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">SPI2初始代码:</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">void nrf_spi2_init(void)</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">{</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitTypeDef GPIO_InitStructure;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitTypeDef SPI_InitStructure;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /* SPI1 Init, before continuing, start the spio clock */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /* initialize GPIO A5.A6.A7 */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_Init(GPIOB, &GPIO_InitStructure);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /* </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO B5 --- CSN(SPI NSS chip select)</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO B6 --- CE (chip mode set)</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_5;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_Init(GPIOB, &GPIO_InitStructure);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /* </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> idle</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> NRF_LOW(NRFB_CE_PORT, NRFB_CE_PIN);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> NRF_HIGH(NRFB_CSE_PORT, NRFB_CSE_PIN);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /*</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO B1 for nRF2401 IRQ </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> GPIO_Init(GPIOB, &GPIO_InitStructure);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_Cmd(SPI2, DISABLE); //必须先禁用,才能改变MODE</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /* configure spi1 */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_Mode = SPI_Mode_Master;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_InitStructure.SPI_CRCPolynomial = 7;</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_Init(SPI2, &SPI_InitStructure);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> </span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> /* enable spi */</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_Cmd(SPI2, ENABLE);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">}</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">之后调用读写函数进行读取:</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">unsigned char spi2_readwritebyte(unsigned char tx)</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">{</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> SPI_I2S_SendData(SPI2, tx);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;"> return SPI_I2S_ReceiveData(SPI2);</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">}</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">返回值永远是0x1E,要是断开与NRF的物理连接返回0xFF</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">请大侠帮看下啊,万分感谢!</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">另,实在不行想用IO模拟SPI,但是不知道怎么下手,有高手有例程吗?</span><br style="word-wrap:break-word;color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;" />
<span style="color:#222222;font-family:sans-serif;font-size:14px;line-height:21px;background-color:#eeeeee;">多谢啊!!!!!!<br />
<br />
</span> |
|