当前位置:网站首页>[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
边栏推荐
- What are the methods of process synchronization?
- 给网站套上Cloudflare(以腾讯云为例)
- What is the prospect of low code development? Are you really optimistic about low code development?
- BUUCTF-RSA roll
- Why does redis cluster use reverse proxy? Just read this one
- Flutter pull_to_refresh-1.6.0/lib/src/internals/slivers.dart:164:13: Error: Method not found: ‘descr
- 五子棋人机对战实现
- Binary conversion method
- The share price soared 180.46%! Shanghai silicon industry, the leader of domestic large silicon wafers, is listed: the cumulative net profit in recent four years is less than 60million
- Reduce error demonstration
猜你喜欢

C # delegate usage -- console project, which implements events through delegation

Redis hash underlying data structure

Redefine analysis - release of eventbridge real-time event analysis platform

【zer0pts CTF 2022】 Anti-Fermat

Xss.haozi.me practice customs clearance

29. Learn the stacked column chart of highcharts using percentage

资深如何确定软件测试结束的标准

Explain the idempotence of distributed system in detail

Posture recognition and simple behavior recognition based on mediapipe

Lua basic grammar learning
随机推荐
Edit the copy and paste judgment problem (bug?), WYSIWYG display symbol problem feedback.
TSMC 3nm detail exposure: transistor density as high as 250million /mm ², Greatly improved performance and energy efficiency
TCP sticking and unpacking problem + Solution
硬布线控制器的特点:
J9数字科普:Sui网络的双共识是如何工作的?
字符流学习14.3
Sudden, wechat important notice
为什么 Redis 集群要使用反向代理? 看这篇就明白了
Realization of gobang man-machine combat
【zer0pts CTF 2022】 Anti-Fermat
Your list is too laggy? These four optimizations can make your list silky smooth
BUUCTF-childRSA费马小定理
C # delegate usage -- console project, which implements events through delegation
给网站套上Cloudflare(以腾讯云为例)
Use of date
Why do I need to wait for 2msl?
[NCTF2019]babyRSA1
Bank marketing predicts the success rate of a customer's purchase of financial products
Unity implements simple Sketchpad drawing function (notes)
数据管理的重点