小弟刚学STM32f103,用到 无线模块NRF24l01 和 SPI,搞了很久单片机都没检测到 nrf24l01
问题1:SPIx->DR=Data之后,我再用串口传SPIx->DR,发现和Data不一致,而且是乱码
问题2:SPI2的MOSI管脚(PB15)用示波器看不到波形,一直是低电平。CLK是2Mhz的矩形波
描述:管脚连接方面我检查了很多遍了,没有接错; 代码用的原子例程里的无线通信部分,用不了,做了一点点修改还是不行,
不知道我初始化有木有问题呢;
目前一直停留在查找是否存在NRF24l01的步骤。
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef *SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; // PB13:CLK PB15:MOSI
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8; //PB7:CE PB8:CSN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9: IRQ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PG9 êäèë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
SPI_Cmd(SPI2, DISABLE);
SPI_InitStructure->SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure->SPI_Mode = SPI_Mode_Master;
SPI_InitStructure->SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure->SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure->SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure->SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure->SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure->SPI_CRCPolynomial = 7;
SPI_Init(SPI2, SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
SPI2_ReadWriteByte(0xff);
}
u8 SPI2_ReadWriteByte(u8 TxData)
{
int retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
{
retry++;
if(retry>200){
return 0;}
}
SPI2->DR=TxData; =============================================这里出问题了
retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
{
retry++;
if(retry>200){
return 0;}}
return SPI2->DR;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15|GPIO_Pin_14;//SPI2的I/O使能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //SCK MOSI MISO复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线翻转速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化函数
一周热门 更多>