新手上路
- 积分
- 31
- 金钱
- 31
- 注册时间
- 2018-1-17
- 在线时间
- 10 小时
|
5金钱
最近做实验室一个项目,用STM32F103CBT6通过SPI读取MPU6000的六轴数据,本身是一个小白,因为这个项目才开始学习这方面的知识。在网上找了很多资料,基于能找到的资料、程序自己写了程序,但是始终测不出数据(偶尔出现测出的一直是255不变化)。调试了好多天还是不知道问题出在哪里。
求助各位大神,帮忙看看程序哪里有问题。
1)主程序main.c
#include "stm32f10x.h"
#include "spi.h"
#include "MPU6000.h"
#include "SysTick.h"
#include "sys.h"
#include "usart.h"
#include "delay.h"
extern uint32_t RunningTime;
long LastTime=0,NowTime;
//int16_t ax,ay,az;
//int16_t gx,gy,gz;
double ax,ay,az;
double gx,gy,gz;
int main(void)
{
SystemInit();
delay_init();
SPI1_Init();
MPU6000_Init();
SysTick_Init();
TimeStart();
while(1)
{
ax=SPI_MPU_ReadReg(MPUREG_ACCEL_XOUT_H);
ay=SPI_MPU_ReadReg(MPUREG_ACCEL_YOUT_H);
az=SPI_MPU_ReadReg(MPUREG_ACCEL_ZOUT_H);
gx=SPI_MPU_ReadReg(MPUREG_GYRO_XOUT_H);
gy=SPI_MPU_ReadReg(MPUREG_GYRO_YOUT_H);
gz=SPI_MPU_ReadReg(MPUREG_GYRO_ZOUT_H);
NowTime=RunningTime;
LastTime=NowTime;
delay_ms(500);
}
}
2)SPI.c
#include "spi.h"
#include "MPU6000.h"
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
MPU_CSN_HIGH();
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
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);
SPI_Cmd(SPI1, ENABLE);
//SPI1_ReadWriteByte(0xff);
}
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI1->CR1&=0XFFC7;
SPI1->CR1|=SPI_BaudRatePrescaler;
SPI_Cmd(SPI1,ENABLE);
}
u8 SPI1_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI1, TxData);
retry=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData(SPI1);
}
3)MPU6000.c
#include "MPU6000.h"
#include "spi.h"
double SPI_MPU_WriteReg(u8 reg,u8 dat)
{
double status;
MPU_CSN_LOW();
status = SPI1_ReadWriteByte(reg);
SPI1_ReadWriteByte(dat);
MPU_CSN_HIGH();
return(status);
}
double SPI_MPU_ReadReg(u8 reg)
{
double reg_val;
//MPU_CE_LOW();
MPU_CSN_LOW();
SPI1_ReadWriteByte(reg);
reg_val = SPI1_ReadWriteByte(reg);
MPU_CSN_HIGH();
return reg_val;
}
void MPU6000_Init(void)
{
SPI_MPU_WriteReg(MPUREG_PWR_MGMT_1,0X80);
SPI_MPU_WriteReg(MPUREG_PWR_MGMT_1,0x00);
SPI_MPU_WriteReg(MPUREG_INT_ENABLE,0X00);
SPI_MPU_WriteReg(MPUREG_USER_CTRL,0X00);
SPI_MPU_WriteReg(MPUREG_FIFO_EN,0X00);
SPI_MPU_WriteReg(MPUREG_INT_PIN_CFG,0X80);
SPI_MPU_WriteReg(MPUREG_PWR_MGMT_1,0X01);
SPI_MPU_WriteReg(MPUREG_PWR_MGMT_2,0X00);
SPI_MPU_WriteReg(MPUREG_SMPLRT_DIV,0x07);
SPI_MPU_WriteReg(MPUREG_CONFIG,0x06);
SPI_MPU_WriteReg(MPUREG_GYRO_CONFIG,0xF8);
SPI_MPU_WriteReg(MPUREG_ACCEL_CONFIG,0xE0);
}
有关的宏定义为:
#define MPUREG_ACCEL_XOUT_H 0x3B
#define MPUREG_ACCEL_XOUT_L 0x3C
#define MPUREG_ACCEL_YOUT_H 0x3D
#define MPUREG_ACCEL_YOUT_L 0x3E
#define MPUREG_ACCEL_ZOUT_H 0x3F
#define MPUREG_ACCEL_ZOUT_L 0x40
#define MPUREG_TEMP_OUT_H 0x41
#define MPUREG_TEMP_OUT_L 0x42
#define MPUREG_GYRO_XOUT_H 0x43
#define MPUREG_GYRO_XOUT_L 0x44
#define MPUREG_GYRO_YOUT_H 0x45
#define MPUREG_GYRO_YOUT_L 0x46
#define MPUREG_GYRO_ZOUT_H 0x47
#define MPUREG_GYRO_ZOUT_L 0x48
#define MPUREG_USER_CTRL 0x6A
#define MPU_CSN_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
#define MPU_CSN_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
#define MPU_CE_HIGH() GPIO_SetBits(GPIOA,GPIO_Pin_4)
#define MPU_CE_LOW() GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define MPU_Read_IRQ() GPIO_ReadInputDataBit ( GPIOA, GPIO_Pin_5)
另附上整个文件,跪求大神解答,前前后后折腾了一周多了,我能找到的资料、能想到的问题都试过了。。。。。
|
|