CH32V003 using I2C mpu6050 - Page 1
Forum section: electronics / general

author: jlsilicon
author_rank: Contributor
timestamp: April 26, 2024, 01:06:17 pm
content: I am attemptiong to connect to mpu6050 via IC on Ch32v003c8t6.I have used 6050 in the past successfully on Arduino.But I am having trouble with it on Ch32 chips.My version uses Ch32f003 I2C Eeprom as base code.https://github.com/cjacker/ch32v003evt_gcc_makefile/blob/main/Examples/I2C/I2C_EEPROM/User/main.cI used these as 6050 I2C models :https://bbs.21ic.com/icview-3129252-1-1.htmlhttps://www.risc-v1.com/thread-1625-1-1.htmlNote that there is old code at beginning converted from - not used now : IIC_ReadByte() , MPU_Read_Byte(), etc.But it locks up waiting at first I2C write.Seems to be hooked up right :Code:Ch32v003f4p6: Mpu6050:3V3 -> VccGnd -> GndPC1 -> SDAPC2 -> SCL6050 Code :Code:/*FAQs :- Wrong I2C pins for 003 : PC1=SDA PC2=SCL , not PA1=SDA PA2=SCLThe VCC and GND pins of the CH32V103 development board are connected to the VCC and GND pins of the MPU6050 module.PC1 = SDA : The PC1 pin of the CH32V003 development board is connected to the SDA pin of the MPU6050 modulePC2 = SCL : The PC2 pin of the CH32V003 development board is connected to the SCL pin of the MPU6050 module//The VCC and GND pins of the CH32V103 development board are connected to the VCC and GND pins of the MPU6050 module.// PA1 = SDA : The PA1 pin of the CH32V103 development board is connected to the SDA pin of the MPU6050 module// PA2 = SCL : The PA2 pin of the CH32V103 development board is connected to the SCL pin of the MPU6050 moduleMpu6050 : Id Address = 0x68 wrt , 0x59 rd- Compiles Downloads , Returns same data : Temp=175, AX=-11823 , AY=-11823 , AZ=-11823 , GX=-11823 , AGY=-11823 , GZ=-11823- TX = PA9- RX = PA10*//**@NoteI2C 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"///u8 AT24CXX_ReadOneByte( u16 ReadAddr ) ;void AT24CXX_WriteOneByte( u16 WriteAddr, u8 DataToWrite ) ;void AT24CXX_Read( u16 ReadAddr, u8 *pBuffer, u16 NumToRead ) ;void AT24CXX_Write( u16 WriteAddr, u8 *pBuffer, u16 NumToWrite ) ;///#ifndef __MPU6050_H#define __MPU6050_H// #include "iic.h"#include "debug.h"//MPU6050 AD0/*#define MPU_AD0_H GPIO_SetBits( GPIOA,GPIO_Pin_15) // SDA#define MPU_AD0_L GPIO_ResetBits(GPIOA,GPIO_Pin_15) // SDA*/// 003-c8t6 : - SCL=A6 , SDA=A7 :#define MPU_AD0_H GPIO_SetBits( GPIOC,GPIO_Pin_1) // SDA#define MPU_AD0_L GPIO_ResetBits(GPIOC,GPIO_Pin_1) // SDA/*// 203-c8t6 : - SCL=C2 , SDA=C1 :#define MPU_AD0_H GPIO_SetBits( GPIOC,GPIO_Pin_1) // SDA#define MPU_AD0_L GPIO_ResetBits(GPIOC,GPIO_Pin_1) // SDA*/#define MPU_ACCEL_OFFS_REG 0X06 //accel_offs#define MPU_PROD_ID_REG 0X0C //prod id#define MPU_SELF_TESTX_REG 0X0D //X#define MPU_SELF_TESTY_REG 0X0E //Y#define MPU_SELF_TESTZ_REG 0X0F //Z#define MPU_SELF_TESTA_REG 0X10 //A#define MPU_SAMPLE_RATE_REG 0X19 //#define MPU_CFG_REG 0X1A //#define MPU_GYRO_CFG_REG 0X1B //#define MPU_ACCEL_CFG_REG 0X1C //#define MPU_MOTION_DET_REG 0X1F //#define MPU_FIFO_EN_REG 0X23 //FIFO#define MPU_I2CMST_CTRL_REG 0X24 //IIC#define MPU_I2CSLV0_ADDR_REG 0X25 //IIC#define MPU_I2CSLV0_REG 0X26 //IIC#define MPU_I2CSLV0_CTRL_REG 0X27 //IIC#define MPU_I2CSLV1_ADDR_REG 0X28 //IIC#define MPU_I2CSLV1_REG 0X29 //IIC#define MPU_I2CSLV1_CTRL_REG 0X2A //IIC#define MPU_I2CSLV2_ADDR_REG 0X2B //IIC2#define MPU_I2CSLV2_REG 0X2C //IIC2#define MPU_I2CSLV2_CTRL_REG 0X2D //IIC2#define MPU_I2CSLV3_ADDR_REG 0X2E //IIC3#define MPU_I2CSLV3_REG 0X2F //IIC3#define MPU_I2CSLV3_CTRL_REG 0X30 //IIC3#define MPU_I2CSLV4_ADDR_REG 0X31 //IIC4#define MPU_I2CSLV4_REG 0X32 //IIC4#define MPU_I2CSLV4_DO_REG 0X33 //IIC4#define MPU_I2CSLV4_CTRL_REG 0X34 //IIC4#define MPU_I2CSLV4_DI_REG 0X35 //IIC4#define MPU_I2CMST_STA_REG 0X36 //IIC#define MPU_INTBP_CFG_REG 0X37 // /#define MPU_INT_EN_REG 0X38 //#define MPU_INT_STA_REG 0X3A //#define MPU_ACCEL_XOUTH_REG 0X3B //X 8#define MPU_ACCEL_XOUTL_REG 0X3C //X 8#define MPU_ACCEL_YOUTH_REG 0X3D //Y 8#define MPU_ACCEL_YOUTL_REG 0X3E //Y 8#define MPU_ACCEL_ZOUTH_REG 0X3F //Z 8#define MPU_ACCEL_ZOUTL_REG 0X40 //Z 8#define MPU_TEMP_OUTH_REG 0X41 //#define MPU_TEMP_OUTL_REG 0X42 //8#define MPU_GYRO_XOUTH_REG 0X43 //X 8#define MPU_GYRO_XOUTL_REG 0X44 //X 8#define MPU_GYRO_YOUTH_REG 0X45 //Y 8#define MPU_GYRO_YOUTL_REG 0X46 //Y 8#define MPU_GYRO_ZOUTH_REG 0X47 //Z 8#define MPU_GYRO_ZOUTL_REG 0X48 //Z 8#define MPU_I2CSLV0_DO_REG 0X63 //IIC0#define MPU_I2CSLV1_DO_REG 0X64 //IIC1#define MPU_I2CSLV2_DO_REG 0X65 //IIC2#define MPU_I2CSLV3_DO_REG 0X66 //IIC3#define MPU_I2CMST_DELAY_REG 0X67 //IIC#define MPU_SIGPATH_RST_REG 0X68 //#define MPU_MDETECT_CTRL_REG 0X69 //#define MPU_USER_CTRL_REG 0X6A //#define MPU_PWR_MGMT1_REG 0X6B //1#define MPU_PWR_MGMT2_REG 0X6C //2#define MPU_FIFO_CNTH_REG 0X72 //FIFO#define MPU_FIFO_CNTL_REG 0X73 //FIFO#define MPU_FIFO_RW_REG 0X74 //FIFO#define MPU_DEVICE_ID_REG 0X75 //ID#define MPU_ADDR 0X68//#define MPU_READ 0XD1//#define MPU_WRITE 0XD0u8 MPU_Init(void); //MPU6050u8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf);//IICu8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf); //IICu8 MPU_Write_Byte(u8 reg,u8 data); //IICu8 MPU_Read_Byte(u8 reg); //IICu8 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);u8 MPU_Set_Fifo(u8 sens);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);#endif///// MPU6050.c //// #include "mpu6050.h"/*#include "debug.h"*///#ifdef OLD_CODE/*void IIC_Start( u8 Flg ){while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );I2C_GenerateSTART( I2C1, Flg );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );}*/void IIC_Start(){while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );I2C_GenerateSTART( I2C1, ENABLE );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );}/*void IIC_Stop( u8 Flg ){I2C_GenerateSTOP( I2C1, Flg );}*/void IIC_Stop(){I2C_GenerateSTOP( I2C1, ENABLE );}int IIC_WaitAck(){while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET ) {}return 0;}void IIC_SendByte( u8 Dat ){I2C_SendData( I2C1, Dat );}u8 IIC_ReadByte( u8 Dat ){u8 R ;R = I2C_ReceiveData( I2C1 );return( R );}#endif // - OLD_CODE ./*u8 MPU_Init(void){u8 res;GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //AFIORCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //IO PORTA// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO 50MHzGPIO_Init(GPIOA, &GPIO_InitStructure); //GPIOAGPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //JTAG PA15 IO PA15 O!!!MPU_AD0_L; // MPU6050 AD0 :0X68IIC_Init(); //IICMPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //MPU6050Delay_Ms(100);MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //MPU6050MPU_Set_Gyro_Fsr(3); //2000dpsMPU_Set_Accel_Fsr(0); //2gMPU_Set_Rate(50); //50HzMPU_Write_Byte(MPU_INT_EN_REG,0X00); //MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //I2CMPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //FIFOMPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //INTres=MPU_Read_Byte(MPU_DEVICE_ID_REG);if(res==MPU_ADDR)// ID{MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); //CLKSEL,PLL XMPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //MPU_Set_Rate(50); //50Hz}elsereturn 1;return 0;}*//*void IIC_Init(u32 bound, u16 address)// u8 MPU_Init(void){GPIO_InitTypeDef GPIO_InitStructure={0};I2C_InitTypeDef I2C_InitTSturcture={0};RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );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_Mode = I2C_Mode_I2C;I2C_InitTSturcture.I2C_ClockSpeed = bound;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;MPU_AD0_L ; // MPU6050 AD0 0X68I2C_Init( I2C1, &I2C_InitTSturcture );I2C_Cmd( I2C1, ENABLE );// IIC_SendByte( ENABLE ) ; // - ???// I2C_AcknowledgeConfig( I2C1, ENABLE );// IIC_WaitAck(); // - ???while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET ) {} // - ???// ??? :MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //MPU6050Delay_Ms(100);MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //MPU6050MPU_Set_Gyro_Fsr(3); //+-2000dpsMPU_Set_Accel_Fsr(0); //+-2gMPU_Set_Rate(50); //50HzMPU_Write_Byte(MPU_INT_EN_REG,0X00); //MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //I2CMPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //FIFOMPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //INTint res ;res = MPU_Read_Byte(MPU_DEVICE_ID_REG);if(res == MPU_ADDR) // ID{MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); // CLKSEL,PLL XMPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //MPU_Set_Rate(50); // 50Hz}// else// return 1;// return 0;}*///fsr:0,250dps;1,500dps;2,1000dps;3,2000dpsu8 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;if( lpf >= 188 )data=1;else if( lpf >= 98 )data=2;else if( lpf >= 42 )data=3;else if( lpf >= 20 )data=4;else if( lpf >= 10 )data=5;elsedata = 6;// // return( MPU_Write_Byte( MPU_CFG_REG , data ) );// return( AT24CXX_WriteOneByte( MPU_CFG_REG , data ) );AT24CXX_WriteOneByte( MPU_CFG_REG , data ) ;return(0);}u8 MPU_Set_Rate(u16 rate){u8 data;if(rate > 1000)rate = 1000;if(rate < 4)rate = 4;data = 1000 / rate - 1 ;// // data = MPU_Write_Byte( MPU_SAMPLE_RATE_REG , data ); //AT24CXX_WriteOneByte( MPU_SAMPLE_RATE_REG , data ); //return MPU_Set_LPF( rate / 2 ); //LPF}short MPU_Get_Temperature(void){u8 buf;short raw;float temp;// // MPU_Read_Len( MPU_ADDR , MPU_TEMP_OUTH_REG , 2 , buf );AT24CXX_Read( MPU_TEMP_OUTH_REG , buf , 2 );raw = ((u16)buf<<8) | buf;temp = 36.53 + ((double)raw) / 340;return( temp * 100 );}u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz){u8 buf,res = 0 ;// // res = MPU_Read_Len( MPU_ADDR , MPU_GYRO_XOUTH_REG , 6 , buf );AT24CXX_Read( MPU_GYRO_XOUTH_REG , buf , 6 );// // if( res == 0 ){*gx = ((u16)buf<<8) | buf;*gy = ((u16)buf<<8) | buf;*gz = ((u16)buf<<8) | buf;printf("GYRO : X=%d , Y=%d , Z=%d \r\n",*gx,*gy,*gz);}return res;;}u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az){u8 buf , res = 0 ;// // res = MPU_Read_Len( MPU_ADDR , MPU_ACCEL_XOUTH_REG , 6 , buf );AT24CXX_Read( MPU_ACCEL_XOUTH_REG , buf , 6 );// // if( res == 0 ){*ax = ((u16)buf<<8) | buf;*ay = ((u16)buf<<8) | buf;*az = ((u16)buf<<8) | buf;printf("ACC : X=%d , Y=%d , Z=%d \r\n", *ax , *ay , *az );}return res;;}#ifdef OLD_CODEu8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf){u8 i;IIC_Start();IIC_SendByte((addr<<1)|0); //if( IIC_WaitAck() ) //{IIC_Stop();return 1;}IIC_SendByte(reg); //IIC_WaitAck(); //for(i=0;i<len;i++){IIC_SendByte(buf); //if( IIC_WaitAck() ) //ACK{IIC_Stop();return 1;}}IIC_Stop();return 0;}u8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf){IIC_Start();IIC_SendByte((addr<<1)|0);//if(IIC_WaitAck()) //{IIC_Stop();return 1;}IIC_SendByte(reg); //IIC_WaitAck(); //IIC_Start();IIC_SendByte((addr<<1)|1);//IIC_WaitAck(); //while(len){if(len==1)* buf = IIC_ReadByte(0);//nACKelse* buf = IIC_ReadByte(1); //ACKprintf( " <%02X> " , * buf );len--;buf++;}IIC_Stop(); // STOPreturn 0;}u8 MPU_Write_Byte(u8 reg,u8 data){IIC_Start();IIC_SendByte((MPU_ADDR<<1)|0);//if(IIC_WaitAck()) //{IIC_Stop();return 1;}IIC_SendByte(reg); //IIC_WaitAck(); //IIC_SendByte(data);//if(IIC_WaitAck()) //ACK{IIC_Stop();return 1;}IIC_Stop();return 0;}u8 MPU_Read_Byte(u8 reg){u8 res;IIC_Start();IIC_SendByte((MPU_ADDR<<1)|0);//IIC_WaitAck(); //IIC_SendByte(reg); //IIC_WaitAck(); //IIC_Start();IIC_SendByte((MPU_ADDR<<1)|1);//IIC_WaitAck(); //res=IIC_ReadByte(0);//nACKIIC_Stop(); // STOPprintf( " <%02X> " , res );return res;}#endif // - OLD_CODE// int main(void)// int main_1(void)int MPU6050_TEST(void){short aacx,aacy,aacz; //short gyrox,gyroy,gyroz; //short temp; ///*NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);Delay_Init();USART_Printf_Init(115200);// MPU_Init(); //MPU6050// IIC_Init( 100000, 0xA0 );IIC_Init( 100000, 0x68 );// IIC_Init( 100000, 0x69 );printf("\r\n\r\n: MPU6050 TEST : \r\n\r\n");printf("- SystemClk : %d \r\n\r\n\r\n",SystemCoreClock);*/while( 1 ){temp = MPU_Get_Temperature(); //MPU_Get_Accelerometer(&aacx,&aacy,&aacz); //MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz); //printf("\r\n Temperature:%d \r\n",temp);Delay_Ms(500);}}////***********************************************************************@Note:AT24CxxREAD 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/* EERPOM DATA ADDRESS Length Selection */#define Address_Lenth Address_8bit//#define Address_Lenth Address_16bit/* 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};RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );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_Mode = I2C_Mode_I2C;I2C_InitTSturcture.I2C_OwnAddress1 = address;I2C_InitTSturcture.I2C_ClockSpeed = bound;I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;I2C_Init( I2C1, &I2C_InitTSturcture );I2C_Cmd( I2C1, ENABLE );I2C_AcknowledgeConfig( I2C1, ENABLE );printf("< IIC_Init() .\r\n");}/********************************************************************** @fn AT24CXX_Init* @brief Initializes AT24xx EEPROM.* @return none*/void AT24CXX_Init(void){IIC_Init( 100000, 0xA0 );}void MPU6050_Init(void){IIC_Init( 100000, 0x68 );}/********************************************************************** @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;while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );I2C_GenerateSTART( I2C1, ENABLE );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );// // I2C_Send7bitAddress( I2C1, 0XA0, I2C_Direction_Transmitter );I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Transmitter );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );// Send Address 8b / 16b : //#if (Address_Lenth == Address_8bit)I2C_SendData( I2C1, (u8)(ReadAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#elif (Address_Lenth == Address_16bit)I2C_SendData( I2C1, (u8)(ReadAddr>>8) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );I2C_SendData( I2C1, (u8)(ReadAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#endifI2C_GenerateSTART( I2C1, ENABLE );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );// // I2C_Send7bitAddress( I2C1, 0XA0, I2C_Direction_Receiver );I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Receiver );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) );while( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) == RESET )I2C_AcknowledgeConfig( I2C1, DISABLE );temp = I2C_ReceiveData( I2C1 );I2C_GenerateSTOP( I2C1, ENABLE );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 );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );// // I2C_Send7bitAddress( I2C1, 0XA0, I2C_Direction_Transmitter );I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Transmitter );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );// Send Address 8b / 16b : //#if (Address_Lenth == Address_8bit)I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#elif (Address_Lenth == Address_16bit)I2C_SendData( I2C1, (u8)(WriteAddr>>8) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#endifif( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) != RESET ){I2C_SendData( I2C1, DataToWrite );}while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );I2C_GenerateSTOP( I2C1, ENABLE );}/********************************************************************** @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 AT24CXX_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite){while(NumToWrite--){AT24CXX_WriteOneByte( WriteAddr , * pBuffer );WriteAddr ++ ;pBuffer ++ ;Delay_Ms(2);}}// // void MPU6050_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite)void MPU6050_Write(){printf("> MPU6050_Write() : \r\n");// // MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //MPU6050AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X80); //MPU6050Delay_Ms(100);// // MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //MPU6050AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X00); //MPU6050// Delay_Ms(1);printf("= MPU6050_Write() -> MPU_Set_Gyro_Fsr() \r\n");// // MPU_Set_Gyro_Fsr(3); //+-2000dpsAT24CXX_WriteOneByte( MPU_ACCEL_CFG_REG , 3 << 3 ) ;// Delay_Ms(1);printf("= MPU6050_Write() -> MPU_Set_Accel_Fsr() \r\n");MPU_Set_Accel_Fsr(0); //+-2gprintf("= MPU6050_Write() -> MPU_Set_Rate() \r\n");MPU_Set_Rate(50); //50Hzprintf("= MPU6050_Write() ... \r\n");// // MPU_Write_Byte(MPU_INT_EN_REG,0X00); //AT24CXX_WriteOneByte(MPU_INT_EN_REG,0X00); //// // MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //I2CAT24CXX_WriteOneByte(MPU_USER_CTRL_REG,0X00); //I2C// // MPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //FIFOAT24CXX_WriteOneByte(MPU_FIFO_EN_REG,0X00); //FIFO// // MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //INTAT24CXX_WriteOneByte(MPU_INTBP_CFG_REG,0X80); //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 XAT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X01); // CLKSEL,PLL X// // MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //AT24CXX_WriteOneByte(MPU_PWR_MGMT2_REG,0X00); //MPU_Set_Rate(50); // 50Hz}// else// return 1;// return 0;printf("< MPU6050_Write() . \r\n");}/********************************************************************** @fn main* @brief Main program.* @return none*/int main(void){// u8 data;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);// // AT24CXX_Init();MPU6050_Init();/*printf("Start Write 24Cxx....\r\n");AT24CXX_Write(100,(u8*)TEXT_Buffer,SIZE);printf("24Cxx Write Success!\r\n");Delay_Ms(500);printf("Start Read 24Cxx....\r\n");AT24CXX_Read(100,data,SIZE);printf("The Data Read Is: \r\n");printf("%s\r\n", data);*/MPU6050_Write();while( 1 ){printf("> mpu6050 Test ... \r\n");MPU6050_TEST();Delay_Ms(500);}}///If it helps , the Code File and the entire Project is attached :- Any help would be appreciated !

author: jlsilicon
author_rank: Contributor
timestamp: April 26, 2024, 04:14:44 pm
content: If this helps - without the old commented code :Code:/*Create project as : ch32v003 - f4p6 (- Not 103/203)- copy main.c from /EVT/EXAM/.../I2C_mpu6050/User/FAQs :- Typo used id 0x58 , should be 0x68- Wrong I2C pins for 003 : not PA1=SDA PA2=SCL , should be PC1=SDA PC2=SCLThe VCC and GND pins of the CH32V103 development board are connected to the VCC and GND pins of the MPU6050 module.PC1 = SDA : The PC1 pin of the CH32V003 development board is connected to the SDA pin of the MPU6050 modulePC2 = SCL : The PC2 pin of the CH32V003 development board is connected to the SCL pin of the MPU6050 module//The VCC and GND pins of the CH32V103 development board are connected to the VCC and GND pins of the MPU6050 module.// PA1 = SDA : The PA1 pin of the CH32V103 development board is connected to the SDA pin of the MPU6050 module// PA2 = SCL : The PA2 pin of the CH32V103 development board is connected to the SCL pin of the MPU6050 moduleMpu6050 : Id Address = 0x68 wrt , 0x69 rd- Compiles Downloads , Returns same data : Temp=175, AX=-11823 , AY=-11823 , AZ=-11823 , GX=-11823 , AGY=-11823 , GZ=-11823- TX = PA9- RX = PA10*////#include "debug.h"//u8 AT24CXX_ReadOneByte( u16 ReadAddr ) ;void AT24CXX_WriteOneByte( u16 WriteAddr, u8 DataToWrite ) ;void AT24CXX_Read( u16 ReadAddr, u8 *pBuffer, u16 NumToRead ) ;void AT24CXX_Write( u16 WriteAddr, u8 *pBuffer, u16 NumToWrite ) ;///#ifndef __MPU6050_H#define __MPU6050_H// // #include "iic.h"// #include "debug.h"//MPU6050 AD0/*#define MPU_AD0_H GPIO_SetBits( GPIOA,GPIO_Pin_15) // SDA#define MPU_AD0_L GPIO_ResetBits(GPIOA,GPIO_Pin_15) // SDA*/// 003-c8t6 : - SCL=A6 , SDA=A7 :#define MPU_AD0_H GPIO_SetBits( GPIOC,GPIO_Pin_1) // SDA#define MPU_AD0_L GPIO_ResetBits(GPIOC,GPIO_Pin_1) // SDA/*// 203-c8t6 : - SCL=C2 , SDA=C1 :#define MPU_AD0_H GPIO_SetBits( GPIOC,GPIO_Pin_1) // SDA#define MPU_AD0_L GPIO_ResetBits(GPIOC,GPIO_Pin_1) // SDA*/#define MPU_ACCEL_OFFS_REG 0X06 //accel_offs#define MPU_PROD_ID_REG 0X0C //prod id#define MPU_SELF_TESTX_REG 0X0D //X#define MPU_SELF_TESTY_REG 0X0E //Y#define MPU_SELF_TESTZ_REG 0X0F //Z#define MPU_SELF_TESTA_REG 0X10 //A#define MPU_SAMPLE_RATE_REG 0X19 //#define MPU_CFG_REG 0X1A //#define MPU_GYRO_CFG_REG 0X1B //#define MPU_ACCEL_CFG_REG 0X1C //#define MPU_MOTION_DET_REG 0X1F //#define MPU_FIFO_EN_REG 0X23 //FIFO#define MPU_I2CMST_CTRL_REG 0X24 //IIC#define MPU_I2CSLV0_ADDR_REG 0X25 //IIC#define MPU_I2CSLV0_REG 0X26 //IIC#define MPU_I2CSLV0_CTRL_REG 0X27 //IIC#define MPU_I2CSLV1_ADDR_REG 0X28 //IIC#define MPU_I2CSLV1_REG 0X29 //IIC#define MPU_I2CSLV1_CTRL_REG 0X2A //IIC#define MPU_I2CSLV2_ADDR_REG 0X2B //IIC2#define MPU_I2CSLV2_REG 0X2C //IIC2#define MPU_I2CSLV2_CTRL_REG 0X2D //IIC2#define MPU_I2CSLV3_ADDR_REG 0X2E //IIC3#define MPU_I2CSLV3_REG 0X2F //IIC3#define MPU_I2CSLV3_CTRL_REG 0X30 //IIC3#define MPU_I2CSLV4_ADDR_REG 0X31 //IIC4#define MPU_I2CSLV4_REG 0X32 //IIC4#define MPU_I2CSLV4_DO_REG 0X33 //IIC4#define MPU_I2CSLV4_CTRL_REG 0X34 //IIC4#define MPU_I2CSLV4_DI_REG 0X35 //IIC4#define MPU_I2CMST_STA_REG 0X36 //IIC#define MPU_INTBP_CFG_REG 0X37 // /#define MPU_INT_EN_REG 0X38 //#define MPU_INT_STA_REG 0X3A //#define MPU_ACCEL_XOUTH_REG 0X3B //X 8#define MPU_ACCEL_XOUTL_REG 0X3C //X 8#define MPU_ACCEL_YOUTH_REG 0X3D //Y 8#define MPU_ACCEL_YOUTL_REG 0X3E //Y 8#define MPU_ACCEL_ZOUTH_REG 0X3F //Z 8#define MPU_ACCEL_ZOUTL_REG 0X40 //Z 8#define MPU_TEMP_OUTH_REG 0X41 //#define MPU_TEMP_OUTL_REG 0X42 //8#define MPU_GYRO_XOUTH_REG 0X43 //X 8#define MPU_GYRO_XOUTL_REG 0X44 //X 8#define MPU_GYRO_YOUTH_REG 0X45 //Y 8#define MPU_GYRO_YOUTL_REG 0X46 //Y 8#define MPU_GYRO_ZOUTH_REG 0X47 //Z 8#define MPU_GYRO_ZOUTL_REG 0X48 //Z 8#define MPU_I2CSLV0_DO_REG 0X63 //IIC0#define MPU_I2CSLV1_DO_REG 0X64 //IIC1#define MPU_I2CSLV2_DO_REG 0X65 //IIC2#define MPU_I2CSLV3_DO_REG 0X66 //IIC3#define MPU_I2CMST_DELAY_REG 0X67 //IIC#define MPU_SIGPATH_RST_REG 0X68 //#define MPU_MDETECT_CTRL_REG 0X69 //#define MPU_USER_CTRL_REG 0X6A //#define MPU_PWR_MGMT1_REG 0X6B //1#define MPU_PWR_MGMT2_REG 0X6C //2#define MPU_FIFO_CNTH_REG 0X72 //FIFO#define MPU_FIFO_CNTL_REG 0X73 //FIFO#define MPU_FIFO_RW_REG 0X74 //FIFO#define MPU_DEVICE_ID_REG 0X75 //ID#define MPU_ADDR 0X68//#define MPU_READ 0XD1//#define MPU_WRITE 0XD0u8 MPU_Init(void); //MPU6050u8 MPU_Write_Len(u8 addr,u8 reg,u8 len,u8 *buf);//IICu8 MPU_Read_Len(u8 addr,u8 reg,u8 len,u8 *buf); //IICu8 MPU_Write_Byte(u8 reg,u8 data); //IICu8 MPU_Read_Byte(u8 reg); //IICu8 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);u8 MPU_Set_Fifo(u8 sens);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);#endif/////fsr:0,250dps;1,500dps;2,1000dps;3,2000dpsu8 MPU_Set_Gyro_Fsr(u8 fsr){AT24CXX_WriteOneByte( MPU_GYRO_CFG_REG , fsr << 3 ) ;return( 0 );}u8 MPU_Set_Accel_Fsr(u8 fsr){AT24CXX_WriteOneByte( MPU_ACCEL_CFG_REG , fsr << 3 ) ;return( 0 );}u8 MPU_Set_LPF(u16 lpf){u8 data = 0;if( lpf >= 188 )data=1;else if( lpf >= 98 )data=2;else if( lpf >= 42 )data=3;else if( lpf >= 20 )data=4;else if( lpf >= 10 )data=5;elsedata = 6;AT24CXX_WriteOneByte( MPU_CFG_REG , data ) ;return(0);}u8 MPU_Set_Rate(u16 rate){u8 data;if(rate > 1000)rate = 1000;if(rate < 4)rate = 4;data = 1000 / rate - 1 ;AT24CXX_WriteOneByte( MPU_SAMPLE_RATE_REG , data ); //return MPU_Set_LPF( rate / 2 ); //LPF}short MPU_Get_Temperature(void){u8 buf;short raw;float temp;AT24CXX_Read( MPU_TEMP_OUTH_REG , buf , 2 );raw = ((u16)buf<<8) | buf;temp = 36.53 + ((double)raw) / 340;return( temp * 100 );}u8 MPU_Get_Gyroscope(short *gx,short *gy,short *gz){u8 buf,res = 0 ;AT24CXX_Read( MPU_GYRO_XOUTH_REG , buf , 6 );*gx = ((u16)buf<<8) | buf;*gy = ((u16)buf<<8) | buf;*gz = ((u16)buf<<8) | buf;printf("GYRO : X=%d , Y=%d , Z=%d \r\n",*gx,*gy,*gz);return res;;}u8 MPU_Get_Accelerometer(short *ax,short *ay,short *az){u8 buf , res = 0 ;AT24CXX_Read( MPU_ACCEL_XOUTH_REG , buf , 6 );*ax = ((u16)buf<<8) | buf;*ay = ((u16)buf<<8) | buf;*az = ((u16)buf<<8) | buf;printf("ACC : X=%d , Y=%d , Z=%d \r\n", *ax , *ay , *az );return res;;}// int main(void)// int main_1(void)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); //printf("\r\n Temperature:%d \r\n",temp);Delay_Ms(500);}}////***********************************************************************@Note:AT24CxxREAD 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/* EERPOM DATA ADDRESS Length Selection */#define Address_Lenth Address_8bit//#define Address_Lenth Address_16bit/* 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};RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );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_Mode = I2C_Mode_I2C;I2C_InitTSturcture.I2C_OwnAddress1 = address;I2C_InitTSturcture.I2C_ClockSpeed = bound;I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;I2C_Init( I2C1, &I2C_InitTSturcture );I2C_Cmd( I2C1, ENABLE );I2C_AcknowledgeConfig( I2C1, ENABLE );printf("< IIC_Init() .\r\n");}/********************************************************************** @fn AT24CXX_Init* @brief Initializes AT24xx EEPROM.* @return none*/void MPU6050_Init(void){IIC_Init( 100000, 0x68 );}/********************************************************************** @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;while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );I2C_GenerateSTART( I2C1, ENABLE );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Transmitter );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );// Send Address 8b / 16b : //#if (Address_Lenth == Address_8bit)I2C_SendData( I2C1, (u8)(ReadAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#elif (Address_Lenth == Address_16bit)I2C_SendData( I2C1, (u8)(ReadAddr>>8) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );I2C_SendData( I2C1, (u8)(ReadAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#endifI2C_GenerateSTART( I2C1, ENABLE );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Receiver );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) );while( I2C_GetFlagStatus( I2C1, I2C_FLAG_RXNE ) == RESET )I2C_AcknowledgeConfig( I2C1, DISABLE );temp = I2C_ReceiveData( I2C1 );I2C_GenerateSTOP( I2C1, ENABLE );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 );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) );I2C_Send7bitAddress( I2C1, 0X68, I2C_Direction_Transmitter );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) );// Send Address 8b / 16b : //#if (Address_Lenth == Address_8bit)I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#elif (Address_Lenth == Address_16bit)I2C_SendData( I2C1, (u8)(WriteAddr>>8) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );I2C_SendData( I2C1, (u8)(WriteAddr&0x00FF) );while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );#endifif( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) != RESET ){I2C_SendData( I2C1, DataToWrite );}while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) );I2C_GenerateSTOP( I2C1, ENABLE );}/********************************************************************** @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 AT24CXX_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite){while(NumToWrite--){AT24CXX_WriteOneByte( WriteAddr , * pBuffer );WriteAddr ++ ;pBuffer ++ ;Delay_Ms(2);}}// // void MPU6050_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite)void MPU6050_Write(){printf("> MPU6050_Write() : \r\n");AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X80); //MPU6050Delay_Ms(100);AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X00); //MPU6050printf("= MPU6050_Write() -> MPU_Set_Gyro_Fsr() \r\n");MPU_Set_Gyro_Fsr(3); //+-2000dpsMPU_Set_Accel_Fsr(0); //+-2gMPU_Set_Rate(50); //50Hzprintf("= MPU6050_Write() ... \r\n");AT24CXX_WriteOneByte(MPU_INT_EN_REG,0X00); //AT24CXX_WriteOneByte(MPU_USER_CTRL_REG,0X00); //I2CAT24CXX_WriteOneByte(MPU_FIFO_EN_REG,0X00); //FIFOAT24CXX_WriteOneByte(MPU_INTBP_CFG_REG,0X80); //INTAT24CXX_ReadOneByte( MPU_DEVICE_ID_REG );AT24CXX_WriteOneByte(MPU_PWR_MGMT1_REG,0X01); // CLKSEL,PLL XAT24CXX_WriteOneByte(MPU_PWR_MGMT2_REG,0X00); //MPU_Set_Rate(50); // 50Hzprintf("< MPU6050_Write() . \r\n");}/********************************************************************** @fn main* @brief Main program.* @return none*/int main(void){Delay_Init();USART_Printf_Init(115200);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 ){printf("> mpu6050 Test ... \r\n");MPU6050_TEST();Delay_Ms(500);}}///

author: jlsilicon
author_rank: Contributor
timestamp: April 26, 2024, 06:27:20 pm
content: I added an I2C Bus Scanner.But it does not see the Mpu6060.- Is the prolem that :-- the I2C Bus Scanner code ,-- or the mpu6050 wiring to Ch32v003f4p6 board ,-- or does the Board Not support I2C - C1=SDA , C2=SCL ?This is the Board :-- Reset -> Reset PD7 pin-- LED <- PD4This is the schematic (except mine has a 10pin DIO Header) :https://taunoerik.art/wp-content/uploads/2023/10/screenshot-from-2023-10-22-08-52-05.pngCode :Code:/**//**@Note*Multiprocessor communication mode routine:*Master:USART1_Tx(PD5)\USART1_Rx(PD6).*This routine demonstrates that USART1 receives the data sent by CH341 and inverts*it and sends it (baud rate 115200).**Hardware connection:PD5 -- Rx* PD6 -- Tx**/#include "debug.h"#include <ch32v00x_i2c.h>#include <ch32v00x_rcc.h>/* Global define *//* Global Variable */vu8 val;/********************************************************************** @fn USARTx_CFG** @brief Initializes the USART2 & USART3 peripheral.** @return none*/void USARTx_CFG(void){GPIO_InitTypeDef GPIO_InitStructure = {0};USART_InitTypeDef USART_InitStructure = {0};RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE);/* USART1 TX-->D.5 RX-->D.6 */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOD, &GPIO_InitStructure);USART_InitStructure.USART_BaudRate = 115200;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;USART_Init(USART1, &USART_InitStructure);USART_Cmd(USART1, ENABLE);}///**@Note7-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 -- PC2PC1 -- PC1*/#include "debug.h"/* I2C Mode Definition */#define HOST_MODE 0#define SLAVE_MODE 1/* I2C Communication Mode Selection */#define I2C_MODE HOST_MODE//#define I2C_MODE SLAVE_MODE/* Global define */#define Size 6#define RXAdderss 0x02#define TxAdderss 0x02/* Global Variable */u8 TxData = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };u8 RxData;/********************************************************************** @fn IIC_Init* @brief Initializes the IIC peripheral.* @return none*/void IIC_Init(u32 bound, u16 address){GPIO_InitTypeDef GPIO_InitStructure = {0};I2C_InitTypeDef I2C_InitTSturcture = {0};RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );/// SDA : ///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 );/// SCL : ///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_Mode = I2C_Mode_I2C;I2C_InitTSturcture.I2C_OwnAddress1 = address;I2C_InitTSturcture.I2C_ClockSpeed = bound;I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9;I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;I2C_Init( I2C1, &I2C_InitTSturcture );I2C_Cmd( I2C1, ENABLE );// #if (I2C_MODE == HOST_MODE)I2C_AcknowledgeConfig( I2C1, ENABLE );// #endif}///********************************************************************** @fn main* @brief Main program.* @return none*/int main(void){NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);SystemCoreClockUpdate();Delay_Init();USART_Printf_Init(115200);printf("\r\n\r\n: I2C_MPU6050_003_2 : \r\n");printf(" SystemClk:%d\r\n",SystemCoreClock);printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );USARTx_CFG();//// u8 i = 0;u8 j = 0;// u8 p = 0;u8 res ;// __IO uint32_t i2creg=0 ;__IO uint32_t i2cxbase=0 , i2cxbase1=0 , i2cxbase2=0 ;i2cxbase = (uint32_t)I2C1 ; // = (uint32_t)I2Cx;// i2creg = I2C_FLAG >> 28;// I2C_FLAG &= FLAG_Mask;// if(i2creg != 0)// {// i2cxbase += 0x18;i2cxbase1 = i2cxbase + 0x14;// }// else// {// I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);// i2cxbase += 0x18;i2cxbase2 = i2cxbase + 0x18;// }printf("> main(): IIC mode\r\n");IIC_Init( 80000, TxAdderss);while( 1 ){printf("= main(): for(0..5) \r\n");for( j=0x01; j <= 0x7F ; j++ ){while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY ) != RESET );printf("= main(): ->I2C:Start() \r\n");I2C_GenerateSTART( I2C1, ENABLE );while( ! I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT ) ) {}printf(" = main(): ->I2C:SendAdrId() = x%02X \r\n" , j );// I2C_Send7bitAddress( I2C1, 0x02, I2C_Direction_Transmitter );// I2C_Send7bitAddress( I2C1, 0x68, I2C_Direction_Transmitter );I2C_Send7bitAddress( I2C1, j, I2C_Direction_Transmitter );// while( ! I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ) {}res = I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ;printf(" => MTM=%d " , res );res = I2C_CheckEvent( I2C1, I2C_Direction_Transmitter ) ;printf(", DirT=%d " , res );Delay_Ms(1);res = I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ;printf(", MTM=%d " , res );// res = I2C_GetFlagStatus( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ;res = (*(__IO uint32_t *) i2cxbase1);printf(", St=%d " , res );res = (*(__IO uint32_t *) i2cxbase2);printf("%d " , res );/*while( I2C_GetFlagStatus( I2C1, I2C_FLAG_TXE ) == RESET ) {}I2C_SendData( I2C1, TxData );while( ! I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED ) ) {}*/printf("\r\n= main(): ->I2C:Stop() \r\n");I2C_GenerateSTOP( I2C1, ENABLE );// res = I2C_GetFlagStatus( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) ;res = (*(__IO uint32_t *) i2cxbase1);printf(" , St=%d " , res );res = (*(__IO uint32_t *) i2cxbase2);printf("%d \r\n" , res );Delay_Ms(100);}printf("\r\n---\r\n\r\n");Delay_Ms(1000);} // - while(1) .//while( 1 ){;}//}///Also added to my github :https://github.com/jlsilicon/Ch32v003/blob/main/Ch32v003/I2C_Bus_Scanner/User/main.c

author: xvr
author_rank: Frequent Contributor
timestamp: April 26, 2024, 07:31:00 pm
content: Do you have pull up resistors on I2C bus? I saw no one in schematic.

author: jlsilicon
author_rank: Contributor
timestamp: April 26, 2024, 08:06:02 pm
content: MPU6050 board HW-123 has built in Pullup resistors.Yeah thanks, I did check that.https://europe1.discourse-cdn.com/arduino/optimized/4X/7/4/6/74631f82563988409a5cddf32fdacbfee3e4707f_2_690x403.jpeg

author: SiliconWizard
author_rank: Super Contributor
timestamp: April 26, 2024, 08:14:53 pm
content: If you've checked everything else, the likely issue is just the slave address. You're using 0x68, which is the 7-bit address. But I2C functions of the SDK (similar to the STM32 HAL, which WCH has gotten its inspiration from) take an 8-bit address IIRC (so the byte that it will send over I2C). In your case, you'd need to shift the address left by 1 bit, like 0x68 << 1. Haven't used the CH32V003, but try this and let me know.

author: jlsilicon
author_rank: Contributor
timestamp: April 27, 2024, 12:34:58 am
content: No, still no result.// I2C_Send7bitAddress( I2C1, 0x68, I2C_Direction_Transmitter );I2C_Send7bitAddress( I2C1, (0x68>>1), I2C_Direction_Transmitter );: I2C_MPU6050_003_2 :SystemClk:48000000ChipID:00300500> main(): IIC mode= main(): for(0..5)= main(): ->I2C:Start()= main(): ->I2C:SendAdrId()

author: brucehoult
author_rank: Super Contributor
timestamp: April 27, 2024, 12:45:45 am
content: Which way is left?

author: jlsilicon
author_rank: Contributor
timestamp: April 27, 2024, 02:29:19 am
content: Why shift left if its sent 7bit.I thought you would shift right : x68 >> 1 => 7bitoh - ok got it !You are right that fixed it.Thx !Below is the output while twisting the mpu6050 around.- a little flakey with connection - probably loose wire.Output :Code:: MPU6050 003 :- SystemClk:48000000> IIC_Init() : bound=100000 , address=68 :< 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 ...ACC : X=17228 , Y=-490 , Z=1082GYRO : X=-363 , Y=-81 , Z=-141Temperature:2745ACC : X=17336 , Y=-450 , Z=1076GYRO : X=-385 , Y=-89 , Z=-232Temperature:2752ACC : X=17652 , Y=-666 , Z=1096GYRO : X=-953 , Y=-364 , Z=-789Temperature:2760ACC : X=17196 , Y=-442 , Z=1074GYRO : X=-334 , Y=-30 , Z=-71Temperature:2764ACC : X=17170 , Y=2434 , Z=1196GYRO : X=-788 , Y=-640 , Z=-5729Temperature:2766ACC : X=15506 , Y=6944 , Z=1336GYRO : X=-1266 , Y=-315 , Z=-4543Temperature:2770ACC : X=14536 , Y=8802 , Z=1432GYRO : X=-326 , Y=53 , Z=-2003Temperature:2755ACC : X=17648 , Y=-1382 , Z=-2266GYRO : X=98 , Y=-879 , Z=-431Temperature:2765ACC : X=17158 , Y=-480 , Z=-4640GYRO : X=-238 , Y=-2639 , Z=-459Temperature:2771ACC : X=16138 , Y=-938 , Z=-7282GYRO : X=537 , Y=-3935 , Z=-489Temperature:2773ACC : X=13872 , Y=-1016 , Z=-10700GYRO : X=-203 , Y=-2806 , Z=-1125Temperature:2776ACC : X=12804 , Y=-118 , Z=-12634GYRO : X=-376 , Y=-1719 , Z=-1261Temperature:2779ACC : X=15312 , Y=-880 , Z=-8602GYRO : X=805 , Y=6467 , Z=1711Temperature:2783ACC : X=15758 , Y=-3340 , Z=-5560GYRO : X=-198 , Y=-438 , Z=1705Temperature:2785ACC : X=16772 , Y=-2438 , Z=-5098GYRO : X=339 , Y=2083 , Z=1109Temperature:2786ACC : X=16914 , Y=-3206 , Z=-2504GYRO : X=-550 , Y=764 , Z=-183Temperature:2789ACC : X=17258 , Y=-3142 , Z=-3218GYRO : X=-353 , Y=-206 , Z=-299Temperature:2794ACC : X=17126 , Y=-2578 , Z=-2918GYRO : X=-1507 , Y=-640 , Z=-1084Temperature:2799ACC : X=17176 , Y=736 , Z=-2528GYRO : X=-971 , Y=-499 , Z=-2939Temperature:2802ACC : X=17278 , Y=2776 , Z=-2682GYRO : X=-605 , Y=28 , Z=-1923Temperature:2805ACC : X=16264 , Y=4588 , Z=-2324GYRO : X=-404 , Y=-362 , Z=568Temperature:2788ACC : X=17156 , Y=-2930 , Z=-2542GYRO : X=-1090 , Y=113 , Z=196ACC : X=17554 , Y=-1932 , Z=-2094GYRO : X=586 , Y=-1509 , Z=2648Temperature:2723ACC : X=17380 , Y=-1980 , Z=-2290GYRO : X=-250 , Y=-57 , Z=-1Temperature:2733ACC : X=17630 , Y=582 , Z=-1344GYRO : X=919 , Y=555 , Z=-2866Temperature:2739ACC : X=16100 , Y=4330 , Z=-2050GYRO : X=-917 , Y=-948 , Z=-4507Temperature:2742ACC : X=15060 , Y=7624 , Z=-2274GYRO : X=-931 , Y=-88 , Z=-2531Temperature:2745ACC : X=14334 , Y=8134 , Z=-2348GYRO : X=290 , Y=0 , Z=4672ACC : X=17540 , Y=1062 , Z=-660GYRO : X=-725 , Y=169 , Z=-258Temperature:2738ACC : X=17246 , Y=2284 , Z=-128GYRO : X=-1049 , Y=-206 , Z=-250Temperature:2745ACC : X=17560 , Y=1094 , Z=-802GYRO : X=-190 , Y=-333 , Z=-2371Temperature:2752ACC : X=15522 , Y=6718 , Z=-1532GYRO : X=-1233 , Y=-881 , Z=-5025Temperature:2758ACC : X=12136 , Y=11176 , Z=-1462GYRO : X=-1309 , Y=-569 , Z=-5168Temperature:2762ACC : X=9374 , Y=13846 , Z=-1524GYRO : X=-1167 , Y=-468 , Z=-2788Temperature:2764ACC : X=9766 , Y=12714 , Z=-2386GYRO : X=757 , Y=-981 , Z=3324Temperature:2769ACC : X=14338 , Y=9206 , Z=-3278GYRO : X=1002 , Y=-456 , Z=2208Temperature:2773ACC : X=16182 , Y=5124 , Z=-3054GYRO : X=347 , Y=890 , Z=3239Temperature:2774ACC : X=17586 , Y=-76 , Z=-1722GYRO : X=1395 , Y=713 , Z=5533Temperature:2792ACC : X=16948 , Y=-4276 , Z=-1744GYRO : X=-36 , Y=-1080 , Z=2626Temperature:2796ACC : X=14582 , Y=-9724 , Z=-1482GYRO : X=2475 , Y=-537 , Z=4286Temperature:2801ACC : X=10942 , Y=-13008 , Z=-558GYRO : X=1316 , Y=-601 , Z=5066Temperature:2805ACC : X=8694 , Y=-14914 , Z=-278GYRO : X=-117 , Y=-88 , Z=293Temperature:2809ACC : X=6540 , Y=-15778 , Z=314GYRO : X=589 , Y=-556 , Z=2217Temperature:2813ACC : X=5366 , Y=-16216 , Z=654GYRO : X=102 , Y=-407 , Z=720Temperature:2817ACC : X=7750 , Y=-14754 , Z=-2234GYRO : X=-6157 , Y=-2149 , Z=-2878Temperature:2826ACC : X=11744 , Y=-7016 , Z=-11324GYRO : X=-5323 , Y=-4320 , Z=-3759Temperature:2832ACC : X=9602 , Y=-2234 , Z=-14698GYRO : X=-2110 , Y=-5370 , Z=-2714Temperature:2835ACC : X=3388 , Y=522 , Z=-17144GYRO : X=-982 , Y=-4714 , Z=-2141Temperature:2839ACC : X=-594 , Y=1308 , Z=-17222GYRO : X=-129 , Y=-1672 , Z=-933Temperature:2842ACC : X=-2078 , Y=1736 , Z=-16932GYRO : X=-544 , Y=-2213 , Z=-938Temperature:2842ACC : X=-2782 , Y=1818 , Z=-16780GYRO : X=-1355 , Y=109 , Z=-115Temperature:2846ACC : X=-1748 , Y=2996 , Z=-16712GYRO : X=-679 , Y=1110 , Z=15Temperature:2848ACC : X=8994 , Y=676 , Z=-14522GYRO : X=345 , Y=11567 , Z=62Temperature:2849ACC : X=14846 , Y=292 , Z=-10048GYRO : X=804 , Y=3665 , Z=-53Temperature:2850ACC : X=16938 , Y=-108 , Z=-4118GYRO : X=-2373 , Y=7016 , Z=1027