Code (far from done):
/* Place your initialization/startup code here */
I2C_1_EnableInt(); //Enables interrupt, which is required for most I2C operations.
wrData = 117; //Reg number
uint8 slaveAddress = 0x68;
#define WR_BUFFER_SIZE (2u)
/* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
status = I2C_1_MasterSendStart(slaveAddress, I2C_1_WRITE_XFER_MODE);
while (stats != I2C_1_MSTR_NO_ERROR);
if(status == I2C_1_MSTR_NO_ERROR) // Check if transfer is completed without errors
/*  END OF FILE */
Welcome in the fascinating world of PSoCs!
At very first sight:
The I2C is interrupt-driven, so you MUST enable global interrupts.
It is always advisable to upload the complete project here, so that we all can have a look at. To do so, in Creator "File -> Create Workspace Bundle (minimal)" and then upload the resulting archive here.
For newbees usually C-language and programming turns out to be a problem, too. When the project grows it becomes more and more complex, so when you start early to divide it into smaller pieces it will help you. so your main() may look something like
Remember: a(ny) project gets complicated. And: A function "ReadMPU6050()" can easily be re-used in your next project.
Assumption made you connected SCL to SDA on accel, not the AUX connections
which do not have pullups ?
Also in Creator there is a working example project using I2C you can modify or
I see on schematic different verison in notes section switched SDA, SCL, check your
version. Lastly I see you writing to I2C, but not reading the accel ? Was that config
commands for the accel in the write buffer ?
Thank you very much for sharing your project with us!
Are you building any real project with that MPU6050?
I am trying the same with PSoC 5 LP097 and MPU 6050. I do not understand as to why I observe no data on the COM port. There is no data displayed on the COM port.
- I am using a hyperterminal on Windows(9600 baud, COM2 used).
- I have connected 10K resistances each for the SDL and SCL pins.
- Besides these pins, I used VCC and GND. Rest of the pins are not connected to PSoC.
I have attached the project bundle here for reference. Kindly help.
GY_521.zip 525.0 K
Your error is not in the UART, but in your I2C handling. The last parameter for I2C_MasterSendStart() and I2C_MasterSendRestart() is the indicator for following reads or writes. Use the defined const from I2C.h : I2C_READ_XFER_MODE, I2C_WRITE_XFER_MODE and when writing to slave I2C_ACK_DATA, I2C_NAK_DATA.
Keep in mind that most I2C APIs return a status which indicates an error. Better check that until program works.
I do not understand how reading out the register "MPU6050_RA_ACCEL_XOUT_H" gives all the 14 bytes including the temp. sensor values in the code provided by Hernán Sánchez. Isn't it supposed to be reached out to individual registers and try reading out the content?
And after making the changes suggested by you, I observe the following behaviour -
1. I am able to read the bytes from the MPU6050 registers, when I do not connect the PULLUP resistors for SDA and SCL. In case of PULL UP registers connected, the command - " I2C_MasterSendStart(MPU6050_DEVICE_ADDRESS, I2C_WRITE_XFER_MODE) " fails with a return error - I2C_MSTR_NOT_READY. Are pull up resistors not required in this case?
2. After reading some random bytes (approx. 14*16 or 14*19), the slave device stops responding. The command - " I2C_MasterWriteByte(reg)" fails with a return value - I2C_MSTR_ERR_LB_NAK.
3. How do I display the values read from the registers to the Hyperterminal. All I can see is some random characters being written on the terminal.
Could you please provide some tips on this?
Can you post your actual project state, please.
OK, so I made some more changes to my code handling the return values from the I2C APIs, and now I am able to get constant data from the MPU 6050. However, the data I receive are completely zeroes. Attaching the project bundle for reference.
Your conversion of hex values to ASCII is not correct. Consider that a uint8 keeps two hex digits, simply adding 0x30 will not convert them.
You might use sprintf() to convert and format into a buffer which you can send to UART. Do not forget to set the heap size to 0x0200 (System view of design wide resources).