芯片是F070的 用的不多 前后的SPI用HAL库、标准库和软件模拟都试了,没能成功驱动 表现就是SPI读不出数据,也没发现哪出问题,因此求助。
贴上代码
/***********************************SPI部分*************************************/
extern SPI_HandleTypeDef hspi1;
//SPI初始化
void SPI_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, SPI_CLK_PIN|SPI_MISO_PIN|SPI_MOSI_PIN, GPIO_PIN_SET);
#if USE_HW_SPI
// HAL_SPI_MspInit(&hspi1);
__HAL_RCC_SPI1_CLK_ENABLE();
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
GPIO_InitStruct.Pin = SPI_CLK_PIN|SPI_MISO_PIN|SPI_MOSI_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_SPI_ENABLE(&hspi1);
#else
GPIO_InitStruct.Pin = SPI_CLK_PIN|SPI_MOSI_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI_MISO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
}
//SPI发送一个字节
u8 SPI_ReadWriteByte(u8 dat)
{
#if USE_HW_SPI
uint8_t rev_dat;
HAL_SPI_TransmitReceive(&hspi1,&dat,&rev_dat,1,1000);
return rev_dat;
#else
for(u8 i=0; i<8; i++)
{
if(dat&0x80)
SPI_MOSI_H();
else
SPI_MOSI_L();
// Delay_us(1);
dat <<= 1;
SPI_CLK_H(); //第一个时钟
// Delay_us(1);
if(SPI_MISO_READ())
dat |= 0x01;
else
;;
SPI_CLK_L();//第二个时钟
}
return dat;
#endif
}
/***********************************2401 检测部分******************************/
uint64_t temp=0,r_temp=0,w_temp=0x123456789AULL;
u8 err_sta = ERROR;
NRF24L01_CE_L();
NRF24L01_ReadBuf(READ_REG_CMD + TX_ADDR,(u8*)&temp,5); //读出数据
NRF24L01_WriteBuf(WRITE_REG_CMD + TX_ADDR,(u8*)&w_temp,5); //写入新数据
NRF24L01_ReadBuf(READ_REG_CMD + TX_ADDR,(u8*)&r_temp,5); //读出数据
if(r_temp == w_temp)
{
NRF24L01_WriteBuf(WRITE_REG_CMD + TX_ADDR,(u8*)&temp,5); //还原数据
err_sta = SUCCESS ;
}
else
err_sta = ERROR ; //MCU与NRF不正常连接
NRF24L01_CE_H();
return err_sta;
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>