| 本帖最后由 haohao663 于 2016-8-6 09:22 编辑 
 【HAL库每天一例】系列例程从今天开始持续更新。。。。。
 我们将坚持每天至少发布一个基于YS-F1Pro开发板的HAL库例程,
 该系列例程将带领大家从零开始使用HAL库,后面会持续添加模块应用例程。
 同样的,我们还程序发布基于HAL库的指导文档和视频教程,欢迎持续关注,并提出改进意见。
 
 例程下载:
 资料包括程序、相关说明资料以及软件使用截图
 
 
 百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1 360云盘:http://yunpan.cn/OcPiRp3wEcA92u密码 cfb6(硬石YS-F1Pro开发板HAL库例程持续更新\2. 软件设计之高级裸机例程(HAL库版本)\YSF1_HAL-118. MPU6050陀螺仪) /**
 ******************************************************************************
 *                           硬石YS-F1Pro开发板例程功能说明
 *
 *  例程名称: 3. MPU6050陀螺仪(LCD液晶显示&匿名上位机)
 *
 ******************************************************************************
 * 说明:
 * 本例程配套硬石stm32开发板YS-F1Pro使用。
 *
 * 淘宝:
 * 论坛:硬石电子社区
 * 版权归硬石嵌入式开发团队所有,请勿商用。
 ******************************************************************************
 */
 
 【1】例程简介
 I2C总线是飞利浦公司开发的两线式串行总线。用于连接微控制器和外围设备。它是同步通信的一
 种特殊形式,具有接口线少,控制方式简单,器件封装形式小,通信速率较高等优点。通过串行数据
 (SDA)线和串行时钟 (SCL)线在连接到总线的器件间传递信息。每个器件都有一个唯一的地址识
 别,而且都可以作为一个发送器或接收器
 
 【2】跳线帽情况
 ******* 为保证例程正常运行,必须插入以下跳线帽 **********
 丝印编号     IO端口      目标功能引脚        出厂默认设置
 JP1        PA10        TXD(CH340G)          已接入
 JP2        PA9         RXD(CH340G)          已接入
 
 【3】操作及现象
 使用开发板配套的MINI USB线连接到开发板标示“调试串口”字样的MIMI USB接口(需要安装驱动),
 在电脑端打开串口调试助手工具,设置参数为115200 8-N-1。下载完程序之后,将MPU6050接入I2C的
 接口,在开发板的LCD显示屏即可看到相应的数值。
 /******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
 
   
   
   
   
   
   
   
   
   
   
   
 
 
 main函数内容
 
 复制代码int main(void)
{  
  uint32_t lcdid;
  inv_error_t result;
    unsigned char accel_fsr,  new_temp = 0;
    unsigned short gyro_rate, gyro_fsr;
    unsigned long timestamp;
    struct int_param_s int_param; 
  
  HAL_Init();
  SystemClock_Config();
  
  lcdid=BSP_LCD_Init(); 
  MX_DEBUG_USART_Init();
  
  MX_SPIFlash_Init();
  
    MPU_INT_GPIO_Init();
  
    I2C_Bus_Init();
  printf("LCD ID=0x%08X\n",lcdid);
  printf("mpu 6050 test start");
  
  LCD_Clear(0,0,LCD_DEFAULT_WIDTH,LCD_DEFAULT_HEIGTH,WHITE);
  
  LCD_BK_ON();
   
  LCD_DispString_EN_CH ( 20, 20, (const uint8_t *)"This is a MPU6050 demo", BACKGROUND, BLACK,USB_FONT_24);
    result = mpu_init(&int_param);
  if (result) {
    MPL_LOGE("Could not initialize gyro.result =  %d\n",result);
    LCD_DispString_EN_CH(0,40,(const uint8_t *)"No MPU6050 detceted!Please check the hardware connection.", BACKGROUND,RED,USB_FONT_24);
  }
  else
  {
    LCD_DispString_EN_CH(30,40,(const uint8_t *)"MPU6050 decteted!", BACKGROUND,BLACK,USB_FONT_24);
  }
      result = inv_init_mpl();
      if (result) {
          MPL_LOGE("Could not initialize MPL.\n");
      }
    inv_enable_quaternion();
    inv_enable_9x_sensor_fusion();
    inv_enable_fast_nomot();
    inv_enable_gyro_tc();
#ifdef COMPASS_ENABLED
    inv_enable_vector_compass_cal();
    inv_enable_magnetic_disturbance();
#endif
    inv_enable_eMPL_outputs();
  result = inv_start_mpl();
  if (result == INV_ERROR_NOT_AUTHORIZED) {
      while (1) {
          MPL_LOGE("Not authorized.\n");
      }
  }
  if (result) {
      MPL_LOGE("Could not start the MPL.\n");
  }
#ifdef COMPASS_ENABLED
    mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);
#else
    mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL);
#endif
    mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);
    mpu_set_sample_rate(DEFAULT_MPU_HZ);
#ifdef COMPASS_ENABLED
    mpu_set_compass_sample_rate(1000 / COMPASS_READ_MS);
#endif
    mpu_get_sample_rate(&gyro_rate);
    mpu_get_gyro_fsr(&gyro_fsr);
    mpu_get_accel_fsr(&accel_fsr);
#ifdef COMPASS_ENABLED
    mpu_get_compass_fsr(&compass_fsr);
#endif
    inv_set_gyro_sample_rate(1000000L / gyro_rate);
    inv_set_accel_sample_rate(1000000L / gyro_rate);
#ifdef COMPASS_ENABLED
    inv_set_compass_sample_rate(COMPASS_READ_MS * 1000L);
#endif
    inv_set_gyro_orientation_and_scale(
            inv_orientation_matrix_to_scalar(gyro_pdata.orientation),
            (long)gyro_fsr<<15);
    inv_set_accel_orientation_and_scale(
            inv_orientation_matrix_to_scalar(gyro_pdata.orientation),
            (long)accel_fsr<<15);
#ifdef COMPASS_ENABLED
    inv_set_compass_orientation_and_scale(
            inv_orientation_matrix_to_scalar(compass_pdata.orientation),
            (long)compass_fsr<<15);
#endif
#ifdef COMPASS_ENABLED
    hal.sensors = ACCEL_ON | GYRO_ON | COMPASS_ON;
#else
    hal.sensors = ACCEL_ON | GYRO_ON;
#endif
    hal.dmp_on = 0;
    hal.report = 0;
    hal.rx.cmd = 0;
    hal.next_pedo_ms = 0;
    hal.next_compass_ms = 0;
    hal.next_temp_ms = 0;
  get_tick_count(×tamp);
    dmp_load_motion_driver_firmware();
    dmp_set_orientation(
        inv_orientation_matrix_to_scalar(gyro_pdata.orientation));
    dmp_register_tap_cb(tap_cb);
    dmp_register_android_orient_cb(android_orient_cb);
    hal.dmp_features = DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
        DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
        DMP_FEATURE_GYRO_CAL;
    dmp_enable_feature(hal.dmp_features);
    dmp_set_fifo_rate(DEFAULT_MPU_HZ);
    mpu_set_dmp_state(1);
    hal.dmp_on = 1;
 
  while(1){
      
    unsigned long sensor_timestamp;
    int new_data = 0;
    if (__HAL_USART_GET_FLAG(&husart_debug, USART_FLAG_RXNE)) {
        __HAL_USART_CLEAR_FLAG(&husart_debug, USART_FLAG_RXNE);
        handle_input();            
    }
    get_tick_count(×tamp);
 #ifdef COMPASS_ENABLED
        if ((timestamp > hal.next_compass_ms) && !hal.lp_accel_mode &&
            hal.new_gyro && (hal.sensors & COMPASS_ON)) {
            hal.next_compass_ms = timestamp + COMPASS_READ_MS;
            new_compass = 1;
        }
#endif
        if (timestamp > hal.next_temp_ms) {
            hal.next_temp_ms = timestamp + TEMP_READ_MS;
            new_temp = 1;
        }
    if (hal.motion_int_mode) {
        mpu_lp_motion_interrupt(500, 1, 5);
        inv_accel_was_turned_off();
        inv_gyro_was_turned_off();
        inv_compass_was_turned_off();
        inv_quaternion_sensor_was_turned_off();
        while (!hal.new_gyro) {}
        mpu_lp_motion_interrupt(0, 0, 0);
        hal.motion_int_mode = 0;
    }
    if (!hal.sensors || !hal.new_gyro) {
        continue;
    }    
        if (hal.new_gyro && hal.lp_accel_mode) {
            short accel_short[3];
            long accel[3];
            mpu_get_accel_reg(accel_short, &sensor_timestamp);
            accel[0] = (long)accel_short[0];
            accel[1] = (long)accel_short[1];
            accel[2] = (long)accel_short[2];
            inv_build_accel(accel, 0, sensor_timestamp);
            new_data = 1;
            hal.new_gyro = 0;
        } else if (hal.new_gyro && hal.dmp_on) {
            short gyro[3], accel_short[3], sensors;
            unsigned char more;
            long accel[3], quat[4], temperature;
            dmp_read_fifo(gyro, accel_short, quat, &sensor_timestamp, &sensors, &more);
            if (!more)
                hal.new_gyro = 0;
            if (sensors & INV_XYZ_GYRO) {
                inv_build_gyro(gyro, sensor_timestamp);
                new_data = 1;
                if (new_temp) {
                    new_temp = 0;
                    mpu_get_temperature(&temperature, &sensor_timestamp);
                    inv_build_temp(temperature, sensor_timestamp);
                }
            }
            if (sensors & INV_XYZ_ACCEL) {
                accel[0] = (long)accel_short[0];
                accel[1] = (long)accel_short[1];
                accel[2] = (long)accel_short[2];
                inv_build_accel(accel, 0, sensor_timestamp);
                new_data = 1;
            }
            if (sensors & INV_WXYZ_QUAT) {
                inv_build_quat(quat, 0, sensor_timestamp);
                new_data = 1;
            }
        } else if (hal.new_gyro) {
            short gyro[3], accel_short[3];
            unsigned char sensors, more;
            long accel[3], temperature;
            hal.new_gyro = 0;
            mpu_read_fifo(gyro, accel_short, &sensor_timestamp,
                &sensors, &more);
            if (more)
                hal.new_gyro = 1;
            if (sensors & INV_XYZ_GYRO) {
                inv_build_gyro(gyro, sensor_timestamp);
                new_data = 1;
                if (new_temp) {
                    new_temp = 0;
                    mpu_get_temperature(&temperature, &sensor_timestamp);
                    inv_build_temp(temperature, sensor_timestamp);
                }
            }
            if (sensors & INV_XYZ_ACCEL) {
                accel[0] = (long)accel_short[0];
                accel[1] = (long)accel_short[1];
                accel[2] = (long)accel_short[2];
                inv_build_accel(accel, 0, sensor_timestamp);
                new_data = 1;
            }
        }
#ifdef COMPASS_ENABLED
        if (new_compass) {
            short compass_short[3];
            long compass[3];
            new_compass = 0;
            if (!mpu_get_compass_reg(compass_short, &sensor_timestamp)) {
                compass[0] = (long)compass_short[0];
                compass[1] = (long)compass_short[1];
                compass[2] = (long)compass_short[2];
                inv_build_compass(compass, 0, sensor_timestamp);
            }
            new_data = 1;
        }
#endif
        if (new_data) {
            inv_execute_on_data();
            read_from_mpl();
        }
    }
}
  
 
 
 |