Escolar Documentos
Profissional Documentos
Cultura Documentos
In the section "pinout" we enable the port I2C1 and put it in "I2C" mode,
as shown in Figure 4. Also in the image seen in the microcontroller pins
ports are enabled PB7 = I2C_SDA and PB6 = I2C_SCL . These ports
are connected by default to the sensor, therefore it is not necessary to
move or rename them.
If we want to use another device with I2C protocol, it will not be possible
to use the I2C1 bus , since it is not mapped to any physical pin of the
expansion ports CN1, CN2 and CN3; however the Ophyra
microcontroller has 2 additional I2C ports, which are possible to enable
(I2C2 or I2C3). We recommend reviewing the Ophyra manual in
section 5.14 Expansion ports to identify in which connector we have an
additional I2C port available.
Image 4. Enabling the I2C port and devices connected to the same
port.
Then we click on the "Configuration" tab and enter the configuration
windows of each activated resource (see image 5). In the picture 6 the
configuration window of the RS-232 protocol is observed. We make sure
that the settings are properly activated and we click on the accept
button. Now we do the same with the configuration of the I2C port.
Image 5. Buttons to select.
Image 6. Parameters of the RS-232 port and the I2C port.
Click on "Apply" and then "Ok". Now we can proceed to generate the
code, by clicking on the "Generate Source Code ..." icon, located at the
top of the main window, where the following window will be displayed:
Picture 8 . Proyect settings window .
Finally we click on "OK", and then the window that shows the generation
of the project will be displayed, in which we will click on "Open Project".
Within the "USER CODE BEGIN Includes" section we add the directives
that are observed in image 10. The first two lines correspond to standard
C libraries, while the third line corresponds to the slave address or ID of
the MPU6050 sensor (see table 1). It should be noted that the address
0x68 is shown in table 1 and 0xD0 is shown in the image. This is so,
because the number 0x68 was previously made a shift to the left
remaining at the value of 0xD0. This shift is necessary because in the
I2C protocol, the least significant bit of the ID is used as a write / read
command. This bit is manipulated by the master device, setting it to 1
when you want to read information and 0 when you want to write to the
slave device.
Section 2 sends the first sensor configuration data. The data that is sent
is 0x6B and 0x00; the first is the address of the sensor configuration
record. The second data corresponds to the data to be written to the
sensor. According to the sensor specification sheet when a zero is
written to the 0x6B register, the sensor exits the standby or standby
state and begins to perform the acceleration and rotation
measurements. The instruction used to send data is:
Arg1: The I2C port with which you are working is specified.
Arg2: Is the ID or address of the slave device.
Arg3: Buffer or array of data to be sent to the slave device.
Arg4: Amount of data to send .
Arg5: Maximum waiting time.
Now we write the code shown in figure 12. This section of code in
general reads the data coming from the Accelerometer, Gyroscope and
temperature; and sends them to the USB-Serial port, for viewing on any
serial terminal. In this particular case we will use the serial terminal that
is integrated with Atollic-TrueStudio.
Image 12. Program section to be executed in infinite while loop .
Section 1 sends only one data (0x3B) to the sensor. This causes the
sensor to position its pointer in the 0x3B register. This register
corresponds to the most significant byte of the X axis of the
Accelerometer. The sensor data sheet specifies that all readings made
by the sensor are stored in stacked records in its internal memory. In
addition, each reading is 16 bits in length, so it uses 2 bytes for each
axis sensed. Therefore, there are 6 bytes for the Accelerometer data (ie
2 bytes for each Cartesian axis: X, Y, Z), 6 bytes for the Gyroscope data
and 2 bytes for the temperature sensor data. In total there are 14 bytes
that have to be read to obtain the complete sensor information.
Section 2 , reads the 14 bytes of information, by means of the following
instruction:
Arg1: The I2C port with which you are working is specified.
Arg2: Is the ID or address of the slave device.
Arg3: Buffer or data array where the information coming from the
slave device is stored .
Arg4: Amount of data to be received .
Arg5: Maximum waiting time.
Finally we compile our program to generate the * .HEX file and download
it to our card. To do this, follow steps 1 to 5 of this GUIDE .
Conclusions
After loading the program to the Ophyra card, we will open the terminal
of the serial port that is integrated in Atollic TRUESTUDIO. In the picture
13 the button that we must press is shown so that we will have the
terminal. Note : This button can vary from location according to the
configuration of your program, for it is necessary that you identify it well.
Image 13. Button to open the serial terminal of Atollic.
Clicking on the button will open a configuration window like the one
shown in image 14.
Image 14. Configuration window of the Atollic serial terminal.
We select the COM port corresponding to our Ophyra card, the other
parameters are not modified as this was configured by default the
Ophyra serial port; finally we click on OK. Atollic will open a window in
the lower part of the window, next to the console. In this window we will
observe the data sent by Ophyra (see image 15).
To end or close the communication with the serial port, we must press
the button that is highlighted in red in image 15.
Figure 15. Activated Atollic serial terminal. Deployment of data.
We have configured and programmed the MPU6050 sensor in Ophyra
through the I2C serial port. We also use the RS232 serial port, to
visualize the information coming from the sensor in the Atollic terminal.