[img]file:///C:UsersAdministratorAppDataRoamingTencentUsers286647259QQWinTempRichOleO8J2YAFA1]V{`$C[@_OOE%0.png[/img]原理图见附件。
首先是SPI初始化,代码如下:
void initSpi1(void)
{
// Specific to the STM32F405
// SPI1 Driver
// PA4 14 SPI1_NSS
// PA5 15 SPI1_SCK
// PA6 16 SPI1_MISO
// PA7 17 SPI1_MOSI
SPI_InitTypeDef spi;
GPIO_InitTypeDef GPIO_InitStructure;
// Enable SPI1 clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_PinAFConfig(SPI1_GPIO,SPI1_SCK_PIN,GPIO_AF_SPI1);
GPIO_PinAFConfig(SPI1_GPIO,SPI1_MISO_PIN,GPIO_AF_SPI1);
GPIO_PinAFConfig(SPI1_GPIO,SPI1_MOSI_PIN,GPIO_AF_SPI1);
// GPIO_PinAFConfig(SPI1_GPIO,SPI1_NSS_PIN,GPIO_AF_SPI1);
// Init pins
GPIO_InitStructure.GPIO_Pin = SPI1_SCK_PIN | SPI1_MISO_PIN | SPI1_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);
#ifdef SPI1_NSS_PIN
GPIO_InitStructure.GPIO_Pin = SPI1_NSS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);
GPIO_SetBits(SPI1_GPIO,SPI1_NSS_PIN);
#endif
// Init SPI hardware
SPI_I2S_DeInit(SPI1);
spi.SPI_Mode = SPI_Mode_Master;
spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spi.SPI_DataSize = SPI_DataSize_8b;
spi.SPI_NSS = SPI_NSS_Soft;
spi.SPI_FirstBit = SPI_FirstBit_MSB;
spi.SPI_CRCPolynomial = 7;
spi.SPI_CPOL = SPI_CPOL_High;
spi.SPI_CPHA = SPI_CPHA_2Edge;
spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_Init(SPI1, &spi);
SPI_Cmd(SPI1, ENABLE);
}
初始化完成之后可以看到SPI1的时钟已一打开,寄存器的值也没问题。
然后是MPU6000的初始化:
void mpu6000_Init(void)
{
if (mpuSpi6000InitDone) return;
initSpi1(); // 3õê¼»ˉSPI1
delay_ms(150);
spiSetDivisor(MPU6000_SPI_INSTANCE,SPI_656_KHZ_CLOCK_DIVIDER); //MPU6000Ô¤·ÖÆμ
delay_ms(150);
// MPU6000 Reset
mpu6000WriteRegister(MPU_RA_PWR_MGMT_1, BIT_H_RESET);
delay_ms(150);
mpu6000WriteRegister(MPU_RA_SIGNAL_PATH_RESET, BIT_GYRO | BIT_ACC | BIT_TEMP);
delay_ms(150);
}
最后是MPU6000写寄存器的代码
bool mpu6000WriteRegister(uint8_t reg, uint8_t data)
{
ENABLE_MPU6000;
spiTransferByte(MPU6000_SPI_INSTANCE, reg);
spiTransferByte(MPU6000_SPI_INSTANCE, data);
DISABLE_MPU6000;
return true;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>