当前位置:网站首页>[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)
[flight control development foundation tutorial 6] crazy shell · open source formation UAV SPI (six axis sensor data acquisition)
2022-07-27 23:56:00 【efans_ Mike】
COCOFLY course
—— Crazy shell · Unmanned aerial vehicle (uav) · series
SPI( Six axis sensor data acquisition )
chart 1
One 、ICM20602 brief introduction
Six axis sensors are widely used in today's smart wear and positioning and navigation products , The best six axis sensor is InvenSense The company's products ,ICM20602 It is one of its excellent six axis sensors .
ICM20602 Integrate 3 Axis accelerometers and 3 Axis gyroscope , The range of gyroscope can be selected
+/-250dps,+/-500dps,+/-1000dps and +/-2000dps These four , The range of accelerometer can be selected +/-2g,+/-4g,+/-8g and +/-16g Four kinds of .
ICM20602 Support as much as 400KHz Of I2C And up to 10MHz Of SPI, It has high interface compatibility .
ICM20602 The physical drawing of is shown below .
chart 2
ICM20602 The pins are shown in the figure below .
chart 3
Two 、SPI summary
SPI Refer to Serial Peripheral Interface Abbreviation , Serial peripheral interface , It's a high speed 、 full duplex 、 Synchronous communication bus , And only four wires are used on the chip pins , The pins of the chip are saved ,SPI Of 4 The communication line is :
(1)MISO Master in slave out interface ;
(2)MOSI Master out slave in interface ;
(3)SCLK Clock signal generated by the master device ;
(4)CS The slave device chip selection signal controlled by the master device .
STM32F103 Of SPI Your clock can reach up to 18MHz, Support DMA.SPI Master slave communication , As shown in the figure below :
chart 4
When there are multiple devices attached to SPI On the bus , The wiring diagram is shown in the figure below .
chart 5
Between single chip microcomputer and peripheral devices SPI Synchronous serial data transmission , Under the shift pulse of the main device , Data is transmitted bit by bit , Little Endian , High position behind , For full duplex communication , The speed of data transmission is generally faster than I2C The bus should be fast , What's the speed Mbp, Compared with other buses ,SPI Simple protocol , The relative data rate is high , however SPI It also has its disadvantages , For example, there is no specified flow control , There is no reply mechanism to confirm whether data has been received .
3、 ... and 、SPI bus protocol
SPI Communication requires the following knowledge : Clock polarity 、 Clock phase and SPI The transmission timing of .
(1) Clock polarity ,SPI By clock polarity (CPOL) To decide when the bus is idle , Synchronous clock (SCLK) Is the level of the signal line high or low . At that time, the polarity of the clock was 0 when (CPOL=0), SCLK The signal line is low when idle ; At that time, the polarity of the clock was 1 when (CPOL=1),SCLK The signal line is high when idle ;
(2) Clock phase ,SPI Through the clock phase (CPHA) Used to decide when to sample the signal . At that time, the phase of the clock is 1 when (CPHA=1), stay SCK The second jump edge of the signal line is sampled ; Whether the jump edge here is a rising edge or a falling edge ? It depends on the polarity of the clock . At that time, the polarity of the clock was 0
when , Take the falling edge ; At that time, the polarity of the clock was 1 when , Take the rising edge ; As shown in the figure below :
chart 6
At that time, the phase of the clock is 0 when (CPHA=0), stay SCK Sample the first jump edge of the signal line . Jump edge is also related to clock polarity : At that time, the polarity of the clock was 0 when , Take the rising edge ; At that time, the polarity of the clock was 1
when , Take the falling edge ; As shown in the figure below :
chart 7
Four 、 SPI register
What is used in this experiment is STM32 The hardware SPI,STM32 The hardware SPI There are many registers involved , Here we choose the more important ones to explain .
(1)SPI_CR1:SPI Control register 1, As shown in the figure below :
chart 8
among SPE by SPI Enable control bit , be equal to 1 Time enabled SPI, be equal to 0 Turn off SPI;BR[2:0] by SPI Baud rate control bit ,BR[2:0] be equal to 000 Then the baud rate is fPCLK/2, be equal to 001 Then the baud rate is fPCLK/4, be equal to 010 Then the baud rate is fPCLK/8, be equal to 011 Then the baud rate is fPCLK/16, be equal to 100 Then the baud rate is fPCLK/32, be equal to 101 Then the baud rate is fPCLK/64, be equal to 110 Then the baud rate is fPCLK/128, be equal to 111 Then the baud rate is fPCLK/256;MSTR by SPI Master slave mode selection bit , be equal to 0 When in slave mode , be equal to 1 Time based mode ;CPOL by SPI Clock polarity setting bit , by 0 Then the idle clock is low , by 1 Then the idle clock is high ;CPHA by SPI Clock phase setting bit , be equal to 0 when , Start collecting at the edge of the first clock jump , be equal to 1 when , In the 2 The edge of the clock jumps and starts to collect .
(2)SPI_SR:SPI Status register , As shown in the figure below :
chart 9
among TXE Is the send buffer status bit , This bit is 0 The send buffer is not empty , by 1 The send buffer is empty ;RXNE Is the receive buffer status bit , This bit is 0 The receive buffer is empty , This bit is 1 when , The receive buffer is not empty .
(3)SPI_DR:SPI Data register , Used to store received or sent data .SPI_DR The description of is shown in the figure below :
chart 10
among DR[15:0] Deposit SPI data .
5、 ... and 、 Six axis sensor data acquisition experiment
Six axis sensor data acquisition experiment use STM32 The hardware SPI With six axis sensor ICM20602 Connect , A serial port 1 namely UART1, adopt USB Connect the serial port module to the computer , hold SPI The obtained six axis data passes through the serial port 1 The serial port debugging assistant transmitted to the computer is displayed . When doing this experiment, you need to temporarily remove the visual module , And the USB The line to serial port is connected to the interface of the visual module . Six axis sensors ICM20602 White on top of the drone RGB In the lantern box , Through the soft row handle SPI And power supply outlet , As shown in the figure below .
chart 11
According to the schematic diagram , You can see ICM20602 Of course SPI The interfaces are :PB13、PA5、PA6、PA7, As shown in the figure below .
chart 12
A serial port 1 The configuration of can refer to 《 A serial port ( Basic transceiver ), Configuration code ( By calling the official library ) obtain ICM20602 The idea of data code writing is as follows :
Code thinking
surface 1
SPI The initialization code is as follows :
chart 13
SPI The reading and writing code of is as follows .
chart 14
ICM20602 The initialization code of is as follows .
chart 15
ICM20602 The reading and writing code of is as follows .
chart 16
Pay attention to configuring the serial port for sending , In this way, the data can be sent to the computer . A serial port 1 adopt USB Connect the serial port module to the computer , obtain ICM20602 The code is as follows .

chart 17
preservation 、 compile 、 Download code , You can see USB The serial port module is constantly printing ICM20602
Of X High axial acceleration 8 position , The data is shown in the figure below :
chart 18
边栏推荐
猜你喜欢

Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
![[RoarCTF2019]babyRSA威尔逊定理](/img/c1/52e79b6e40390374d48783725311ba.gif)
[RoarCTF2019]babyRSA威尔逊定理

Lua basic grammar learning

File&递归14.1

File & recursion 14.1

BUUCTF-RSA4

The 4-hour order exceeds 20000+, claiming to be "the most luxurious in a million". Is the domestic brand floating?

Put cloudflare on the website (take Tencent cloud as an example)

2022 summer vacation daily question (5)

Spark 离线开发框架设计与实现
随机推荐
Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
How to bold font in Latex & how to make circle serial number
Design and implementation of spark offline development framework
How to use C WinForm to copy files and display progress
Reduce error demonstration
传奇服中怎么刷装备
[MRCTF2020]babyRSA
测试类中的断言机制
BUUCTF-RSA roll
MapReduce (III)
硬布线控制器的特点:
Key points of data management
JS提升:JS中的数组扁平化问题
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
Flutter pull_ to_ refresh-1.6.0/lib/src/internals/slivers. dart:164:13: Error: Method not found: ‘descr
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
Features of hardwired controller:
Use a grayscale filter
Flutter pull_to_refresh-1.6.0/lib/src/internals/slivers.dart:164:13: Error: Method not found: ‘descr
CaEGCN: Cross-Attention Fusion based Enhanced Graph Convolutional Network for Clustering 2021