stm32 iic读取mpu6050失败

mpu6050使用iic一直失败。放弃治疗,使用串口。。。

#include "led.h"
#include "mpu6050.h"
#include "iic.h"

#include "string.h"

//*************************************************
unsigned char Re_buf[11],counter=0;
unsigned char sign;

static unsigned char Temp[11];

float a[3],w[3],angle[3],T;

//************************************************
void GyroscopeInit(){
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    //PB10 USART3的T PB11 USART3的R
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
    
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//据说要改成上拉输入***************************************************
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);
    
    USART_InitStructure.USART_BaudRate=115200;
    USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
    USART_InitStructure.USART_Parity=USART_Parity_No;
    USART_InitStructure.USART_StopBits=USART_StopBits_1;
    USART_InitStructure.USART_WordLength=USART_WordLength_8b;
    
    USART_Init(USART3,&USART_InitStructure);
    
    USART_Cmd(USART3,ENABLE);
    
    USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
    USART_ClearFlag(USART3, USART_FLAG_TC);
    
    NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
    NVIC_Init(&NVIC_InitStructure);
}
void USART3_IRQHandler(void)
{   
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //接收中断有效,若接收数据寄存器满
     { 
      Temp[counter] = USART_ReceiveData(USART3);   //接收数据
      if(counter == 0 && Temp[0] != 0x55) return;      //第 0 号数据不是帧头,跳过
      counter++; 
      if(counter==11) //接收到 11 个数据
      { 
         memcpy(Re_buf,Temp,11);
         counter=0; //重新赋值,准备下一帧数据的接收
         sign=1;
      }    
   }        
}




int Gyroscopetest(void){
    int AngleCar;
    unsigned char Temp[11];
   //u8 i;
   delay_ms(2);    
   while (1)
   {

       
      if(sign)
      {  
         memcpy(Temp,Re_buf,11);
         sign=0;
         if(Re_buf[0]==0x55)       //检查帧头
         {  
            switch(Re_buf[1])
            {
                /*//我只需要Z角度所以就注释啦^_^
               case 0x51: //标识这个包是加速度包
                  a[0] = ((short)(Temp[3]<<8 | Temp[2]))/32768.0*16;      //X轴加速度
                  a[1] = ((short)(Temp[5]<<8 | Temp[4]))/32768.0*16;      //Y轴加速度
                  a[2] = ((short)(Temp[7]<<8 | Temp[6]))/32768.0*16;      //Z轴加速度
                  T    = ((short)(Temp[9]<<8 | Temp[8]))/340.0+36.25;      //温度
                  break;
               case 0x52: //标识这个包是角速度包
                  w[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*2000;      //X轴角速度
                  w[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*2000;      //Y轴角速度
                  w[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*2000;      //Z轴角速度
                  T    = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25;      //温度
                  break;
               case 0x53: //标识这个包是角度包
                  angle[0] = ((short)(Temp[3]<<8| Temp[2]))/32768.0*180;   //X轴滚转角(x 轴)
                  angle[1] = ((short)(Temp[5]<<8| Temp[4]))/32768.0*180;   //Y轴俯仰角(y 轴)
                  angle[2] = ((short)(Temp[7]<<8| Temp[6]))/32768.0*180;   //Z轴偏航角(z 轴)
                  T        = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25;   //温度

                  //printf("X轴角度:%.2f   Y轴角度:%.2f   Z轴角度:%.2f\r\n",angle[0],angle[1],angle[2]);
                */
                case 0x53:
                    angle[2]=((short)(Temp[7]<<8| Temp[6]))/32768.0*180;   //Z轴偏航角(z 轴)
                    T   = ((short)(Temp[9]<<8| Temp[8]))/340.0+36.25;   //温度
                    AngleCar=angle[2];
                    printf("Z:%.2f\n",angle[2]);
                    break;
               default:  break;
            }
            //printf("X角度:%.2f  Y角度:%.2f  Z角度:%.2f  X速度:%.2f  Y速度:%.2f  Z速度:%.2f\r\n",angle[0],angle[1],angle[2],w[0],w[1],w[2]);
         }
         
      }
      
   }
   
}

猜你喜欢

转载自www.cnblogs.com/uestcman/p/9419688.html