1、由datasheet P34得知:CPOL=1,CPHA=1;
可以看到是在SCLK上升沿采样,SCLK下降沿改变数据,结合如下2张SPI时序图,不难发现
从上面可以得出,CPOL=0 CPHA=0与CPOL=1 CPHA=1都满足SPI时序要求,我们选择CPOL=1 CPHA=1的SPI时序
2. SPI2初始化代码如下,是基于HAL库编写
SPI_HandleTypeDef SPI2_Handler; //SPI¾ä±ú
//ÕâÀïÕëÊǶÔSPI2µÄ³õʼ»¯ CPOL=1 CPHA=1
void SPI2_Init(void)
{
SPI2_Handler.Instance=SPI2; //SP2
SPI2_Handler.Init.Mode=SPI_MODE_MASTER;
SPI2_Handler.Init.Direction=SPI_DIRECTION_2LINES;
SPI2_Handler.Init.DataSize=SPI_DATASIZE_8BIT;
SPI2_Handler.Init.CLKPolarity=SPI_POLARITY_HIGH;
SPI2_Handler.Init.CLKPhase=SPI_PHASE_2EDGE;
SPI2_Handler.Init.NSS=SPI_NSS_SOFT;
SPI2_Handler.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_256;
SPI2_Handler.Init.FirstBit=SPI_FIRSTBIT_MSB;
SPI2_Handler.Init.TIMode=SPI_TIMODE_DISABLE;
SPI2_Handler.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLE;
SPI2_Handler.Init.CRCPolynomial=7;
HAL_SPI_Init(&SPI2_Handler);
__HAL_SPI_ENABLE(&SPI2_Handler);
}
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_GPIOD_CLK_ENABLE(); //ʹÄÜGPIODʱÖÓ
__HAL_RCC_GPIOB_CLK_ENABLE(); //ʹÄÜGPIOBʱÖÓ
__HAL_RCC_SPI2_CLK_ENABLE(); //ʹÄÜSPI3ʱÖÓ
//PB14,15
GPIO_Initure.Pin=GPIO_PIN_14|GPIO_PIN_15;
GPIO_Initure.Mode=GPIO_MODE_AF_PP;
GPIO_Initure.Pull=GPIO_PULLUP;
GPIO_Initure.Speed=GPIO_SPEED_HIGH;
GPIO_Initure.Alternate=GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB,&GPIO_Initure);
//SPI2_SCK
GPIO_Initure.Pin=GPIO_PIN_3;
HAL_GPIO_Init(GPIOD,&GPIO_Initure);
}
void SPI2_SetSpeed(u16 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//ÅжÏÓÐЧÐÔ
__HAL_SPI_DISABLE(&SPI2_Handler); //¹Ø±ÕSPI
SPI2_Handler.Instance->CR1&=0XFFC7; //λ3-5ÇåÁ㣬ÓÃÀ´ÉèÖò¨ÌØÂÊ
SPI2_Handler.Instance->CR1|=SPI_BaudRatePrescaler;//ÉèÖÃSPIËÙ¶È
__HAL_SPI_ENABLE(&SPI2_Handler); //ʹÄÜSPI
}
u8 SPI2_ReadWriteByte(u8 TxData)
{
u8 Rxdata;
HAL_SPI_TransmitReceive(&SPI2_Handler,&TxData,&Rxdata,1, 1000);
return Rxdata; //·µ»ØÊÕµ½µÄÊý¾Ý
}
MPU6500的初始化及读取代码如下:
//³õʼ»¯MPU6500
//·µ»ØÖµ:0,³É¹¦
// ÆäËû,´íÎó´úÂë
u8 MPU6500_Init(void)
{
u8 res=0;
MPU6500_GPIO_Init();
SPI2_Init();
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80);//¸´Î»MPU6500
delay_ms(100); //ÑÓʱ100ms
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00);//»½ÐÑMPU6500
delay_ms(100); //ÑÓʱ100ms
//MPU_Write_Byte(MPU_SIGPATH_RST_REG,0X07); //reset GYR+ACC+TEMP
delay_ms(10); //ÑÓʱ100ms
MPU_Write_Byte(MPU_USER_CTRL_REG,0X11);SET spi mode+Reset all gyro digital signal path, accel digital signal path, and temp
delay_ms(10); //ÑÓʱ10ms
res=MPU_Read_Byte(MPU_DEVICE_ID_REG); //¶ÁÈ¡MPU6500µÄID
if(res==MPU6500_ID) //Æ÷¼þIDÕýÈ·
{
MPU_Set_Gyro_Fsr(3); //ÍÓÂÝÒÇ´«¸ÐÆ÷,¡À2000dps
delay_ms(10); //ÑÓʱ10ms
MPU_Set_Accel_Fsr(2); //¼ÓËٶȴ«¸ÐÆ÷,¡À4g
delay_ms(10); //ÑÓʱ10ms
MPU_Write_Byte(MPU_CFG_REG,0X03); //gyr Fs=1khz,bandwidth=41hz
delay_ms(10); //ÑÓʱ10ms
MPU_Write_Byte(MPU_ACCEL_CFG2_REG,0X03); //Acc Fs=1khz, bandtidth=41hz
delay_ms(10); //ÑÓʱ10ms
MPU_Write_Byte(MPU_INT_EN_REG,0X00); //¹Ø±ÕËùÓÐÖжÏ
delay_ms(10); //ÑÓʱ10ms
MPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //¹Ø±ÕFIFO
delay_ms(10); //ÑÓʱ10ms
//MPU_Write_Byte(MPU_INTBP_CFG_REG,0X82);//INTÒý½ÅµÍµçƽÓÐЧ£¬¿ªÆôbypassģʽ£¬¿ÉÒÔÖ±½Ó¶ÁÈ¡´ÅÁ¦¼Æ
delay_ms(10); //ÑÓʱ10ms
MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); //ÉèÖÃCLKSEL,PLL XÖáΪ²Î¿¼
delay_ms(10); //ÑÓʱ10ms
MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //¼ÓËÙ¶ÈÓëÍÓÂÝÒǶ¼¹¤×÷
delay_ms(10); //ÑÓʱ10ms
MPU_Set_Rate(50); //ÉèÖòÉÑùÂÊΪ50Hz
}else return 1;
return 0;
}
2、采样频率200hz,陀螺仪量程正负250dps,加速度量程正负2g,16bit输出;注意延时
3. 把MPU6500平放,在垂直方向应该有1g的加速度,测试数据如下,是正确的