对icm20602(与mpu6050差不多的6轴陀螺仪)通过spi进行数据读取失败,SCK和MOSI引脚用示波器打起都有波形,但是MISO引脚打波形一直为低电平,下面是初始化代码
void spi_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
#if SPI1_ENABLE == 1
//- SPI pins configuration ---------------------------------------------------
//- Enable GPIO clocks
RCC_AHB1PeriphClockCmd( SPI1_SCK_GPIO_RCC|
SPI1_MISO_GPIO_RCC|
SPI1_MOSI_GPIO_RCC, ENABLE);
//- Connect SPI pins to AF
GPIO_PinAFConfig(SPI1_SCK_GPIO, SPI1_SCK_SOURCE, SPI1_SCK_AF);
GPIO_PinAFConfig(SPI1_MOSI_GPIO, SPI1_MOSI_SOURCE, SPI1_MOSI_AF);
GPIO_PinAFConfig(SPI1_MISO_GPIO, SPI1_MISO_SOURCE, SPI1_MISO_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//- SPI SCK pin configuration
GPIO_InitStructure.GPIO_Pin = SPI1_SCK_PIN;
GPIO_Init(SPI1_SCK_GPIO, &GPIO_InitStructure);
//- SPI MOSI pin configuration
GPIO_InitStructure.GPIO_Pin = SPI1_MOSI_PIN;
GPIO_Init(SPI1_MOSI_GPIO, &GPIO_InitStructure);
//- SPI MISO pin configuration
GPIO_InitStructure.GPIO_Pin = SPI1_MISO_PIN;
GPIO_Init(SPI1_MISO_GPIO, &GPIO_InitStructure);
//- SPI configuration --------------------------------------------------------
//- Enable SPI clocks
RCC_APB2PeriphClockCmd(SPI1_RCC, ENABLE);
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(SPI1, &SPI_InitStructure);
//- Enable the sFLASH_SPI
SPI_Cmd(SPI1, ENABLE);
#endif
#if SPI2_ENABLE == 1
//- SPI pins configuration ---------------------------------------------------
//- Enable GPIO clocks
RCC_AHB1PeriphClockCmd( SPI2_SCK_GPIO_RCC|
SPI2_MISO_GPIO_RCC|
SPI2_MOSI_GPIO_RCC, ENABLE);
//- Connect SPI pins to AF
GPIO_PinAFConfig(SPI2_SCK_GPIO, SPI2_SCK_SOURCE, SPI2_SCK_AF);
GPIO_PinAFConfig(SPI2_MOSI_GPIO, SPI2_MOSI_SOURCE, SPI2_MOSI_AF);
GPIO_PinAFConfig(SPI2_MISO_GPIO, SPI2_MISO_SOURCE, SPI2_MISO_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
//- SPI SCK pin configuration
GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN;
GPIO_Init(SPI2_SCK_GPIO, &GPIO_InitStructure);
//- SPI MOSI pin configuration
GPIO_InitStructure.GPIO_Pin = SPI2_MOSI_PIN;
GPIO_Init(SPI2_MOSI_GPIO, &GPIO_InitStructure);
//- SPI MISO pin configuration
GPIO_InitStructure.GPIO_Pin = SPI2_MISO_PIN;
GPIO_Init(SPI2_MISO_GPIO, &GPIO_InitStructure);
//- SPI configuration --------------------------------------------------------
//- Enable SPI clocks
RCC_APB1PeriphClockCmd( SPI2_RCC, ENABLE);
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_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
//- Enable the sFLASH_SPI
SPI_Cmd(SPI2, ENABLE);
#endif
}
ICM20602用的SPI1:
初始化读取ID码代码:
/*******************************************************************************
* 函 数 名:Icm20602_Self_Check
* 功 能:ICM20602自检
* 入口参数:无
* 返 回 值:无
* 说 明:
* 例 程:
*******************************************************************************/
void Icm20602_Self_Check(void)
{
uint8_t val;
do
{
Icm_ReadReg(ICM20602_WHO_AM_I, &val, 1);
//卡在这里原因有以下几点
//1 MPU6050坏了,如果是新的这样的概率极低
//2 接线错误或者没有接好
//3 可能你需要外接上拉电阻,上拉到3.3V
}
while(ICM20602_ID != val);
}
/*******************************************************************************
* 函 数 名:Icm_ReadReg
* 功 能:读寄存器
* 入口参数:reg:寄存器地址,data:读取返回数据,len:读取数据长度
* 返 回 值:无
* 说 明:
* 例 程:
*******************************************************************************/
//读寄存器
uint8_t Icm_ReadReg(uint8_t reg, uint8_t *data, uint8_t len)
{
uint8_t reg_val = 0x00;
spi_write_read_byte(ICM_SPI, reg|ICM20602_SPI_R);
//reg_val = ICM_Write_Read_Byte(reg|ICM20602_SPI_R);
reg_val = SPI1_ReadLen(data, NULL, len);//发送null
return (reg_val);//
}
/*******************************************************************************
* 函 数 名:SPI1_ReadLen
* 功 能:读寄存器
* 入口参数:outdata:读取返回数据,indata:写入数据,len:读取数据长度
* 返 回 值:无
* 说 明:
* 例 程:
*******************************************************************************/
boolean SPI1_ReadLen(uint8_t *outdata, const uint8_t *indata, uint8_t len)
{
uint8_t b = 0x00;
while (len--)
{
b = indata ? *(indata++) : 0xFF;
while (SPI_I2S_GetFlagStatus(ICM_SPI, SPI_I2S_FLAG_TXE) == RESET){} //等待发送区空,即发送完成
SPI_I2S_SendData(ICM_SPI, b);
while (SPI_I2S_GetFlagStatus(ICM_SPI, SPI_I2S_FLAG_RXNE) == RESET){} //等待接收完一个byte
b = SPI_I2S_ReceiveData(ICM_SPI);
if (outdata)
*(outdata++) = b;
}
return 1;
}
/*******************************************************************************
* 函 数 名:uint8_t spi_write_read_byte( SPI_TypeDef* SPIx, uint8_t byte)
* 功 能:通过SPI口发送1字节数据同时接收1字节数据
* 入口参数:uint8_t byte - 待发送的1字节数据
* 返 回 值:
* 说 明:
* 例 程:
*******************************************************************************/
uint8_t spi_write_read_byte( SPI_TypeDef* SPIx, uint8_t byte)
{
//- Loop while DR register in not empty
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
//- Send byte through the SPI1 peripheral
SPI_I2S_SendData(SPIx, byte);
//- Wait to receive a byte
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
//- Return the byte read from the SPI bus
return SPI_I2S_ReceiveData(SPIx);
}
问题是读取的数据始终就是0x00,不是0x12,我都从新焊接了几块芯片了,还是这种,最开始热风枪温度为340度,我看手册说最高只能260度,最后一块芯片焊接温度240度,还是存在这种情况,究竟是没焊接好还是怎么会事,各位大神指导哈,谢谢! |
|