Code MPU6050
Code MPU6050
/*
*@Note
*7-bit addressing mode, master/slave mode, transceiver routine:
*I2C1_SCL(PC2)\I2C1_SDA(PC1).
*This routine demonstrates that Master sends and Slave receives.
*Note: The two boards download the Master and Slave programs respectively,
* and power on at the same time.
* Hardware connection:
* PC2 -- PC2
* PC1 -- PC1
*
Mpu6050 : Id Address = 0x68 wrt , 0x69 rd
: MPU6050 003 :
- SystemClk:48000000
> IIC_Init() : bound=100000 , address=0x68 :
< IIC_Init() .
> MPU6050_Write() :
= MPU6050_Write() -> MPU_Set_Gyro_Fsr()
= MPU6050_Write() -> MPU_Set_Accel_Fsr()
= MPU6050_Write() -> MPU_Set_Rate()
= MPU6050_Write() ...
< MPU6050_Write() .
> mpu6050 Test ...
Temperature:2849
ACC : X=14846 , Y=292 , Z=-10048
GYRO : X=804 , Y=3665 , Z=-53
Temperature:2850
ACC : X=16938 , Y=-108 , Z=-4118
GYRO : X=-2373 , Y=7016 , Z=1027
*/
/*
*@Note
I2C interface routine to operate EEPROM peripheral:
I2C1_SCL(PC2)\I2C1_SDA(PC1).
This example uses EEPROM for AT24Cxx series.
Steps:
READ EEPROM:Start + 0xA0 + 8bit Data Address + Start + 0xA1 + Read Data + Stop.
WRITE EERPOM:Start + 0xA0 + 8bit Data Address + Write Data + Stop.
*/
#include "debug.h"
///
///
#define __MPU6050_H
//MPU6050 AD0
#define MPU_AD0_H GPIO_SetBits( GPIOC,GPIO_Pin_1) // SDA
#define MPU_AD0_L GPIO_ResetBits(GPIOC,GPIO_Pin_1) // SDA
u8 MPU_Set_Gyro_Fsr(u8 fsr);
u8 MPU_Set_Accel_Fsr(u8 fsr);
u8 MPU_Set_LPF(u16 lpf);
u8 MPU_Set_Rate(u16 rate);
short MPU_Get_Temperature(void);
u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz);
u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az);
u8 MPU_Set_Gyro_Fsr(u8 fsr)
{
// return( MPU_Write_Byte( MPU_GYRO_CFG_REG , fsr << 3 ) );//
AT24CXX_WriteOneByte( MPU_GYRO_CFG_REG , fsr << 3 ) ;
return( 0 );
}
u8 MPU_Set_Accel_Fsr(u8 fsr)
{
// return MPU_Write_Byte( MPU_ACCEL_CFG_REG , fsr << 3 );//
AT24CXX_WriteOneByte( MPU_ACCEL_CFG_REG , fsr << 3 ) ;
return( 0 );
}
u8 MPU_Set_LPF(u16 lpf)
{
u8 data = 0;
u8 MPU_Set_Rate(u16 rate)
{
u8 data;
short MPU_Get_Temperature(void)
{
u8 buf[2];
short raw;
float temp;
return( temp);
}
// // if( res == 0 )
{
*gx = ((u16)buf[0]<<8) | buf[1];
*gy = ((u16)buf[2]<<8) | buf[3];
*gz = ((u16)buf[4]<<8) | buf[5];
return res;;
}
u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az)
{
u8 buf[6] , res = 0 ;
// // if( res == 0 )
{
*ax = ((u16)buf[0]<<8) | buf[1];
*ay = ((u16)buf[2]<<8) | buf[3];
*az = ((u16)buf[4]<<8) | buf[5];
*ax +=*ax; *ax = *ax/1000; //loc gia tri goc quay tới
*ay +=*ay; *ay = *ay/1000; //loc gia tri goc trái phải
*az +=*az; *az = *az/1000; //lọc giá trị góc
return res;;
}
int MPU6050_TEST(void)
{
short aacx,aacy,aacz;
short gyrox,gyroy,gyroz;
short temp;
while( 1 )
{
temp = MPU_Get_Temperature(); //
MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //
//MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //
Delay_Ms(500);
}
}
///
/**********************************************************************
*@Note:
AT24Cxx
READ EEPROM Start + 0xA0 + 8bit Data Address + Start + 0xA1 + Read Data + Stop.
WRITE EERPOM Start + 0xA0 + 8bit Data Address + Write Data + Stop.
*******************************************************************************/
/* EERPOM DATA ADDRESS Length Definition */
#define Address_8bit 0
#define Address_16bit 1
/* Global define */
#define SIZE sizeof(TEXT_Buffer)
/* Global Variable */
const u8 TEXT_Buffer[]={"CH32V00x I2C TEST"};
/*********************************************************************
* @fn IIC_Init
* @brief Initializes the IIC peripheral.
* @return none
*/
void IIC_Init(u32 bound, u16 address)
{
printf("> IIC_Init() : bound=%d , address=%02X :\r\n" , bound , address );
GPIO_InitTypeDef GPIO_InitStructure={0};
I2C_InitTypeDef I2C_InitTSturcture={0};
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC, &GPIO_InitStructure );
I2C_InitTSturcture.I2C_ClockSpeed = bound;
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitTSturcture.I2C_OwnAddress1 = address;
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init( I2C1, &I2C_InitTSturcture );
/*********************************************************************
* @fn AT24CXX_Init
* @brief Initializes AT24xx EEPROM.
* @return none
*/
void MPU6050_Init(void) //void AT24CXX_Init(void)
{
IIC_Init( 100000, 0x68 ); //IIC_Init( 100000, 0xA0 );
}
/*********************************************************************
* @fn AT24CXX_ReadOneByte
* @brief Read one data from EEPROM.
* @param ReadAddr - Read first address.
* @return temp - Read data.
*/
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp=0;
#endif
return temp;
}
/*********************************************************************
* @fn AT24CXX_WriteOneByte
* @brief Write one data to EEPROM.
* @param WriteAddr - Write frist address.
* @return DataToWrite - Write data.
*/
void AT24CXX_WriteOneByte(u16 WriteAddr, u8 DataToWrite)
{
while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );
I2C_GenerateSTART( I2C1, ENABLE );
#endif
/*********************************************************************
* @fn AT24CXX_Read
* @brief Read multiple data from EEPROM.
* @param ReadAddr - Read frist address. (AT24c02: 0~255)
* pBuffer - Read data.
* NumToRead - Data number.
* @return none
*/
void AT24CXX_Read(u16 ReadAddr, u8 *pBuffer, u16 NumToRead)
{
while( NumToRead )
{
* pBuffer ++ = AT24CXX_ReadOneByte( ReadAddr ++ );
NumToRead -- ;
}
}
/*********************************************************************
* @fn AT24CXX_Write
* @brief Write multiple data to EEPROM.
* @param WriteAddr - Write frist address. (AT24c02: 0~255)
* pBuffer - Write data.
* NumToWrite - Data number.
* @return none
*/
void MPU6050_Write()
{
printf("> MPU6050_Write() : \r\n");
// // MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //MPU6050
AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X80); //MPU6050
Delay_Ms(100);
// // MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //MPU6050
AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X00); //MPU6050
// Delay_Ms(1);
// // MPU_Write_Byte(MPU_INT_EN_REG,0x00); //
AT24CXX_WriteOneByte(MPU_INT_EN_REG,0); //
// // MPU_Write_Byte(MPU_USER_CTRL_REG,0x00); //I2C
AT24CXX_WriteOneByte(MPU_USER_CTRL_REG,0); //I2C
// // MPU_Write_Byte(MPU_FIFO_EN_REG,0x00); //FIFO
AT24CXX_WriteOneByte(MPU_FIFO_EN_REG,0); //FIFO
// // MPU_Write_Byte(MPU_INTBP_CFG_REG,0x80); //INT
AT24CXX_WriteOneByte(MPU_INTBP_CFG_REG,1); //INT
// // int res ;
// // res = MPU_Read_Byte(MPU_DEVICE_ID_REG);
AT24CXX_ReadOneByte( MPU_DEVICE_ID_REG );
// // if( res == MPU_ADDR ) // ID
{
// // MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); // CLKSEL,PLL X
AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,1); // CLKSEL,PLL X
// // MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //
AT24CXX_WriteOneByte(MPU_PWR_MGMT2_REG,0); //
MPU_Set_Rate(50); // 50Hz
}
// return 1;
/*********************************************************************
* @fn main
* @brief Main program.
* @return none
*/
int main(void)
{
// u8 data[SIZE];
Delay_Init();
USART_Printf_Init(115200);
// // printf(": eeprom 24Cxx :\r\n");
printf("\r\n\r\n: MPU6050 003 :\r\n");
printf("- SystemClk:%d\r\n",SystemCoreClock);
MPU6050_Init();
Delay_Ms(500);
MPU6050_Write();
Delay_Ms(500);
while( 1 )
{
MPU6050_TEST();
Delay_Ms(1000);