mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
413 lines
16 KiB
C
413 lines
16 KiB
C
/*********************************************************************************************************************
|
||
* 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 ICM20602
|
||
* @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>壺
|
||
------------------------------------
|
||
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>
|
||
------------------------------------
|
||
********************************************************************************************************************/
|
||
|
||
|
||
#include "zf_stm_systick.h"
|
||
#include "zf_gpio.h"
|
||
#include "zf_spi.h"
|
||
#include "SEEKFREE_IIC.h"
|
||
#include "SEEKFREE_ICM20602.h"
|
||
|
||
|
||
int16 icm_gyro_x,icm_gyro_y,icm_gyro_z;
|
||
int16 icm_acc_x,icm_acc_y,icm_acc_z;
|
||
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief ICM20602<30>Լ캯<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 icm20602_self1_check(void)
|
||
{
|
||
uint8 dat=0;
|
||
while(0x12 != dat) //<2F><>ȡICM20602 ID
|
||
{
|
||
dat = simiic_read_reg(ICM20602_DEV_ADDR,ICM20602_WHO_AM_I,SIMIIC);
|
||
systick_delay_ms(STM0, 10);
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
|
||
//1 ICM20602<30><32><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>ICM20602
|
||
// @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 icm20602_init(void)
|
||
{
|
||
simiic_init();
|
||
systick_delay_ms(STM0, 10); //<2F>ϵ<EFBFBD><CFB5><EFBFBD>ʱ
|
||
|
||
//<2F><><EFBFBD><EFBFBD>
|
||
icm20602_self1_check();
|
||
|
||
//<2F><>λ
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_PWR_MGMT_1,0x80); //<2F><>λ<EFBFBD>豸
|
||
systick_delay_ms(STM0, 2); //<2F><>ʱ
|
||
while(0x80 & simiic_read_reg(ICM20602_DEV_ADDR,ICM20602_PWR_MGMT_1,SIMIIC));//<2F>ȴ<EFBFBD><C8B4><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
||
|
||
//<2F><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_PWR_MGMT_1,0x01); //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_PWR_MGMT_2,0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻͼ<C7BA><CDBC>ٶȼ<D9B6>
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_CONFIG,0x01); //176HZ 1KHZ
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_SMPLRT_DIV,0x07); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SAMPLE_RATE = INTERNAL_SAMPLE_RATE / (1 + SMPLRT_DIV)
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_GYRO_CONFIG,0x18); //<2F><>2000 dps
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_ACCEL_CONFIG,0x10); //<2F><>8g
|
||
simiic_write_reg(ICM20602_DEV_ADDR,ICM20602_ACCEL_CONFIG_2,0x03); //Average 4 samples 44.8HZ //0x23 Average 16 samples
|
||
}
|
||
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief <20><>ȡICM20602<30><32><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_icm20602_accdata(void)
|
||
{
|
||
uint8 dat[6];
|
||
|
||
simiic_read_regs(ICM20602_DEV_ADDR, ICM20602_ACCEL_XOUT_H, dat, 6, SIMIIC);
|
||
icm_acc_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
|
||
icm_acc_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
|
||
icm_acc_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
|
||
}
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief <20><>ȡICM20602<30><32><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_icm20602_gyro(void)
|
||
{
|
||
uint8 dat[6];
|
||
|
||
simiic_read_regs(ICM20602_DEV_ADDR, ICM20602_GYRO_XOUT_H, dat, 6, SIMIIC);
|
||
icm_gyro_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
|
||
icm_gyro_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
|
||
icm_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 ICM20602<30>Լ캯<D4BC><ECBAAF>
|
||
//// @param NULL
|
||
//// @return void
|
||
//// @since v1.0
|
||
//// Sample usage:
|
||
////-------------------------------------------------------------------------------------------------------------------
|
||
//void icm20602_self2_check(void)
|
||
//{
|
||
// uint8 dat=0;
|
||
// while(0x12 != dat) //<2F><>ȡICM20602 ID
|
||
// {
|
||
// iic_read_reg(IIC_NUM, ICM20602_DEV_ADDR, ICM20602_WHO_AM_I, &dat);
|
||
// systick_delay_ms(STM0, 10);
|
||
// //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
|
||
// //1 ICM20602<30><32><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>ICM20602
|
||
//// @param NULL
|
||
//// @return void
|
||
//// @since v1.0
|
||
//// Sample usage:
|
||
////-------------------------------------------------------------------------------------------------------------------
|
||
//void icm20602_init_hardware(void)
|
||
//{
|
||
// uint8 dat = 0;
|
||
// systick_delay_ms(STM0, 10); //<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
|
||
//
|
||
// //<2F><><EFBFBD><EFBFBD>
|
||
// icm20602_self2_check();
|
||
//
|
||
// //<2F><>λ
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_PWR_MGMT_1, 0x80); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
|
||
// systick_delay_ms(STM0, 2); //<2F><>ʱ
|
||
//
|
||
//
|
||
// while(0x80 & dat) //<2F>ȴ<EFBFBD><C8B4><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
||
// {
|
||
// iic_read_reg(IIC_NUM, ICM20602_DEV_ADDR, ICM20602_PWR_MGMT_1, &dat);
|
||
// systick_delay_ms(STM0, 10);
|
||
// }
|
||
//
|
||
// //<2F><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_PWR_MGMT_1, 0x01); //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_PWR_MGMT_2, 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻͼ<C7BA><CDBC>ٶȼ<D9B6>
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_CONFIG, 0x01); //176HZ 1KHZ
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_SMPLRT_DIV, 0x07); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SAMPLE_RATE = INTERNAL_SAMPLE_RATE / (1 + SMPLRT_DIV)
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_GYRO_CONFIG, 0x18); //<2F><>2000 dps
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_ACCEL_CONFIG, 0x10); //<2F><>8g
|
||
// iic_write_reg(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_ACCEL_CONFIG_2, 0x03); //Average 4 samples 44.8HZ //0x23 Average 16 samples
|
||
//
|
||
//}
|
||
//
|
||
//
|
||
////-------------------------------------------------------------------------------------------------------------------
|
||
//// @brief <20><>ȡICM20602<30><32><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_icm20602_accdata_hardware(void)
|
||
//{
|
||
// uint8 dat[6];
|
||
//
|
||
// iic_read_reg_bytes(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_ACCEL_XOUT_H, dat, 6);
|
||
// icm_acc_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
|
||
// icm_acc_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
|
||
// icm_acc_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
|
||
//}
|
||
//
|
||
////-------------------------------------------------------------------------------------------------------------------
|
||
//// @brief <20><>ȡICM20602<30><32><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_icm20602_gyro_hardware(void)
|
||
//{
|
||
// uint8 dat[6];
|
||
//
|
||
// iic_read_reg_bytes(IIC_NUM,ICM20602_DEV_ADDR, ICM20602_GYRO_XOUT_H, dat, 6);
|
||
// icm_gyro_x = (int16)(((uint16)dat[0]<<8 | dat[1]));
|
||
// icm_gyro_y = (int16)(((uint16)dat[2]<<8 | dat[3]));
|
||
// icm_gyro_z = (int16)(((uint16)dat[4]<<8 | dat[5]));
|
||
//}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// <20><><EFBFBD>º<EFBFBD><C2BA><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>Ӳ<EFBFBD><D3B2>SPIͨ<49>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>Ƚ<EFBFBD>IIC<49><43><EFBFBD>ٶȱ<D9B6>IIC<49><43><EFBFBD>dz<EFBFBD><C7B3>ࡣ
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
#define SPI_NUM SPI_0
|
||
#define SPI_SCK_PIN SPI0_SCLK_P20_11 //<2F><>ģ<EFBFBD><C4A3>SPC
|
||
#define SPI_MOSI_PIN SPI0_MOSI_P20_14 //<2F><>ģ<EFBFBD><C4A3>SDI
|
||
#define SPI_MISO_PIN SPI0_MISO_P20_12 //<2F><>ģ<EFBFBD><C4A3>SDO
|
||
#define SPI_CS_PIN SPI0_CS2_P20_13 //<2F><>ģ<EFBFBD><C4A3>CS
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief ICM20602 SPIд<49>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||
// @param cmd <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||
// @param val <20><>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
// @return void
|
||
// @since v1.0
|
||
// Sample usage:
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
void icm_spi_w_reg_byte(uint8 cmd, uint8 val)
|
||
{
|
||
uint8 dat[2];
|
||
|
||
dat[0] = cmd | ICM20602_SPI_W;
|
||
dat[1] = val;
|
||
|
||
spi_mosi(SPI_NUM,SPI_CS_PIN,dat,dat,2,1);
|
||
}
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief ICM20602 SPI<50><49><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||
// @param cmd <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||
// @param *val <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĵ<DDB5>ַ
|
||
// @return void
|
||
// @since v1.0
|
||
// Sample usage:
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
void icm_spi_r_reg_byte(uint8 cmd, uint8 *val)
|
||
{
|
||
uint8 dat[2];
|
||
|
||
dat[0] = cmd | ICM20602_SPI_R;
|
||
dat[1] = *val;
|
||
|
||
spi_mosi(SPI_NUM,SPI_CS_PIN,dat,dat,2,1);
|
||
|
||
*val = dat[1];
|
||
}
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief ICM20602 SPI<50><49><EFBFBD>ֽڶ<D6BD><DAB6>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||
// @param cmd <20>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ
|
||
// @param *val <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĵ<DDB5>ַ
|
||
// @param num <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
|
||
// @return void
|
||
// @since v1.0
|
||
// Sample usage:
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
void icm_spi_r_reg_bytes(uint8 * val, uint8 num)
|
||
{
|
||
spi_mosi(SPI_NUM,SPI_CS_PIN,val,val,num,1);
|
||
}
|
||
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief ICM20602<30>Լ캯<D4BC><ECBAAF>
|
||
// @param NULL
|
||
// @return void
|
||
// @since v1.0
|
||
// Sample usage:
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
void icm20602_self3_check(void)
|
||
{
|
||
uint8 dat=0;
|
||
while(0x12 != dat) //<2F><>ȡICM20602 ID
|
||
{
|
||
icm_spi_r_reg_byte(ICM20602_WHO_AM_I,&dat);
|
||
systick_delay_ms(STM0, 10);
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
|
||
//1 ICM20602<30><32><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>ICM20602
|
||
// @param NULL
|
||
// @return void
|
||
// @since v1.0
|
||
// Sample usage:
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
void icm20602_init_spi(void)
|
||
{
|
||
uint8 val = 0x0;
|
||
|
||
systick_delay_ms(STM0, 10); //<2F>ϵ<EFBFBD><CFB5><EFBFBD>ʱ
|
||
|
||
(void)spi_init(SPI_NUM, SPI_SCK_PIN, SPI_MOSI_PIN, SPI_MISO_PIN, SPI_CS_PIN, 3, 10*1000*1000);//Ӳ<><D3B2>SPI<50><49>ʼ<EFBFBD><CABC>
|
||
|
||
icm20602_self3_check();//<2F><><EFBFBD><EFBFBD>
|
||
|
||
icm_spi_w_reg_byte(ICM20602_PWR_MGMT_1,0x80);//<2F><>λ<EFBFBD>豸
|
||
systick_delay_ms(STM0, 2);
|
||
do
|
||
{//<2F>ȴ<EFBFBD><C8B4><EFBFBD>λ<EFBFBD>ɹ<EFBFBD>
|
||
icm_spi_r_reg_byte(ICM20602_PWR_MGMT_1,&val);
|
||
}while(0x41 != val);
|
||
|
||
icm_spi_w_reg_byte(ICM20602_PWR_MGMT_1, 0x01); //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
icm_spi_w_reg_byte(ICM20602_PWR_MGMT_2, 0x00); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻͼ<C7BA><CDBC>ٶȼ<D9B6>
|
||
icm_spi_w_reg_byte(ICM20602_CONFIG, 0x01); //176HZ 1KHZ
|
||
icm_spi_w_reg_byte(ICM20602_SMPLRT_DIV, 0x07); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SAMPLE_RATE = INTERNAL_SAMPLE_RATE / (1 + SMPLRT_DIV)
|
||
icm_spi_w_reg_byte(ICM20602_GYRO_CONFIG, 0x18); //<2F><>2000 dps
|
||
icm_spi_w_reg_byte(ICM20602_ACCEL_CONFIG, 0x10); //<2F><>8g
|
||
icm_spi_w_reg_byte(ICM20602_ACCEL_CONFIG_2, 0x03); //Average 4 samples 44.8HZ //0x23 Average 16 samples
|
||
}
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief <20><>ȡICM20602<30><32><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_icm20602_accdata_spi(void)
|
||
{
|
||
struct
|
||
{
|
||
uint8 reg;
|
||
uint8 dat[6];
|
||
}buf;
|
||
|
||
buf.reg = ICM20602_ACCEL_XOUT_H | ICM20602_SPI_R;
|
||
|
||
icm_spi_r_reg_bytes(&buf.reg, 7);
|
||
icm_acc_x = (int16)(((uint16)buf.dat[0]<<8 | buf.dat[1]));
|
||
icm_acc_y = (int16)(((uint16)buf.dat[2]<<8 | buf.dat[3]));
|
||
icm_acc_z = (int16)(((uint16)buf.dat[4]<<8 | buf.dat[5]));
|
||
}
|
||
|
||
//-------------------------------------------------------------------------------------------------------------------
|
||
// @brief <20><>ȡICM20602<30><32><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_icm20602_gyro_spi(void)
|
||
{
|
||
struct
|
||
{
|
||
uint8 reg;
|
||
uint8 dat[6];
|
||
}buf;
|
||
|
||
buf.reg = ICM20602_GYRO_XOUT_H | ICM20602_SPI_R;
|
||
|
||
icm_spi_r_reg_bytes(&buf.reg, 7);
|
||
icm_gyro_x = (int16)(((uint16)buf.dat[0]<<8 | buf.dat[1]));
|
||
icm_gyro_y = (int16)(((uint16)buf.dat[2]<<8 | buf.dat[3]));
|
||
icm_gyro_z = (int16)(((uint16)buf.dat[4]<<8 | buf.dat[5]));
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|