226 lines
8.7 KiB
C
Raw Normal View History

2022-01-18 00:58:54 +08:00
/*********************************************************************************************************************
* COPYRIGHT NOTICE
* Copyright (c) 2020,<EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD>
* All rights reserved.
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><EFBFBD>824575535
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD>;<EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>ӭ<EFBFBD><EFBFBD>λʹ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD>İ<EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @file MPU6050
* @company <EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>޹<EFBFBD>˾
* @author <EFBFBD><EFBFBD><EFBFBD>ɿƼ<EFBFBD>(QQ3184284598)
* @version <EFBFBD>doc<EFBFBD><EFBFBD>version<EFBFBD>ļ<EFBFBD> <EFBFBD>˵<EFBFBD><EFBFBD>
* @Software ADS v1.2.2
* @Target core TC264D
* @Taobao https://seekfree.taobao.com/
* @date 2020-3-23
* @note
<EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD>
------------------------------------
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC
SCL <EFBFBD>SEEKFREE_IIC<EFBFBD>ļ<EFBFBD><EFBFBD>ڵ<EFBFBD>SEEKFREE_SCL<EFBFBD><EFBFBD><EFBFBD>
SDA <EFBFBD>SEEKFREE_IIC<EFBFBD>ļ<EFBFBD><EFBFBD>ڵ<EFBFBD>SEEKFREE_SDA<EFBFBD><EFBFBD><EFBFBD>
Ӳ<EFBFBD><EFBFBD>IIC
SCL <EFBFBD>init_mpu6050_hardware<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SDA <EFBFBD>init_mpu6050_hardware<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
------------------------------------
********************************************************************************************************************/
//#include "zf_iic.h"
#include "zf_stm_systick.h"
#include "SEEKFREE_IIC.h"
#include "SEEKFREE_MPU6050.h"
int16 mpu_gyro_x,mpu_gyro_y,mpu_gyro_z;
int16 mpu_acc_x,mpu_acc_y,mpu_acc_z;
//-------------------------------------------------------------------------------------------------------------------
// @brief MPU6050<35>Լ캯<D4BC><ECBAAF>
// @param NULL
// @return void
// @since v1.0
// Sample usage: <09><><EFBFBD>øú<C3B8><C3BA><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>ģ<EFBFBD><C4A3>IIC<49>ij<EFBFBD>ʼ<EFBFBD><CABC>
//-------------------------------------------------------------------------------------------------------------------
void mpu6050_self1_check(void)
{
simiic_write_reg(MPU6050_DEV_ADDR, PWR_MGMT_1, 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
simiic_write_reg(MPU6050_DEV_ADDR, SMPLRT_DIV, 0x07); //125HZ<48><5A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(0x07 != simiic_read_reg(MPU6050_DEV_ADDR, SMPLRT_DIV,SIMIIC))
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
//1 MPU6050<35><30><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8>ʼ<EFBFBD><CABC><EFBFBD>
//2 <20><><EFBFBD>ߴ<EFBFBD><DFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>нӺ<D0BD>
//3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3.3V
//4 <20><><EFBFBD><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD>ģ<EFBFBD><C4A3>IIC<49>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
//-------------------------------------------------------------------------------------------------------------------
// @brief <20><>ʼ<EFBFBD><CABC>MPU6050
// @param NULL
// @return void
// @since v1.0
// Sample usage: <09><><EFBFBD>øú<C3B8><C3BA><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><C8B5><EFBFBD>ģ<EFBFBD><C4A3>IIC<49>ij<EFBFBD>ʼ<EFBFBD><CABC>
//-------------------------------------------------------------------------------------------------------------------
void mpu6050_init(void)
{
simiic_init();
systick_delay_ms(STM0, 100); //<2F>ϵ<EFBFBD><CFB5><EFBFBD>ʱ
mpu6050_self1_check();
simiic_write_reg(MPU6050_DEV_ADDR, PWR_MGMT_1, 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
simiic_write_reg(MPU6050_DEV_ADDR, SMPLRT_DIV, 0x07); //125HZ<48><5A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
simiic_write_reg(MPU6050_DEV_ADDR, MPU6050_CONFIG, 0x04); //
simiic_write_reg(MPU6050_DEV_ADDR, GYRO_CONFIG, 0x18); //2000
simiic_write_reg(MPU6050_DEV_ADDR, ACCEL_CONFIG, 0x10); //8g
simiic_write_reg(MPU6050_DEV_ADDR, User_Control, 0x00);
simiic_write_reg(MPU6050_DEV_ADDR, INT_PIN_CFG, 0x02);
}
//-------------------------------------------------------------------------------------------------------------------
// @brief <20><>ȡMPU6050<35><30><EFBFBD>ٶȼ<D9B6><C8BC><EFBFBD><EFBFBD><EFBFBD>
// @param NULL
// @return void
// @since v1.0
// Sample usage: ִ<>иú<D0B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӳ鿴<D3B2><E9BFB4>Ӧ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//-------------------------------------------------------------------------------------------------------------------
void get_accdata(void)
{
uint8 dat[6];
simiic_read_regs(MPU6050_DEV_ADDR, ACCEL_XOUT_H, dat, 6, SIMIIC);
mpu_acc_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
mpu_acc_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
mpu_acc_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
}
//-------------------------------------------------------------------------------------------------------------------
// @brief <20><>ȡMPU6050<35><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// @param NULL
// @return void
// @since v1.0
// Sample usage: ִ<>иú<D0B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӳ鿴<D3B2><E9BFB4>Ӧ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//-------------------------------------------------------------------------------------------------------------------
void get_gyro(void)
{
uint8 dat[6];
simiic_read_regs(MPU6050_DEV_ADDR, GYRO_XOUT_H, dat, 6, SIMIIC);
mpu_gyro_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
mpu_gyro_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
mpu_gyro_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
}
//-------------------------------------------------------------------------------------------------------------------
// <20><><EFBFBD>Ϻ<EFBFBD><CFBA><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IICͨ<43>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>Ƚ<EFBFBD>Ӳ<EFBFBD><D3B2>IIC<49><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<49><43><EFBFBD>Ÿ<EFBFBD><C5B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3AC><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨIO
//-------------------------------------------------------------------------------------------------------------------
////-------------------------------------------------------------------------------------------------------------------
//// <20><><EFBFBD>º<EFBFBD><C2BA><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>Ӳ<EFBFBD><D3B2>IICͨ<43>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD>IIC<49><43>Ӳ<EFBFBD><D3B2>IIC<49>ٶȿ<D9B6><C8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
////-------------------------------------------------------------------------------------------------------------------
//
//#define IIC_NUM IIC_1
//#define IIC_SDA_PIN IIC1_SDA_B17
//#define IIC_SCL_PIN IIC1_SCL_B16
////-------------------------------------------------------------------------------------------------------------------
//// @brief MPU6050<35>Լ캯<D4BC><ECBAAF>
//// @param NULL
//// @return void
//// @since v1.0
//// Sample usage:
////-------------------------------------------------------------------------------------------------------------------
//void mpu6050_self2_check(void)
//{
// uint8 dat=0;
//
// iic_write_reg(IIC_NUM, MPU6050_DEV_ADDR, PWR_MGMT_1, 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
// iic_write_reg(IIC_NUM, MPU6050_DEV_ADDR, SMPLRT_DIV, 0x07); //125HZ<48><5A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// while(0x07 != dat)
// {
// iic_read_reg(IIC_NUM, MPU6050_DEV_ADDR, SMPLRT_DIV, &dat);
// systick_delay_ms(STM0, 10);
// //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
// //1 MPU6050<35><30><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8>ʼ<EFBFBD><CABC><EFBFBD>
// //2 <20><><EFBFBD>ߴ<EFBFBD><DFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>нӺ<D0BD>
// //3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3.3V
// }
//}
//
////-------------------------------------------------------------------------------------------------------------------
//// @brief <20><>ʼ<EFBFBD><CABC>MPU6050
//// @param NULL
//// @return void
//// @since v1.0
//// Sample usage:
////-------------------------------------------------------------------------------------------------------------------
//void mpu6050_init_hardware(void)
//{
// systick_delay_ms(STM0, 100); //<2F>ϵ<EFBFBD><CFB5><EFBFBD>ʱ
// iic_init(IIC_NUM, IIC_SDA_PIN, IIC_SCL_PIN,400*1000); //Ӳ<><D3B2>IIC<49><43>ʼ<EFBFBD><CABC> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>400K
//
// mpu6050_self2_check();
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, PWR_MGMT_1, 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, SMPLRT_DIV, 0x07); //125HZ<48><5A><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, MPU6050_CONFIG, 0x04); //
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, GYRO_CONFIG, 0x18); //2000
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, ACCEL_CONFIG, 0x10); //8g
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, User_Control, 0x00);
// iic_write_reg(IIC_NUM,MPU6050_DEV_ADDR, INT_PIN_CFG, 0x02);
//}
//
////-------------------------------------------------------------------------------------------------------------------
//// @brief <20><>ȡMPU6050<35><30><EFBFBD>ٶȼ<D9B6><C8BC><EFBFBD><EFBFBD><EFBFBD>
//// @param NULL
//// @return void
//// @since v1.0
//// Sample usage: ִ<>иú<D0B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӳ鿴<D3B2><E9BFB4>Ӧ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
////-------------------------------------------------------------------------------------------------------------------
//void get_accdata_hardware(void)
//{
// uint8 dat[6];
//
// iic_read_reg_bytes(IIC_NUM,MPU6050_DEV_ADDR, ACCEL_XOUT_H, dat, 6);
// mpu_acc_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
// mpu_acc_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
// mpu_acc_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
//}
//
////-------------------------------------------------------------------------------------------------------------------
//// @brief <20><>ȡMPU6050<35><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//// @param NULL
//// @return void
//// @since v1.0
//// Sample usage: ִ<>иú<D0B8><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӳ鿴<D3B2><E9BFB4>Ӧ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
////-------------------------------------------------------------------------------------------------------------------
//void get_gyro_hardware(void)
//{
// uint8 dat[6];
//
// iic_read_reg_bytes(IIC_NUM,MPU6050_DEV_ADDR, GYRO_XOUT_H, dat, 6);
// mpu_gyro_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
// mpu_gyro_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
// mpu_gyro_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
//}