Documente Academic
Documente Profesional
Documente Cultură
The objective of this tutorial is to show how to configure our Ophyra development
card to read the MPU6050 sensor. This sensor is an integrated Accelerometer and
Gyroscope with I2C communication. To demonstrate its operation we will make an
example where we will send the data read from the sensor to the RS232 port to
visualize them in the compiler terminal.
The next step is to enable the I2C port number 1. The MMPU6050 sensor is
connected to this port. As you know this port uses two pins or ports to perform the
communication:
SDA: Bidirectional synchronous serial port, through which data is
transmitted from device to device.
SCL: Synchronous data serial port, this generates a clock signal with the
speed of data transfer to the sensor.
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.
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.
The section marked with number 1 corresponds to the variables used to transmit
and receive data from the MPU6050 sensor.
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.
Section 3 configures the sampling frequency of the sensor at 1KHz, writing the
data 0x07 in the internal register 0x19 of the sensor.
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.
Arg1: The I2C port with which you are working is specified.
Arg3: Buffer or data array where the information coming from the slave
device is stored .
Section 3 is responsible for assembling the data in floating 32-bit words. The first
thing you do is a concatenation (at 16 bits) of the most significant byte with the
least significant byte of each acceleration axis, temperature sensor and
gyroscope. Then divide each concatenation by a factor of sensitivity or conversion,
as specified in the data sheet of the sensor.
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 .
The last step is to program your Ophyra card. If you have never done it, follow
steps 6 through 12 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).
For this example, a chain of data corresponding to each reading of the axes of the
accelerometer-gyroscope and of the temperature sensor will be
observed. Accelerometer measurement units are configured in gravities or G ; for
the Gyroscope they are given in º / sec and for the thermometer in ºC . By default
the Accelerometer is configured for a range of 2G , but can be configured for
ranges of 4, 8 and 16G. Similarly, the Gyroscope is set to a sensitivity of 250 º /
sec , but can be configured for sensitivities of 500, 1000 and 2000 º / sec.
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.
My problem is that with a higher frequency of data downloading, the microcontroller hangs right
after the start.
But one by one, it communicates with the sensor via I2C. The configuration of the gyroscope and
accelerometer looks like this:
The data reading itself triggers an interrupt from the timer with the frequency of 1kHz (allowing
the accelerometer enough)
where:
ACC_CONFIG = 28
GYRO_CONFIG = 27
SETTINGS_ACC = 0x00
SETTINGS_GYRO = 0x00
ACCEL_XOUT_H = 0x3B
Well, just at such a frequency, the microcontroller often hangs and nothing happens right after
the start (it reads data using stmStudio), usually it will catch after a few resets.
By reducing the frequency of interruption, the problem occurs less often, until at a certain level it
completely disappears. The problem also disappears when commenting on the lines to read the
value.
Veeeeeeefugehwfk
Microsemi’s broad portfolio of RF and microwave technology is perfectly positioned to solve complex
engineering problems at the discrete, RFIC, MMIC, module and subsystem levels for these
application areas. It is also complemented by a wide variety of FPGA, analog mixed signal, and
discrete power technologies for an overall solution.
Hjg4tuiy45jiuj35i33tiu32