初中生
最后登录1970-1-1
在线时间 小时
注册时间2016-2-21
|
配置好串口、MPU6000以及SPI后始终读取不到数据,经检测程序停在这一步
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) { }
请问该如何解决?
现贴出MPU6000的配置以及SPI配置,求解答一下。
/*********************************************************************************************************
* mpu6000.c
*********************************************************************************************************/
#include "STM32F4.h"
u8 mpu6000_buf[14];
//u8 offset_flag = 0;
u16 adress=0;
S_INT16_XYZ MPU6000_Acc_Offset = {0,0,0};
S_INT16_XYZ MPU6000_Gyro_Offset = {0,0,0};
S_INT16_XYZ MPU6000_Acc = {0,0,0};
S_INT16_XYZ MPU6000_Gyro = {0,0,0};
float mpu6000_tempreature = 0;
s16 mpu6000_tempreature_temp = 0;
s16 mpu6000_tempreature_Offset = 0;
void MPU6000_ReadValue(void)
{
uint8_t i;
MPU6000_CS(0);
SPI1_Read_Write_Byte(ACCEL_XOUT_H|0x80);
for(i = 0;i < 14;i++)
mpu6000_buf = SPI1_Read_Write_Byte(0xff);
}
MPU6000_Acc.X = BYTE16(s16, mpu6000_buf[0], mpu6000_buf[1]) - MPU6000_Acc_Offset.X;
MPU6000_Acc.Y = BYTE16(s16, mpu6000_buf[2], mpu6000_buf[3]) - MPU6000_Acc_Offset.Y;
MPU6000_Acc.Z = BYTE16(s16, mpu6000_buf[4], mpu6000_buf[5]);
MPU6000_Gyro.X = BYTE16(s16, mpu6000_buf[8], mpu6000_buf[9]) - MPU6000_Gyro_Offset.X;
MPU6000_Gyro.Y = BYTE16(s16, mpu6000_buf[10], mpu6000_buf[11]) - MPU6000_Gyro_Offset.Y;
MPU6000_Gyro.Z = BYTE16(s16, mpu6000_buf[12], mpu6000_buf[13]) - MPU6000_Gyro_Offset.Z;
mpu6000_tempreature_temp = BYTE16(s16, mpu6000_buf[6], mpu6000_buf[7]);
mpu6000_tempreature = (float)(35000+((521+mpu6000_tempreature_temp)*100)/34);
mpu6000_tempreature = mpu6000_tempreature/1000;
if(( -4 < MPU6000_Gyro.X ) && (MPU6000_Gyro.X < 4) ) MPU6000_Gyro.X = 0;
if(( -4 < MPU6000_Gyro.Y ) && (MPU6000_Gyro.Y < 4) ) MPU6000_Gyro.Y = 0;
if(( -4 < MPU6000_Gyro.Z ) && (MPU6000_Gyro.Z < 4) ) MPU6000_Gyro.Z = 0;
MPU6000_CS(1);
}
u8 MPU6000_Init(void)
{
SPI1_Init();
adress=MPU6000_Read_Reg(WHO_AM_I);
if(MPU6000_Read_Reg(WHO_AM_I) == 0x68)
{
MPU6000_Write_Reg(PWR_MGMT_1,0X80);
Delay_Ms(100);
MPU6000_Write_Reg(SIGNAL_PATH_RESET,0X07);
Delay_Ms(100);
MPU6000_Write_Reg(PWR_MGMT_1,0X01);
MPU6000_Write_Reg(PWR_MGMT_2,0X00);
MPU6000_Write_Reg(CONFIG,0X02);
MPU6000_Write_Reg(SMPLRT_DIV,0X00);
MPU6000_Write_Reg(GYRO_CONFIG,0X18);
MPU6000_Write_Reg(ACCEL_CONFIG,0x10);
return 1;
}
else return 0;
}
/*********************************************************************************************************
* mpu6000_spi.c
*********************************************************************************************************/
#include "STM32F4.h"
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE ); /
GPIO_InitStructure.GPIO_Pin = GPIO_PinSource5|GPIO_PinSource6|GPIO_PinSource7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure); //3õê¼»ˉ
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOC, &GPIO_InitStructure);
MPU6000_CS(1);
GPIO_SetBits(GPIOA,GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
SPI_Cmd(SPI1,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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI1_Read_Write_Byte(0xff);
}
u8 MPU6000_Write_Reg(uint8_t reg,uint8_t value)
{
uint8_t status;
MPU6000_CS(0);
status = SPI1_Read_Write_Byte(reg);
SPI1_Read_Write_Byte(value);
MPU6000_CS(1);
return(status);
}
u8 MPU6000_Read_Reg(uint8_t reg)
{
uint8_t reg_val;
MPU6000_CS(0);
// SPI1_Read_Write_Byte(reg | 0x80);
SPI1_Read_Write_Byte(reg);
reg_val = SPI1_Read_Write_Byte(0xff);
MPU6000_CS(1);
return(reg_val);
}
u8 SPI1_Read_Write_Byte(uint8_t TxData)
{
u8 retry = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
retry++;
if(retry > 250) return 0;
}
SPI_I2S_SendData(SPI1, TxData);
retry = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
retry++;
if(retry > 250) return 0;
}
return SPI_I2S_ReceiveData(SPI1);
}
更多
|
|