STM32 I2C Lecture 34 – Exercise: Coding Part 1

  • Post author:
  • Post category:Blog

 

Exercise: Coding Part 

 

Steps to create a new source file is as follows:

  • Copy the old source file and paste it into the source file section. Then change the name to 011I2C_master_rx_testing and save it by clicking on ok, as shown in Figure 1.
Figure 1. Change the source file name is 011I2C_master_rx_testing.

 

  • Exclude the Tx testing source file from the build: Right-click on the source file, go to properties, select the option C/C++ to build, and then click on apply.
Figure 2. Exclude the Tx testing source file.

 

In the Rx testing file, the slave address is 0x68, which is defined as shown below. 

#include<stdio.h>
#include<string.h>
#include "stm32f407xx.h"

//extern void initialise_monitor_handles();

#define MY_ADDR 0x61;

#define SLAVE_ADDR 0x68

void delay(void)
{
for(uint32_t i = 0 ; i < 500000/2 ; i ++);
}

I2C_Handle_t I2C1Handle;

 

 

Remove all the codes which are not necessary:

  • Rename the send_data array as rcv_buf (Figure 3) since we are receiving the data.
Figure 3. Code to declare receive buffer.

 

  • Remove master send data shown in Figure 5.
Figure 5. Code to master send data.

 

Keep all the codes that are necessary:

  • Keep the GPIO init as it is (Figure 6).
Figure 6. Code for GPIO init.

 

  • Keep I2C1_inits shown in Figure 7.
Figure 7. Code for I2C1_inits.

 

  • Keep the GPIO button init shown in Figure 8.
Exercise: Coding Part 1
Figure 8. Code for GPIO button init.

 

  • The peripheral control code shown in Figure 9 is required, so keep that as it is.
Exercise: Coding Part 1
Figure 9. Code for peripheral control.

 

Now let’s write the code for I2C transactions to read 1-byte length information from the slave. For that, you have to perform the first data write by sending the command code 0x51, as shown in Figure 10.

Exercise: Coding Part 1
Figure 10. Code to send the command code 0x51.

 

Code for the master to read response from the slave, which is nothing, but 1-byte length information is shown in Figure 11.

Exercise: Coding Part 1
Figure 11. Code for the master to read the response from the slave.

 

Write the code for I2C transactions to read “length” bytes of data. First, you have to perform data write by sending the command code 0x52 to the slave (Figure 12).

Exercise: Coding Part 1
Figure 12. Code for I2C transactions to read “length” bytes of data.

 

Next, the master reads the whole data from the slave by using the data read function (Figure 13).

Exercise: Coding Part 1
Figure 13. Code for the master to read whole data from the slave.

 

Specify the size of the recv_buf array, as shown in Figure 14.

Exercise: Coding Part 1
Figure 14. Code to specify the size of recv_buf array.

Build the project to see if everything is fine or not. Let’s continue the code in the following article.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.