2022-01-18 00:58:54 +08:00

226 lines
8.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*********************************************************************************************************************
* COPYRIGHT NOTICE
* Copyright (c) 2020,<2C><><EFBFBD>ɿƼ<C9BF>
* All rights reserved.
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>QQȺ<51><C8BA><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA>824575535
*
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>У<EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>;<EFBFBD><CDBE>
* <20><>ӭ<EFBFBD><D3AD>λʹ<CEBB>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EBB1A3><EFBFBD><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC>İ<EFBFBD>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @file MPU6050
* @company <09>ɶ<EFBFBD><C9B6><EFBFBD><EFBFBD>ɿƼ<C9BF><C6BC><EFBFBD><EFBFBD>޹<EFBFBD>˾
* @author <09><><EFBFBD>ɿƼ<C9BF>(QQ3184284598)
* @version <09>鿴doc<6F><63>version<6F>ļ<EFBFBD> <20>汾˵<E6B1BE><CBB5>
* @Software ADS v1.2.2
* @Target core TC264D
* @Taobao https://seekfree.taobao.com/
* @date 2020-3-23
* @note
<09><><EFBFBD>߶<EFBFBD><DFB6>
------------------------------------
<09><><EFBFBD><EFBFBD>IIC
SCL <20>鿴SEEKFREE_IIC<49>ļ<EFBFBD><C4BC>ڵ<EFBFBD>SEEKFREE_SCL<43><EFBFBD><EAB6A8>
SDA <20>鿴SEEKFREE_IIC<49>ļ<EFBFBD><C4BC>ڵ<EFBFBD>SEEKFREE_SDA<44><EFBFBD><EAB6A8>
Ӳ<><D3B2>IIC
SCL <20>鿴init_mpu6050_hardware<72><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<49><43>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SDA <20>鿴init_mpu6050_hardware<72><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<49><43>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><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]));
//}