Graduation project "Design and Implementation of Intelligent Table Tennis Ball Picking Car Based on STM32" Process Notes from Scratch

Graduation project "Design and Implementation of Intelligent Table Tennis Ball Picking Car Based on STM32" Process Notes from Scratch

Actual car

Actual car
Preface:
This article was written after recording. If there are any deficiencies, criticism and correction are welcome.
If you think the article is well written, you are welcome to connect it with one click. (Bi Xin orz)

Table of contents

Zero, data download

Correct atomic information:

Reference article: (115 messages) Learning stm32f103c8t6, ​​how to download and use information from the official website of Zhengdian Atom_quxuexi's blog-CSDN blog

other information:

Link: https://pan.baidu.com/s/1u_wxi-8H6zIsLmtEf_jZqw?pwd=2023
Extraction code: 2023

Information description: Thanks to Punctual Atom for providing the information. The information on Zhengdian Atom is explained in detail and comprehensively, and its nutritional value is very high. It is much more important than this document. Other information is mainly supplementary to the on-point atomic information.

1. Hardware purchase and learning

1. Hardware List

illustrate:

  1. The component price statistics time is 2023.5.27, and actual purchases may experience slight changes.
  2. The unit price is the cost of buying one on Taobao, including possible shipping charges.
  3. In addition to the parts listed here, you will also need scissors, screwdrivers, and a computer. (It’s best to have an electric soldering iron and soldering wire, but it doesn’t matter if you don’t have one)
serial number Component name quantity unit price total price
1 Double-layer trolley chassis 1 29.8 29.8
2 L298N chip motor driver module 1 6.8 6.8
3 3 12V lithium batteries 1 30 30
4 STM32F103C8T6 minimum system board 1 15.4 15.4
5 Maix Dock development board 1 205 205
6 HC-05 Bluetooth module 1 12.2 12.2
7 Infrared obstacle avoidance module 3 5.1 7.8
8 PWLINK2 Lite Debug Downloader 1 9.9 9.9
9 MB-102 breadboard 1 3.6 3.6
10 Male to male, female to female, male to female Dupont wire, one row each 20cm long 1 set 10.68 10.68
11 Hexagonal copper pillar M3 (20 pieces) 1 package 2.8 2.8
12 Copper screw rod brass thread rod full thread screw rod tooth rod full thread 2 sticks 2.3 4.6
13 Stainless steel hexagonal nut 304 (20 pieces) 1 package 1.72 1.72
14 Stainless steel tripod bracket 1 3.5 3.5
15 Light emitting diodes (5mm, 50 pieces) 1 package 4.8 4.8
16 Galvanized gasket half a catty 4.5 4.5
17 Nylon tie 1 package 1.2 1.2
total 342.1

2. Introduction to purchasing each component

If you want to buy components, I recommend directly copying the component names in the file and searching on Taobao, and then looking for ones with appropriate prices and good reviews to buy. The purchase link in the document is just a link that I personally purchased at that time. It may not still have excellent prices and good reviews now, so I encourage everyone to shop online scientifically.

The most important function of the transaction snapshot is the "Color Classification" column. That column explains the name, model and other important parameters of the purchased item. It is an important reference option when purchasing in the merchant's store.

1). Double-layer trolley chassis

  1. Actual components:

    Car base

  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Smart Car Kit 51 MCU/STM32/UNO Development Board Tracking and Obstacle Avoidance Remote Control Robot Chassis - Taobao (taobao.com)

  3. Transaction snapshot:image-20230527143403372

  4. Component function: See "Baby Details" on Taobao product page

  5. Instructions for use: See "Baby Details" on Taobao product page

  6. Learning materials: See "Baby Details" on Taobao product page

  7. Personal note: There is nothing to learn about this component. Just assemble it and pay attention to the positive and negative poles of the motor and the acceptable power supply voltage.

2). L298N chip motor driver module

  1. Actual components:L298N physical picture
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): L298N chip motor driver board module stepper DC deceleration smart car robot microcontroller red-tmall.com Tmall
  3. Transaction snapshot:image-20230527144315811
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: Pay attention to which power supply method this component uses, 12V or 5V. Pay attention to whether the jumper cap should be unplugged. This motor driver can drive two motors.

3). 3 12V lithium batteries

  1. Actual components:12V lithium battery
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Smart Car 18650 Rechargeable Lithium Battery Pack 12V Large Capacity LED Light Strip Router Light Cat Bar Light 7.4 - Taobao (taobao.com)
  3. Transaction snapshot:image-20230527144751517
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: In the later stages of the project, I felt that I could use a more common power bank to replace the lithium battery, which would save a lot of money. However, the power supply methods of the two are different. If you want to use a power bank to achieve the same effect as a lithium battery, use the terminal wire. For power supply, you also need to purchase a "USB male to XH2.54-2P terminal cable"

4). STM32F103C8T6 minimum system board

  1. Actual components:stm32
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): STM32F103C8T6 core board C6T6 STM32 development board ARM microcontroller minimum system experiment board - Taobao (taobao.com)
  3. Transaction snapshot:image-20230527145417024
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: I bought the one with the pin headers soldered, which is a little more expensive than the one without soldering, but saves trouble. Note that the board must be powered off when burning code. When working, do not blindly connect unused pins. Do not connect any wires to the four pins for programming. Do not use the GND among them. If the GND pins on both sides of the board are not enough, use DuPont. Wires or jumpers connect GND to the breadboard to increase the number of GNDs. Its power supply method can be connected to its 5V pin using a 5V power supply, or it can be powered by a Micro port. When using, you should also pay attention to the use of the two jumper caps and whether they need to be unplugged.

5). Maix Dock development board (not MAIX-II M2dock)

  1. Actual components:k210M1W development board

  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Sipeed Maix Dock K210 AI+lOT Deep Learning Vision Development Board-Taobao (taobao.com)

  3. Transaction snapshot:image-20230527150216403

  4. Component function: See "Baby Details" on Taobao product page

  5. Instructions for use: See "Baby Details" on Taobao product page

  6. Learning materials: See "Baby Details" on Taobao product page

  7. Personal note: I only use it to identify table tennis balls, and then after a series of processing (implemented by writing internal programs), I communicate with STM32 through the serial port. The power supply method I used was Micro port power supply. At first, I also used the 5V pin to power it, but later I found out that after working for a while, the chip lost power for no reason and could not work normally. I simply used the charger directly later. Power it through the Micro port.

  8. Why choose this development board? The process is a bit dramatic. Let’s take a look at some product introductions of MAIX: Sipeed open source product overview - Sipeed Wiki . I was also wondering which one to choose. At the beginning, I first thought of using openMV to identify table tennis balls. Later, I asked the teacher and said that the installation was not reimbursed, and openMV was so expensive (frequently 300+, 500+), so I thought After changing the board, I accidentally saw someone saying that k210 can replace openmv, and the computing power is still higher, so I wanted to buy k210. The MAIX series products use k210, which is the Maix-I series, so I was thinking of choosing one from the Maix-I series. At that time, I mainly considered the Sipeed Maix Bit Sipeed Maix Bit RISC-V AI+lOT K210 direct plug-in breadboard Development board kit - Taobao (taobao.com) and Sipeed Maix Dock Sipeed Maix Dock K210 AI+lOT deep learning visual development board - Taobao (taobao.com) . The prices of the two are about the same, both are around 185r. I saw it by chance later. An article: (113 messages) MaixII-Dock (v831) study notes - first introduction to v831_The Cat in the Cardboard Blog - CSDN Blog It said that the Dock is good, so I bought the Sipeed Maix Dock. I didn't see it clearly at the time, but it actually said MaixII-Dock (v831). Hey, I accidentally hit it.

    There are M1 Dock and M1W Dock in the purchase link. The difference between them is that M1W Dock has one more WIFI module than M1 Dock, so the price is 15r more expensive.

6). HC-05 Bluetooth module

  1. Actual components:image-20230529113159299

  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): HC-05 master-slave integrated Bluetooth module with base plate, wireless serial port transparent transmission communication HC-05 Bluetooth module - Taobao (taobao.com)

  3. Transaction snapshot:image-20230527150618703

  4. Component function: See "Baby Details" on Taobao product page

  5. Instructions for use: See "Baby Details" on Taobao product page

  6. Learning materials: See "Baby Details" on Taobao product page

  7. Personal note: HC-05 has three models: compatible version, original version, and affordable version. The affordable version is the cheapest, but it can only be connected to other Bluetooth devices as a slave, and cannot actively connect to other Blue Night devices. This is the single slave mode; both the compatible version and the original version can be used as a host to connect to other Bluetooth devices, or as a slave. Being connected by other Bluetooth devices is the master-slave mode. The difference is that the compatible version is cheaper than the original version because it is not a genuine component.

    HC-05 is a Bluetooth serial port module commonly used for wireless communication. The difference between the compatible version and the original version is:

    1. The original version refers to the module produced by the original manufacturer of HC-05, and the compatible version refers to the compatible module produced by other manufacturers.

    2. In terms of function, there is no difference between the original version and the compatible version, both can realize Bluetooth serial communication.

    3. In appearance, there may be some differences between the original version and the compatible version, such as size, color, etc.

    4. In terms of price, compatible versions are usually cheaper than original versions.

    It should be noted that since the compatible version is produced by other manufacturers, its quality and stability may be different, so when choosing, you need to pay attention to choosing products from regular manufacturers.

    If Bluetooth is only used to passively wait for other devices to connect, you can choose the affordable version. You can choose to purchase the affordable version for this project.

    Before use, you need to configure this component, enter the AT mode configuration, set the baud rate and so on.

7). Infrared obstacle avoidance module

  1. Actual components:Infrared obstacle avoidance
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Infrared barrier module/obstacle avoidance car obstacle avoidance sensor/black and white line recognition distance adjustable photoelectric switch - Taobao (taobao.com)
  3. Transaction snapshot:image-20230527150757928
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: There is a cross screw in the blue cube above. Rotate this to adjust the infrared detection distance. However, the accuracy of the component may not be high because it is cheap after all.

8). PWLINK2 Lite debug downloader

  1. Actual components:PWLINK2 Lite
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): PowerWriter PWLINK2 Core Creation Workshop Simulation STM32 Burning Stlink Debugging GD32 Downloader-tmall.com Tmall
  3. Transaction snapshot: [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-9tXZMr06-1688999686627) (https://article.biliimg.com/bfs/article/ad182f8fe94afed9213f7b832730368d9f476a66. png)]
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: When I bought it, it was still 9.9 yuan, but now the price has increased unexpectedly. But if you look for that flagship store, it’s still 9.9 yuan. When burning code, you need to configure some parameters in the magic wand in keil. When debugging the Bluetooth serial port, because Bluetooth needs to be connected to a 5v VCC, the VCC of PWlink lite supports two output voltages, 3.3V and 5V. The default is 3.3V output. If you want it to output 5V, you can set the burner it comes with. settings in the recording software.image-20230527151618104

9). MB-102 breadboard

  1. Actual components:Breadboard

  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): MB-102 breadboard 65 Dupont lines 830-hole development board power module electronic DIY experiment kit 400-tmall.com Tmall

  3. Transaction snapshot:image-20230527151658578

  4. Component function: See "Baby Details" on Taobao product page

  5. Instructions for use: See "Baby Details" on Taobao product page

  6. Learning materials: See "Baby Details" on Taobao product page

  7. Personal note: This kind of breadboard can directly plug the STM32F103C8T6 into it. Don’t buy the one in the picture. I bought this kind of breadboard at the beginning, but it couldn’t plug in the STM32. After I asked the customer service, he said that this kind of board is for To plug in DuPont cables and jumpers:image-20230527152347599

  8. Hole board circuit board

    What is the difference between perforated circuit board and breadboard?

    Both perfboard circuit boards and breadboards are commonly used tools in electronic prototyping.

    Perforated board circuit board is a circuit board based on fiberglass board. Its surface has many small holes through which electronic components can be inserted into the circuit board and connected by soldering. Hole board circuit boards are suitable for circuit designs that require high-density wiring, but they require manual welding and require a long production cycle.

    A breadboard is a circuit board based on a plastic board with many small holes on its surface through which electronic components can be inserted into the circuit board and connected through sockets. Breadboard is suitable for rapid prototyping, does not require welding, and has a short production cycle.

    In general, perfboard circuit boards are suitable for circuit designs that require high-density wiring, while breadboards are suitable for rapid prototyping.

    Hole board circuit board

10). Male to male, female to female, male to female Dupont wire, each row is 20cm long.

  1. Actual components:dupont line
  2. Purchase link (you can search based on the component name on Taobao, taking price and sales into consideration): DuPont wire female to female male to female male to male 40P color cable connecting wire male to bus 10/30/20/40CM-tmall.com
  3. Transaction snapshot:image-20230528112449039
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: The 20cm length is enough. You don’t need to tear it into pieces one by one when using it. For example, when connecting the Bluetooth module, you need four DuPont wires. You can tear off four wires from the top at one time, so that it will not look messy.

11). Hexagonal copper pillar M3 (20 pieces)

  1. Actual components:Hexagonal copper pillar
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Hexagonal copper pillar M3M4M5M6 single-head copper stud chassis motherboard screw pillar copper pillar isolation pillar support M2M2.5-tmall.com Tmall.
  3. Transaction snapshot:image-20230528112531466
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: Hexagonal copper posts are used to fix other components. Because the holes drilled on the trolley are 3mm, I bought 3mm ones.

12). Copper screw rod, brass threaded rod, full thread screw rod, fully threaded rod

  1. Physical component: [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-QyDwiNpt-1688999686634) (C:/Users/zhouxin/Documents/study Learning/Computer Science and Technology (embedded)] four-year study materials/senior year/graduation project/2023 graduation design template (for reference)/graduation design materials/graduate thesis insertion picture/actual component picture/copper wire rod.jpg)
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Copper wire rod, brass thread rod, full-thread screw rod, full-thread stud M3M4M5M6M8M10M12M14-Taobao.com (taobao.com)
  3. Transaction snapshot:image-20230528112851735
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: One copper wire rod is used as the central axis of the trolley roller, and the other is used to tie it to the trolley. I bought two of them for express delivery. It's okay to just buy one, but you need to think of another way to fix the other end to the trolley.

13). Stainless steel hexagonal nut 304 (20 pieces)

  1. Actual components:Stainless steel hexagonal nut 304
  2. Purchase link (you can search based on the component name on Taobao, taking price and sales into consideration): 304 316 stainless steel hexagonal nuts, bolts, nuts, screw caps, M3M4M5M6M8M10M12M33-tmall.com Tmall.com
  3. Transaction snapshot:image-20230528113042960
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: Hexagonal nuts are mainly used on the roller to fix the transparent acrylic plates on both sides.

14). Stainless steel tripod bracket

  1. Actual components:Stainless steel tripod bracket
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Thickened stainless steel tripod bracket storage rack wall support rack partition bracket nine-bit rack shelf wall hanging bookshelf - Taobao (taobao.com)
  3. Transaction snapshot:image-20230528113410363
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: The purchase of the support frame was a bit unexpected. I originally wanted to buy the support frame as a roller stand. Later, I customized an acrylic roller stand and did not use this iron stand. But when I use Maix Dock to identify table tennis balls, I need to place the camera higher, so I use the camera set up on this bracket.

15). Light-emitting diodes (5mm, 50 pieces)

  1. Actual components:led
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Light-emitting diode_Taobao search (taobao.com)
  3. Transaction snapshot:image-20230528114234638
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: This component can be used for breathing lamp experiments, and can also be used to detect whether a certain power pin has power. Be careful not to use a 5V voltage to connect a light-emitting diode. It will burn out as soon as you connect it. You can see that it only flashes once and then goes out. Then the diode is out forever. I originally bought light-emitting diodes mainly to test whether a certain GPIO port would output a high level, mainly to connect the four lines of the motor driver. Some GPIO ports of STM32 are easy to be damaged by me. I need this method to detect whether that pin is good.

16). Galvanized gasket

  1. Actual components:Galvanized gasket
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): Galvanized gasket_Taobao search (taobao.com)
  3. Transaction snapshot:image-20230528115336015
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: Galvanized gaskets are also used to fix two transparent acrylic plates. At first, I made some gaskets using Coke bottle caps, but found that plastic gaskets are easily deformed and affect the shape of the roller, so I made them. Bought hard iron spacers.

17). Nylon cable ties

  1. Actual components:Nylon tie
  2. Purchase link (you can search on Taobao based on the component name, taking price and sales into consideration): 3*100 transparent cable ties, nylon cable ties, 4x200 plastic buckle binding strips, powerful large size, tired dogs - Taobao (taobao.com)
  3. Transaction snapshot:image-20230528174833282
  4. Component function: See "Baby Details" on Taobao product page
  5. Instructions for use: See "Baby Details" on Taobao product page
  6. Learning materials: See "Baby Details" on Taobao product page
  7. Personal note: Cable ties are mainly used to fix some parts and are disposable items.

3. Acquisition and learning of data on each component

1). Double-layer trolley chassis

Start by assembling the car. Before assembling, you can download the "Attachment 1 - Car Base Installation Instruction Document.pdf" in "3. Acquisition and Learning of Each Component. Data Connection " to have a rough look at the installation steps of the car, but don't do it yet. Installation, because the motor has not been wired yet, it is not convenient to solder copper wires to the motor after assembly. It is best to use a longer copper wire for welding, just use a 20cm male-to-male DuPont wire.

We only bought one motor driver, which can only drive two motors, so we need to connect the motors on the same side in series to work synchronously.

Solder the copper wire to the motor:

DC motor in series

If you don't have a soldering iron or solder, you can buy it, or you can tie the copper wire directly into the copper hole of the motor. However, this has potential safety hazards. It is best to wrap it with a layer of insulating tape.

After connecting the copper wires to the motor, start assembling the car base.

Base motor wiring

At this point the base part of the car is done.

2). L298N chip motor driver module

Learning materials for this component are from

Taobao: L298N motor driver board module stepper motor smart car robot-tmall.com Tmall

csdn: (113 messages) L298N, motor, microcontroller line connection (51, stm32 program)_l298n motor drive module and microcontroller connection_Xiao Yao Zai Li’s blog-CSDN blog

Station B: Detailed description of L298N module driving DC motor, and analysis of PWM speed regulation code_bilibili_bilibili

The circuit schematic diagram of L298N can be downloaded from "3. Data acquisition and learning of each component. Data connection"

image-20230601204256018

Here we only care about the penultimate sentence.

"The module can use the built-in 78M05 to draw power from the driving power supply. However, in order to avoid damage to the voltage regulator chip, when using a driving voltage greater than 12V, please use an external 5V logic power supply."

L298N is a dual H-bridge driver chip that can control the rotation direction and speed of a DC motor or stepper motor. The 78M05 is a voltage regulator chip that can stably output the input voltage to a DC voltage of 5V. In L298N, the function of 78M05 is to provide a stable 5V power supply for the chip to ensure the normal operation of the chip. At the same time, 78M05 can also provide 5V power supply for external circuits, such as control circuits or sensors.

"When using a driving voltage greater than 12V, please use an external 5V logic power supply." What does this sentence mean?

What this sentence means is that the L298N module can obtain power from the driving power part through the built-in 78M05 voltage stabilizing chip, but when a driving voltage greater than 12V is used, the built-in voltage stabilizing chip may be damaged. In order to avoid this situation, an external 5V logic power supply needs to be used to ensure the normal operation of the voltage regulator chip. To put it simply, in order to protect the voltage stabilizing chip of the L298N module, an additional 5V logic power supply needs to be provided when using a high-voltage driving power supply.

If you don’t understand, take a look at the explanation below

Several power supply methods of L298N.
1. Use 5V voltage to power the L298N
: Both the 12V and 5V of the L298N are connected to the 5V power supply. GND must not only be connected to the GND of the driving power supply (if it is connected to other microcontrollers or other systems, it must be led out from here) A GND is connected to the GND of the microcontroller or system so that the voltage has a reference level), as shown below

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-h6BozVB0-1688999686643)(https://article.biliimg.com/bfs/article/b66cbc902d55a807e4b5ba9d888b650a3cf30f00.png)]

(This method is not recommended, as the supplied voltage may be small and the motor speed may be insufficient)


Although it is not recommended, if you must try it, you can try your own power bank instead of the 12V lithium battery before buying a 12V lithium battery, but you will need to purchase an additional USB male to XH2.54-2P terminal cable. , specifically shown in the "fourth stage of making the car move at full speed" .

Transaction snapshot:

image-20230607215839994

Purchase link: USB male to XH2.54-2P terminal cable 2-core power supply USB plug to PH2.0mm terminal power cable - Taobao (taobao.com)

The output voltage of a normally purchased power bank is 5V. Although the power bank has two USB ports, their output voltages are the same, both are 5V. The difference is that one has a current of 1A and the other has a current of 2A. Remember to use the 2A USB port. .


The working voltage of the L298N module is generally 5V-35V, and the maximum current is 2A. If you use a power bank for power supply, you need to ensure that the output voltage of the power bank is between 5V-35V, and the output current is not less than the working current of the L298N module, which is 2A. If the output current of the power bank is only 1A, the L298N module may not work properly or work unstable. Therefore, it is recommended to use a power bank with an output current of 2A and above to power the L298N module . At the same time, in order to ensure the stability of the circuit, it is recommended to use a power bank with reliable quality.

2. When the power input voltage is 7~12V, the 5v position does not need to be connected to the power supply. This position can output a 5v for powering the microcontroller. The GND of the L298N is connected to the GND of the microcontroller (otherwise there is no reference voltage and normal control cannot be performed). As shown below

image-20230601205621618

(This method is strongly recommended)
3. When the input voltage is greater than 12v, you need to unplug the jumper cap next to the power supply. The 5V terminal needs to be connected to 5v voltage, and GND should be connected to GND, as shown below

image-20230601205643007

Reason for unplugging the jumper cap: Use 5V power supply to power the chip. If you do not disconnect the onboard enable jumper cap, the built-in 7805 voltage regulator chip may be damaged.

image-20230601211807360

L298N

The first two paragraphs here are easy to understand, especially after knowing the three power supply methods of L298N, but we can pay attention to the indicator lights mentioned in it to verify whether they are consistent.

The last paragraph talks about PWM speed control. When I first bought it, both ENA and ENB were plugged in jumper caps and connected to VCC. That is, under this kind of drive, the motor runs at full speed. When we want to adjust the speed of the motor, we need to unplug the jumper caps of ENA and ENB and connect the PWM output signal on the microcontroller to ENA and ENB. ENA and ENB work independently and control the left and right motors respectively. .

DC motor drive:
This driver board can drive 2 DC motors. It is effective when the enable terminals ENA and ENB are high level. The control method and DC motor status table are as follows:

image-20230602205549122

If you want to perform PWM speed regulation on a DC motor, you need to set IN1 and IN2 to determine the rotation direction of the motor, and then output PWM pulses to the enable terminal to achieve speed regulation. Note that when the enable signal is 0, the motor is in a free stop state; when the enable signal is 1, and IN1 and IN2 are 00 or 11, the motor is in a braking state, preventing the motor from rotating.

At this point, the preliminary study of obtaining some information on the "L298N chip motor drive module" has been completed.

3). 3 12V lithium batteries

image-20230602203444265

Note: The red wire is the positive pole and the black wire is the negative pole.

At this point, the study of "3 12V lithium batteries" is completed.

4). STM32F103C8T6 minimum system board

There are many learning materials for this component, which can be obtained from

Taobao: STM32F103C8T6 Development Board C6T6 Core Board Experimental Board Minimum System Board Kit Science and Technology Association Electronics-tmall.com Tmall

Punctual Atomic: 资料盘 Development board information Disk A: https://pan.baidu.com/s/1gCnUaE9sqOMJQbXdq-LByw Extraction code: izgd

The circuit schematic diagram of STM32F103C8T6 can be downloaded from "3. Data acquisition and learning of each component. Data connection" , and can be obtained from "Graduation Project Learning Materials-Circuit Schematic Diagram-Stm32f103c8t6 Minimum System Schematic Diagram Original"

First, watch a physical introduction video: STM32F103C8T6 core board usage special explanation to help you easily play 32 [Open Future Technology]_bilibili_bilibili

Introduction to principle knowledge: [ 1-2] Introduction to STM32_bilibili_bilibili

As for the information link in the introduction of this video, it doesn’t matter whether you download it or not, because the development method of STM32 chosen in this video is standard library development (but as a novice, the more friendly development method is HAL library development), and we also use some of the programs in it No, some documents can also be downloaded from Zhengdian Atom. But there is a picture I always use:

ttttttttttttt

This picture can be downloaded from "STM32F103C8T6 pin definition.xlsx" in "3. Data acquisition and learning of each component. Data connection"

What are the development methods for STM32? (114 messages) Four development methods of STM32_stm32 development_ONE_Day|'s blog-CSDN blog

How to place the jumper caps of boot0 and boot1? (I will give you the conclusion directly. It is usually related to the way of burning the program)

Burning method Operation of boot0 and boot1 boot mode
usb to ttl Set boot0 to 1 and boot1 to 0 system memory
jlink Set boot0 to 0, boot1 is arbitrary main flash memory
stlink Set boot0 to 0, boot1 is arbitrary main flash memory
daplink Set boot0 to 0, boot1 is arbitrary main flash memory

You will understand after reading this article: (115 messages) Detailed explanation of the three BOOT startup modes of STM32 (the most complete in the entire network)_Junior Guoguo’s Blog-CSDN Blog


Tired of studying, let’s watch a short video: Single-chip Microcomputer Graduation Project 2023 Real-life Single-chip Microcomputer Quick Start Module Classification and Topic Recommendation Graduation Ceiling-Level Tutorials Defense Interviews on the Road Talking about Projects on the Road Standing Videos 6 Minutes to Quickly Enter the State Single-chip Microcomputer Graduation Design_Bilibili Mile_bilibili


This project is about learning what you need to use. If you want to learn systematically, you can find a series of courses online and follow it in detail.

At this point, the study of "STM32F103C8T6 Minimum System Board" is completed.

5). Maix Dock development board

How to assemble the Maix Dock development board after you get it, a preliminary understanding: Introduction to using maix dock - Technology - Watch high-definition full genuine video online - Youku (tudou.com)

The video above was narrated by a child, and it’s pretty good.

The official learning website of the development board provides Chinese document teaching, which is very detailed and can be studied:

MaixPy documentation introduction: MaixPy documentation introduction - Sipeed Wiki

At this point, the study of the "Maix Dock Development Board" part is completed.

6). HC-05 Bluetooth module

Where can I learn the information? Of course, go to Taobao. Taobao sells this product. The product introduction will usually introduce the usage and precautions of the product. If this is your first time learning through Taobao, don’t worry, because you don’t know enough about the product, you may overlook some parts of the instructions on Taobao, but as you learn to understand the component well enough, you will pay more attention to it. Some small details, such as power supply, because getting it wrong means that it may burn out. Here we take the hc-05 Bluetooth module as an example to study this component in detail.

First find the Taobao purchase link for the Bluetooth module: HC-05 master-slave integrated Bluetooth module with base plate, wireless serial port transparent transmission communication HC-05 Bluetooth module - Taobao (taobao.com)

Sometimes, some merchants are lazy and don't introduce the component in detail enough or even don't introduce it. Don't worry, if we check another merchant, we can always find the responsible one. Some share information in the form of Baidu Netdisk, while others put the content directly in the baby details. Knowledge can be learned in both cases.

The baby details in the link above provide a detailed introduction to the level logic, product features, product uses of HC-05, how to connect the Bluetooth module to the computer (valid in slave mode), etc. I copied it directly here and made notes to help learn this component.


If you want to enter AT mode

Just keep pressing the button

until the setup is complete

You may not understand this sentence at first. Let’s first understand the several modes of HC-05:

HC-05 Bluetooth module has the following modes:

  1. AT command mode: In this mode, AT commands can be sent through the serial port to configure various parameters of the module, such as baud rate, name, PIN code, etc.

  2. Transparent transmission mode: In this mode, the module sends the data received by the serial port through Bluetooth, and also outputs the data received by Bluetooth through the serial port.

The transparent transmission mode of the HC-05 Bluetooth module is a simple serial communication mode that can wirelessly transmit serial data to the other end through Bluetooth to achieve wireless serial communication. In transparent transmission mode, the HC-05 Bluetooth module will transmit the received data directly to the opposite end through Bluetooth, and will also output the data from the opposite end through the serial port. This mode is very suitable for application scenarios that require wireless transmission of data, such as smart homes, smart vehicles, etc. To enter the transparent transmission mode, you need to set the working mode of the HC-05 Bluetooth module to the transparent transmission mode. For the specific setting method, please refer to the user manual of the HC-05 Bluetooth module.

  1. Master-slave mode: In this mode, the module can serve as a host to connect to other Bluetooth devices, or as a slave to be connected to other Bluetooth devices.

  2. Host mode: In this mode, the module can only serve as a host to connect to other Bluetooth devices.

  3. Slave mode: In this mode, the module can only be connected as a slave by other Bluetooth devices.

The AT mode here can be understood as the setting mode, mainly setting the baud rate.

Baud rate refers to the number of symbols transmitted per second in digital communication. It is commonly used in serial communication and represents the number of bits transmitted per second. Common baud rates are 9600, 115200, etc. A higher baud rate results in faster data transfer, but also increases the likelihood of transmission errors. When performing serial communication, it is necessary to ensure that the baud rate of the sending and receiving parties is consistent , otherwise data transmission errors will occur.

img

try

Notice! ! ! Do not connect the power supply to the signal pin, as it will burn out directly!

The signal pins are RXD and TXD, which are the serial port transmitting pin and the serial port receiving pin respectively. It is marked on the component that these two pins must be connected to a 3.3V signal and cannot be connected to a 5V power supply, so do not connect them incorrectly. . The voltage range of the VCC pin is 3.6V~6V. If it is connected to 3.3V, it will not work properly. It is usually connected to 5V.

image-20230529115327289

1.1 Little common sense (very important):
TXD: Sending end, generally represented as your own sending end. Normal communication must be connected to the RXD of another device.
RXD: Receiving end, generally represented as your own receiving end. Normal communication must be connected to the TXD of another device.
During normal communication, your own TXD is always connected to the device's RXD!
Self-transmitting: During normal communication, RXD is connected to the TXD of other devices. Therefore, if you want to receive the data sent by yourself, as the name implies, you will receive the data sent by yourself, that is, your own TXD is directly connected to RXD to test your own sending and receiving. Whether it is normal or not is the fastest and simplest test method. When a problem occurs, do this test first to determine whether the product is faulty . Also called loopback testing.

Here is a reminder not to make a mistake in connecting RXD and TXD.

The waves sent by HC-05 Bluetooth communication are radio waves, which are electromagnetic waves that belong to the 2.4GHz ISM frequency band. Specifically, it uses the Bluetooth protocol and uses the GFSK modulation method. GFSK is a high-frequency phase shift keying modulation method that can achieve high-speed data transmission and reliable communication in the 2.4GHz frequency band.


1.2 Level logic:
TTL level: Usually data representation uses binary, stipulating that +5V is equivalent to logic "1", 0V is equivalent to logic "0", called TTL signal system, it is a positive logic RS232 level: uses
-12V to -3V, equivalent to logic "0", logic levels from +3V to +12V, equivalent to logic "1", are negative logic

The level logic of HC-05 is TTL level.

1.3 Product features:
1. The core module uses HC-05 slave module. The lead-out interface includes VCC, GND, TXD, RXD, KEY pins, and Bluetooth connection status pin (STATE). The output is low when not connected, and the output is high after connection.
2. The LED indicates the Bluetooth connection status. Flashing quickly indicates that there is no Bluetooth connection. Flashing slowly indicates entering AT mode. Double flashing indicates that Bluetooth is connected and the port is open.
3. The base plate is equipped with an anti-reverse diode , with a 3.3V LDO, and an input voltage of 3.6~6V. The current is about 30mA when not paired, and about 10mA after pairing. The input voltage is prohibited to exceed 7V!
4. The interface level is 3.3V, which can be directly connected to various microcontrollers (51, AVR, PIC, ARM, MSP430, etc.). 5V microcontrollers can also be directly connected, without MAX232 and cannot pass through MAX232!
5. The effective distance in the open space is 10 meters (power level is CLASS 2). It is possible to exceed 10 meters, but the connection quality at this distance is not guaranteed. 6.
After pairing, it can be used as a full-duplex serial port. There is no need to understand any Bluetooth protocol. Support A communication format with 8 data bits, 1 stop bit, and configurable parity check . This is also the most commonly used communication format, and other formats are not supported.
7. You can enter AT command mode by pulling up pin 34 to set parameters and query information.
8. Small size** (3.57cm*1.52cm), factory patch production is accurate, ensuring patch quality. It is also covered with transparent heat shrinkable tube, which is dust-proof and beautiful, and has certain anti-static capabilities.
9. You can switch to host or slave mode through AT commands, and you can connect to designated devices through AT commands.
10. Supports standard baud rates from 4800bps to 1382400bps

Point 1 talks about the pins of HC-05. This project only uses VCC, GND, TXD, and RXD, and the other two pins are not connected to anything.

Point 2 will be understood when using it

Points 3 and 4:

This passage means:

  1. The base plate of the HC-05 module is equipped with an anti-reverse connection diode to prevent the user from damaging the module due to reverse connection when connecting the power supply.

  2. There is also a 3.3V LDO regulator on the bottom board, which can stabilize the input voltage at 3.3V to ensure the normal operation of the module.

  3. The input voltage range of the HC-05 module is 3.6V to 6V. If the input voltage exceeds this range, the module may be damaged.

  4. When the HC-05 module is not paired, the current is about 30mA, and when paired, the current is about 10mA. This is because when paired, the module needs to send and receive data, while when unpaired, the module only needs to wait for connection requests.

In short, this paragraph reminds users to pay attention to the input voltage range when using the HC-05 module, avoid reverse power supply, and pay attention to the current consumption of the module.

Point 6: Full-duplex serial port refers to a serial port communication method that can send and receive data at the same time.

Point 7: Pin 34 is a pin on the HC-05 module. When this pin is pulled high, the HC-05 module will enter the AT command mode. At this time, AT commands can be sent through the serial port to set parameters and query information. .

image-20230529123606189

Looking at the schematic diagram, we learned that pulling pin 34 high is actually pressing the button on the component. This button is the KEY pin in the first point.

1.4 Product usage:
After pairing, it only needs to be used as a fixed baud rate serial port. Therefore, as long as it is a serial port device with a "fixed baud rate, 8 data bits, no parity check" communication format, it can directly replace the original one. wired serial port without modifying the program. Such as data collection, smart cars, serial printers, outdoor dot matrix strip screen control, etc.
**Paired with a computer: ** Suitable for communication between the computer and the device through the Bluetooth serial port, the usage method is the same as the serial port **
Paired with the mobile phone: ** Suitable for communication between the mobile phone and the device through the Bluetooth serial port, the usage method is the same as the serial port
** Paired with a Bluetooth host: **Suitable for two devices to communicate directly through the Bluetooth serial port, such as between a single-chip computer and a single-chip computer, between a wired serial port and a single-chip computer, etc. The usage method is the same as the serial port.

Alternative use:
baud rate conversion, because the receiving end and the transmitting end can select their own baud rates, and can be used as a baud rate conversion device when the amount of data is small.

Paired with computer: The computer communicates with HC-05 through the serial port debugging assistant " XCOM V2.8.exe " (download in ** Information link **). The usage method is introduced below.

Paired with a mobile phone: The mobile phone communicates with HC-05 through the serial port assistant " xnj.lazydog.btcontroller.apk " (download in ** Information link **). The usage method is introduced below.

This item is not used when pairing with a Bluetooth host.

Before using HC-05, we first enter the AT command mode and perform some configurations. For the configuration method, refer to the Taobao introduction below:

1. Equipment preparation required:

①One HC05 Bluetooth module

②One USB to TTL module

③4 DuPont wires

2. Wiring

img

Note: The VCC of the Bluetooth module can only be connected to 5V, not 3.3V!

3. Test

First press and hold the button on the HC05 Bluetooth module, and then power on the Bluetooth module. The LED on the Bluetooth module enters slow flash mode (flashes once in about 1 second), and the AT command test can be performed.

The AT command format is: baud rate 38400, 8 data bits, 1 stop bit, no parity. After each command is entered, you must press the Enter key before sending!

For example: input AT, press the Enter key to send, the Bluetooth module will return OK

①How to modify the Bluetooth module to host mode:

Enter AT+ROLE=1 and then press the Enter key to send. Bluetooth returns OK, indicating that the modification is successful! After powering off and then powering on again, the Bluetooth module will be in master mode and automatically search for surrounding slave devices.

Enter AT+ROLE=0 to change to slave mode. (Note: The default is slave mode, no modification is required)

②How to modify the Bluetooth module name:

Enter AT+NAME=Beijin and press the Enter key to send. Bluetooth returns OK, indicating that the Bluetooth name is changed to Beijing.

③How to modify the Bluetooth module pairing password:

Enter AT+PSWD=2222 and press the Enter key to send. Bluetooth returns OK, indicating that the Bluetooth pairing password has been changed to 2222.

④How to modify the communication baud rate (Note: The baud rate in AT mode is 38400, and the baud rate in normal communication is 9600):

Enter AT+UART=115200,1,2 and then press the Enter key to send. Bluetooth returns OK, indicating that the Bluetooth communication baud rate is modified to 115200, 2 stop bits, Even verification (Note: The default communication parameter is 9600,0 ,0)

Kind tips:

1. The power supply voltage must be 5V, not 3.3V

2. Do not connect the power cord pins to the data lines. Please confirm carefully before wiring.

3. The LED must enter slow flash mode before AT command testing can be performed.

4. After each AT command is input, you must press the Enter key before sending it.

5. If OK cannot be returned, please exchange RXD and TXD

6. Please do not tear off the heat shrink tube or weld it yourself, otherwise it will be regarded as giving up the warranty and return and exchange services.

We use PWlink for the USB to TTL module.

PWLink is a USB to TTL serial port converter that can convert the USB interface into TTL serial port signals and is used to connect and control various serial port devices.

When wiring, the VCC of HC-05 can only be connected to 5V, but the two VDDs of PWlink output 3.3V by default. You also need to set it in the software. For specific operation methods, see: (106 messages) pwlink is used to convert USB to TTL . Entering the AT mode of HC-05_hc05 cannot enter the AT mode_quxuexi's blog-CSDN blog

(1). Enter AT mode to set the baud rate

First press and hold the button on the HC05 Bluetooth module, and then power on the Bluetooth module. The LED on the Bluetooth module enters slow flash mode (flashes once in about 1 second) to enter the AT command mode.

The physical connection diagram is as follows:

image-20230613163811020

Open the serial port debugging assistant "XCOM V2.8.exe" (download in ** Information link **), select the serial port and select the corresponding serial port number to view the method

verify:

We enter "AT" in the input box below, press Enter again, and then click "Send" on the right. At this time, you can see the "OK" sent back by HC-05, as shown below:

image-20230529194057298

Set baud rate:

Enter "AT+UART=115200, 0, 0" (stop bit 0 or 1, check bit 0, 1, 2,) and then press the Enter key and click Send. Bluetooth returns OK, indicating that the Bluetooth communication baud rate has been modified. It is 115200, 1 stop bit, no parity. The reason for this setting is that the default configuration when configuring the serial port in STM32CubeMx is like this. After configuring it here, you can keep the default configuration when configuring the serial port in STM32CubeMx. The following figure shows the default configuration of serial port sending and receiving data in STM32CubeMx:

image-20230613195414956

(Note: [AT+UART= < param1 >, < param2 >, < param3 > param1: baud rate param2: stop bit, 0=1 bit, 1=2 bits param3: check bit, 0=no check ( None), 1=odd parity (Odd), 2=even parity (Even), the default setting is 9600,0,0】)

More settings reference: (127 messages) How to enter the AT mode of the Bluetooth module_zs040 red light flashes slowly_john_crash's blog-CSDN blog

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-OaNT5IXV-1688999686651)(https://article.biliimg.com/bfs/article/5786f71f62463f15ffe031f22896606207016d22.png)]

At this point, the study of "HC-05 Bluetooth module" is completed.

7). Infrared obstacle avoidance module

The learning part of the "Infrared Obstacle Avoidance Module" is explained in the "Tenth Stage: Learning and Using the Infrared Obstacle Avoidance Module". It is never too late to learn when to use it.

8). PWLINK2 Lite debug downloader

(1) Why buy PWLINK2 Lite debugging downloader?

Let’s first understand what are the common downloaders for stm32 microcontrollers?

What I learned about include stlink, daplink, jlink, usb to ttl.

  • ST-Link is an emulator launched by ST Company for debugging and programming STM32 series microcontrollers.
  • DAPLink is an emulator launched by ARM that supports a variety of MCU platforms, including ARM Cortex-M series, Nordic nRF51/nRF52, NXP LPC, etc.
  • J-Link is an emulator launched by SEGGER that supports a variety of MCU platforms, including ARM Cortex-M series, Renesas RX series, NXP LPC, etc.
  • USB to TTL is a serial port to USB device, often used to communicate with microcontrollers, such as during debugging and programming.

Watch a video introduction: Is this the essential burning tool for STM32? Comparison of three entry-level programming tools for beginners, with usage tutorials! _bilibili_bilibili

This video introduces and compares three burning tools: stlink, jlink, and usb to ttl:

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-iYno6rab-1688999688885)(https://article.biliimg.com/bfs/article/88ab6d70e1c3a660b9e72b95be015a68aadf3126.png)] [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-aoV3Mtbp-1688999688886)(https://article.biliimg.com/bfs/article/dd48fe939bf9190587945799d9ad6b8eafb825f3.png)] [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-AVICGKmN-1688999688887)(https://article.biliimg.com/bfs/article/5630e5bd6d4bc9e84970e79d3291a410c7ebbd22.png)]

DAPLink is an open source debugging and programming interface that can be used for debugging and programming ARM Cortex-M microcontrollers. Here are some features of DAPLink:

  1. Open Source: DAPLink is an open source project and anyone can view and modify its code.
  2. Multi-platform support: DAPLink supports multiple operating systems such as Windows, Linux and MacOS.
  3. Multi-device support: DAPLink supports a variety of ARM Cortex-M microcontrollers, including STMicroelectronics, NXP, Cypress, Silicon Labs, etc.
  4. Fast download: DAPLink can quickly download programs to the microcontroller through the USB interface.
  5. Debugging function: DAPLink supports online debugging, and can perform single-step debugging, breakpoint debugging and other operations through the debugger and IDE.
  6. Easy to use: DAPLink is very simple to use, just connect it to your computer and microcontroller to start debugging and programming.

Read the comments:

Slow? The total download of the firmware is only a few kb, no matter how slow it is, not to mention stlink is not slow at all.

——You haven’t used the stm32f4 series yet. When you come into contact with real-time system programs, you will know how popular j-link is.

——This looks like I have never used stlink download for large projects, which is really terrible.

Novices generally need to burn a small number of programs, and they can use any one.

This, this, this, PWLINK doesn’t have it, CHlink doesn’t have it either?

——Both of them are essentially DAP.

Yes, the PWLINK2 Lite debugging downloader I purchased belongs to the DAPLink introduced above. Regarding the use of the PWLINK2 Lite debugging downloader , learn how to use it in the chapter "3. Starting from Scratch and Getting Started".


Reading a short story out of boredom: "Experience Narration of a Professional": STM32 downloader debugger CMSIS-DAP, DAPLink, USB to serial port download - Zhihu (zhihu.com)

I generally agree with what is said. The PWlink I bought also uses the DAPLink method for burning.


(2) Supplement: About serial port burning

The serial port burning program requires the installation of the CH340 driver and the purchase of a USB to TTL conversion tool (about 5 yuan), which costs less than an emulator. When burning, you need to connect the USB to TTL tool to the microcontroller.

Instructional video:

A nanny-level tutorial to use MCU to program stm32c8t6 minimum system board_bilibili_bilibili

How to program stm32 through the serial port_bilibili_bilibili (USB to TTL and STM32F103C8T6 only connect two wires (TXD, RXD), VCC and GND are not connected, and the USB to Micro data cable (which is the data cable for mobile phone charging) Powering STM32F103C8T6)

Teaching documents: (113 messages) Introduction to the three methods of STM32 chip burning, serial port, STM32 ST-LINK Utility and STM32CubeProgrammer_stm32 program burning_Blog that regained ten years of dreams-CSDN blog (only see "1.2 STM32 Serial Port Burning" "Method" can be used)

The USB to TTL connection method with STM32F103C8T6 is shown in the table below:

USB to TTL STM32F103C8T6
3V3 3V3
GND GND
TXD PA10
RXD PA9
Set BOOT1 to 0 and BOOT0 to 1

Actual picture of serial port burning:

image-20230601163552402

Search Taobao for "CH340" to view the USB to TTL conversion tool:

image-20230601161839027

Using the PWLINK2 Lite debug downloader to burn code belongs to the second method: downloading based on the emulator. In this project, you need to buy an emulator, because not only the burning program, but also the emulator is needed for debugging when testing the Bluetooth module.

Is it okay not to buy it? What about other ones?

Can. You can also buy STLINK, but this document only introduces the DAPlink burning method. The STLINK burning method is very common, and there is a lot of information. Readers can check the information online to learn how to use it.

At this point, the learning part of "PWLINK2 Lite Debug Downloader" is completed.

9). MB-102 breadboard

The use of the breadboard is very simple, and it is all explained in one picture: [External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-VWnn9Hpc-1688999686653) (https://article .biliimg.com/bfs/article/9caf6fdf5d23abc387fd9ec7f5bf3426f5b0cf73.png)]

如果非要觉得有必要再看一个教学视频的话,给出一个:你不一定知道的面包板使用方法_哔哩哔哩_bilibili

至此”MB-102面包板“部分的学习搞定。

10). 发光二极管(5mm,50只)

注意,长的那根是正极,短的那根是负极。可以用3.3v给小灯供电,千万不能用5v直接给小灯供电。

二、软件安装与学习

1. Keil uvision5软件安装及开发环境配置

我的Keil uvision5是在大三上学的一门课《MCS-51单片机》时安装的,当时只安装了51单片机相关的芯片环境,没安装MDK-ARM系列的。

img

keil C51的介绍:(114条消息) Keil(C51)介绍、下载、安装与注册_keil c51_strongerHuang的博客-CSDN博客

keil MDK的介绍:(114条消息) Keil uVision5 MDK(ARM)软件的介绍、下载、安装与注册_keil uvision5官网_Bornpride的博客-CSDN博客

现在想用keil编写STM32的程序,就需要安装MDK-ARM模块。我安装时参考的:[2-1] 软件安装_哔哩哔哩_bilibili

如果你啥也没有,之前也没有安装过Keil uvision5,可以参考这个视频去安装:2023年最新keil5 安装 升级 C51 V9.61 51单片机 最新更新 保姆级安装教程_哔哩哔哩_bilibili。视频里涉及的安装包可以下载**《3. 各元件的资料获取与学习.资料连接》**里的”Keil C51V961_MDK538a软件安装“。

如果你不知道自己的Keil uvision5上有没有安装MDK-ARM,可以查看一下。

keil uvision5查看是否安装keil MDK(用于编写stm32的开发板)?

答:按照下图查看,如果许可证里有MDK-ARM的,就算安装了。

image-20230602163608049

2. Keil uvision5软件使用

keil官方的一些使用手册文档:ARM Product Manuals (keil.com)

在上面的链接里,可以了解到软件的图形用户界面如何使用,各个按钮的功能,例如如何debug,如何软件仿真,如何编译程序,烧录程序。官方的说明文档系统全面,但可能对于一些问题说明地不够细致,可以再去百度或csdn上搜搜问题,搭配着学习。

如工程配置页面的说明:(113条消息) Keil-5_使用手册_keil5使用手册_零号0的博客-CSDN博客

keil编写代码时的uint8_t / uint16_t / uint32_t /uint64_t 这些数据类型是什么?

(118条消息) uint8_t / uint16_t / uint32_t /uint64_t 这些数据类型是什么?_一剃解千愁的博客-CSDN博客

keil中如何Debug调试

视频教程:(时间:2:57~5:17)STM32必备烧录工具居然是它?三款新手入门烧录工具对比,附使用教程!_哔哩哔哩_bilibili

3. STM32CubeMX软件安装及固件库下载

STM32CubeMX软件安装及固件库下载可以参考正点原子的“STM32F103 MINI开发指南V1.3.pdf”这个文件,在第十章

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hDbysPcq-1688999686656)(https://article.biliimg.com/bfs/article/f5d6f4dd72df02cd84d5e564e2c5db42aa6f12c8.png)]

下面的内容是参考的网上的资料,结合自己的实践经验做出的总结。网上的资料好多也是参考的正点原子的,所以根据自己的喜好选择跟着那个教学资料进行安装。

别着急,在安装之前,我们先简单了解一下,STM32CubeMX这个软件的安装路径我们可以修改,一般修改为D盘,使用软件进行不同单片机型号的开发,我们需要针对性地安装不同固件库,固件库系统默认安装路径是:“C:\Users\‘用户名’\STM32Cube\Repository”,如果我们不修改,随着使用次数越来越多,固件包下载地越来越多,可能很占用c盘空间,所以还是改一下固件库的安装路径吧。

参考下面这个安装教程进行安装,在下载固件库之前一定要将固件库的保存目录做一下修改,修改为D盘你知道的一个全英文路径(空格啥的也别带),最好是STM32CubeMax安装路径,我的在“D:\ProgramFiles\STM32CUBEMAX\Repository”。下图为固件库路径下的固件库。

image-20230602200717045

修改固件库路径的教程:(114条消息) stm32cubeIDE与stm32cubeMX库文件安装路径自定义设置_cubemx库地址_s2018307的博客-CSDN博客 (这个文章咱看一下,有个印象,在下面这个软件安装教程里,记得在下载固件库之前更改软件默认的固件库路径就行)

STM32CubeMax下载安装教程:(114条消息) (实测可用)STM32 CubeMx安装教程_stm32cubemx_智能小屋ZYXC的博客-CSDN博客

如果不确定自己的电脑有没有装过java环境,可以看看这篇文章里的如何检查是否安装了java环境(即JRE):JAVA环境_如何检查是否安装了java环境(即JRE) (cha138.com)

上面的安装教程里在“3、安装STM32 HAL库”部分只讲解了在线安装的方法,如果在线安装失败可以试试离线安装,离线安装可参考:(114条消息) STM32CubeMX——固件库下载以及安装_stm32固件库下载及安装_꧁花念ᝰ的博客-CSDN博客

我采取的是上面这个链接介绍的离线安装的方式,首先将固件库的路径改为D盘全英文,去官网下载STM32CubeF1的固件库(如果官网不好下,也可以在**《3. 各元件的资料获取与学习.资料连接》**里下载“STM32Cube_FW_F1_V1.8.4.zip”),我将下载好的固件库解压后放在“D:\ProgramFiles\STM32CUBEMAX\Repository”,然后在软件里操作从本地导入固件库。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wwgfDt8Q-1688999686657)(https://article.biliimg.com/bfs/article/05172b0f9cf31767bda890a278471c05a767cb76.png)]

上图为解压的固件库文件夹放在设置的固件库的目录,然后进入STM32CubeMax,点击“Help”,再选择“Manage embedded software packages”,点击”Refresh“。

image-20230602201437552

点击refresh,就会识别我们放在固件库目录下的固件包,

image-20230602195154589

4. STM32CubeMX软件使用

学习正点原子资料的“STM32F103 MINI开发指南V1.3.pdf”这个文件的第十章内容。

三、从零开始,上手操作

前言

学习资料哪里找?

第一:关于硬件的资料去淘宝找。一般商品介绍会给一些资料,如果给的网盘形式,但网盘链接是图片无法复制,就去联系客服要,不用管是不是买他家的商品。

第二:去b站。b站上大都是视频介绍,对于小白最友好。但不要过分依赖视频,跟着视频做是傻瓜式操作,对于刚起步的新手很有帮助,但也要去了解为什么要这样操作,以便出现问题自己也能找到解决的办法。

第三:去SCDN、博客园、百度等网站。这些网站搜索的内容很多也很杂,学习时做好甄别,取其精华,去其糟粕。

第四:去官网。像keil软件使用,Maix Dock使用等,官网的资料最全,也最权威,网上讲解的那些好多的源头不都是来自官网。

查看官网说明:µVision User’s Guide (arm.com)

第一阶段:小车底座的购买和搭建

这一阶段主要讲小车底座的购买和搭建。

先在淘宝上买个小车底座,顺便再把杜邦线也买了,因为在小车组装过程中需要对tt直流电机进行焊接连线,这个铜线基于可以使用杜邦线。用于焊接电机的铜线两端,一段是铜丝,就是用于焊接在电机上,另一端是杜邦线的公头或者铜丝,这一段主要用于连接L298N电机驱动。

按照一.2.1).双层小车底盘的讲解购买小车底座;按照一.2.10).公对公、母对母、公对母杜邦线,长20cm各一排的讲解购买杜邦线;按照一.3.1).双层小车底盘的讲解焊接并搭建小车底座;

至此第一阶段的内容告一段落,准备下一阶段的学习。

第二阶段:电机驱动L298N的购买与学习使用

上一阶段讲了小车底座的购买和搭建,这一阶段主要讲电机驱动L298N的购买与学习使用。

按照一.2.2).L298N芯片 电机驱动模块的讲解购买电机驱动,为节约成本,买一个驱动就够了(原因在一.2.1).双层小车底盘部分讲了)。在等快递的时候,我们学习一些关于这款电机驱动的知识。

用一个电机驱动四个电机的方法参考这个视频(不看也行):简易版智能小车《蓝牙版》,Arduino UNO + L298N电机驱动板 + 直流电机x4_哔哩哔哩_bilibili

从这个视频主要学到的就是电机的接线方法,包括将线焊接在电机上,将线连接在电机驱动上。

再看个L298N的介绍视频:简易版智能小车《蓝牙版》,Arduino UNO + L298N电机驱动板 + 直流电机x4_哔哩哔哩_bilibili

这个视频很好地讲解了L298N如何使用,最后还讲了PWM调速的原理

看完介绍视频后再回顾学习一.3.2).L298N芯片 电机驱动模块,了解那里讲的供电方式和PWM相关知识。

电机驱动使用注意事项

  1. L298N供电的5V如果是用另外电源供电的话,(即不是和单片机的电源共用,如使用锂电池给L298N供电),那么需要将单片机的GND和L298N模块上的GND连接在起,只有这样单片机上过来的逻辑信号才有个参考0点。板载5V稳压芯片的输入引脚和电机供电驱动接线端子导通的。

  2. l298n的板载5V稳压芯片的输入引脚和电机供电驱动接线端子导通的。这句话是什么意思?5V稳压芯片的输入引脚和电机驱动接线端子分别在l298N的哪个位置?

这句话的意思是,L298N电机驱动模块上的5V稳压芯片的输入引脚和电机供电驱动接线端子是连通的。也就是说,当5V稳压芯片输入电压时,电机供电驱动接线端子也会有电压输出。L298N电机驱动模块上的5V稳压芯片的输入引脚是VCC,一般位于模块的左上角。电机供电驱动接线端子则是OUT1、OUT2、OUT3和OUT4,分别对应4个电机驱动通道,一般位于模块的右侧。

L298N

  1. L298N上有三个跳线帽,分别为板核5V输出使能、A通道使能、B通道使能。板核5V输出使能是否应该拔掉应该看我们怎么给L298N供电,只有当供电电压高于12V时,板核5V输出使能跳线帽才拔掉。A通道使能和B通道使能的跳线帽都是使用到PWM调速时才拔掉,然后接单片机的PWM输出信号。

开始动手了

DC motor in series

按照这张图的方法将电机的铜线接到L298N上,例如由下面的两个电机引出的有2根线,L298N对应的下面有OUT1和OUT2,至于两根线哪根接OUT1哪根接OUT2一开始都行,两种接法的区别就是电机转动的方向不一样,假如通电后电机转动的方向与设想的相反,就将两根线对调一下就可以了。

现在还不能让电机转起来,因为L298N的IN1、IN2、IN3、IN4需要单片机提供高低电平,所以等我们的STM32F103C8T6最小系统板到手之后再着手让电机转动起来。

至此第二阶段的内容告一段落,准备下一阶段的学习。

第三阶段:STM32F103C8T6最小系统板的购买与学习使用

上一阶段讲了电机驱动L298N的购买与学习使用,这一阶段主要讲STM32F103C8T6最小系统板的购买与学习使用。

按照“一.2.4).STM32F103C8T6最小系统板”的讲解购买,再按照“一.2.9). MB-102面包板”的演示买个面包板。

在等快递的时候,先按照“二、软件安装与学习”部分,安装好Keil uvision5和STM32CubeMX。

下面我们通过编写程序控制STM32F103C8T6上LED灯闪烁的实验来介绍一下,项目开发的大致流程。

查看一下STM32F103C8T6的原理图(在**其他资料**里的“毕业设计学习资料/电路原理图/stm32f103c8t6最小系统原理图原件/stm32f103c8t6.pdf”),STM32F103C8T6上有两个板载LED灯,红色为电源指示灯,通电即亮;绿色为测试灯,右端接3.3V,左端接芯片的PC13引脚,PC13在STM32F103C8T6最小系统板的实物上并没有引出这个引脚,所以需要在代码里设置该引脚的高低电平来达到控制小灯的亮灭。当PC13为高电平时,绿色灯灭,为低电平时,绿色灯亮。我们可以在代码中设置让PC13高一会,低一会,来查看绿色灯是否为灭一会,亮一会。

image-20230603164511139

1. STM32CubeMX中的配置

这里的配置请参考**正点原子资料**的“STM32F103 MINI开发指南V1.3.pdf”的“10.3.3 新建工程 ” 的1~6小节:

image-20230603175327894

根据资料的讲解,结合本项目做出如下说明:

  1. 在”图 10.3.3.4 选择具体的芯片型号“ 中,我们选择的芯片型号为“STM32F103C8”

    image-20230603161149150

  2. 在“2 HSE 和 LSE 时钟源设置 ”部分,我们需要查看STM32F103C8T6的HSE和LSE,在**其他资料**里的“毕业设计学习资料/STM32F103C8T6参考文档/STM32F103x8B数据手册(中文).pdf”查看。结果也是外部高速晶振和外部低速晶振分别是: 8MHZ 和 32.768KHZ。

    image-20230603162839783

  3. 在“3 时钟系统(时钟树)配置 ”这节,前面讲解的是手动计算的方法,为了简单我们也已只看该节的最后一段文字:

    image-20230603163330101

    按照这里的操作,直接在“HCLK(MHz)”位置输入72,然后按回车,就会自动进行配置。如果跳出弹窗点击ok就行。

  4. 在“4 GPIO 功能引脚配置”,文档里介绍的PA8 和 PD2 两个GPIO口,将PA8 设为“GPIO_Output”即GPIO输出口,在System Core 下的 GPIO 选项里配置PA8为推挽输出、上拉 、高速 、别名为 LED0。 我们配置STM32F103C8T6的板载绿色LED灯,将PC13设为 “GPIO_Output” 即GPIO输出口,在System Core 下的 GPIO 选项里配置PC13为 默认输出低电平(Low)、推挽输出(Output Push Pull)、不上下拉(No pull-up and no pull-down) 、低速(Low) 、别名为 LED0。

    image-20230603170703862

  5. 在“5 配置 Debug 选项 ”这节也有所不同,文档那里给出的是使用JTAG Debug,本项目使用Serial Wire Debug。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tuwUiyIN-1688999686662)(https://article.biliimg.com/bfs/article/135918bd83ed5424b9e1b9511cb232b3a8c0b6ea.png)]

    SWD是Serial Wire Debug的简称,翻译成中文是”串行线调试”。 SWD是ARM目前支持的两种调试端口之一,另一个调试端口叫做JTAG Debug Port,也就是我们常用的J-link上面的调试端口(JTAG模式下)。SWD是用于访问ARM调试接口的双线协议。它是ARM调试接口规范(ARM Debug Interface Architecture Specification)的一部分,是JTAG的替代品。SWD的物理层由两条线组成:SWDIO: 双向数据线;SWCLK: host驱动的时钟线

    image-20230603172057965

    所以,我们在文档里的“图 10.3.3.14 打开 Debug 选项”的第4个标号那里选择“Serial Wire” 。

    image-20230603172500673

  6. 在“6 生成工程源码 ”中看图“图 10.3.3.15 工程配置 ”,序号2我们选择自己的文件保存路径,序号3我们自己设置文件名,序号4右边的”Min Version”选项我选的是“V5”(因为我的keil MDK不是最新的版本,我选个保险点的V5,事实证明,这样选没事),其他的和文档里的一样配置。我给出我的配置页面:

    image-20230603174727774

    image-20230603174753539


错误解决

提醒我们它想用V1.8.5版本的固件包,但是我们没有,它问我们需要下载吗?

image-20230603180735288

这种情况下果断选择“No”,先来搞清楚一下,我们之前安装过固件包,如果你按照正点原子的开发指南的文档安装的STM32CubeMx的固件包,它安装的是V1.8.3版本;如果你按照我给的方法安装的STM32CubeMx的固件包,我安装的是V1.8.4版本。无论如何我们都安装过了,我们想要它使用我们安装过的固件包生成代码。所以就查看一下生成工程源码的配置界面有没有关于固件库版本的设置。

image-20230603181703585

在“Project”选项页面的下面可以看到上面这张图,默认勾选的“Use latest available version”。我们把勾去掉,选择我们安装过的固件库版本,如下图:

image-20230603181851368


至此在STM32CubeMx中的工作就做完了,我们页得到了生成项目基本框架。准备下一步在keil中编写代码。

2. Keil uvision5中的操作

在Keil中的操作请参考**正点原子**资料的“STM32F103 MINI开发指南V1.3.pdf”的“10.3.3 新建工程 ” 的第7小节:用户程序

大致说明的是根据前面在STM32CubeMx中的操作生成的项目文件,我们点击编译,正常情况下会显示编译通过,然后我们编写代码要在注释的 BEGIN 和END 之间编写代码,这样重新生成工程之后,这些代码会保留而不会被覆盖(我们可能在编写一部分代码之后重新到STM32CubeMx添加配置新的引脚,所以需要再次生成工程)。

代码我们不用抄文档里代码,我给出一段控制STM32F103C8T6板载LED绿色小灯的代码:

while (1)
{
    
    	
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);//PC13引脚电平翻转
    HAL_Delay(100);//延迟100ms
}

我们打开工程,来到”Application/User”目录下,打开“main.c”文件,将main函数中的while(1){}段更换为上面给出的代码,然后点击编译。

image-20230603182243309

烧录参数配置

这里的配置请参考**正点原子资料**的“STM32F103 MINI开发指南V1.3.pdf”的“8.1.4 魔术棒设置 ” 的1~6小节:

image-20230603195539990

根据资料的讲解,结合本项目做出如下说明:

文档里没讲“Device”、”User“、“ASM”、“Linker”该怎么设置,保持默认不修改就好。

在“Device”一栏主要是选择芯片,核实一下是不是STM32F103C8系列的芯片就行,如下图所示。

Device选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

image-20230603211306194

”User“一栏不用改。

User选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

ASM选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

Linker选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

  1. 在“设置 Target 选项卡”小节里,我项目的设置和文档一致,如下图:

    Target选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

    Xtal为外部晶振时钟设置,该项只影响仿真结果。On chip ROM设置为定义存放程序的flash的首地址及可用空间大小,当有IAP和APP时,两个的ROM的地址及空间要分开。RAM设置为定义程序所用的RAM的首地址及可用空间大小。

    image-20230603195845156

  2. 在“设置 Output 选项卡 ”小节里,我们可以不用修改,保持默认的就行,默认的设置如下图。

    Output 选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

    image-20230603200638494

    当我们通过STM32CubeMx生成项目开发的基本框架时,会在工程项目的MDK-ARM文件夹下建立一个与工程同名的文件夹用于存放编译产生的调试信息、hex文件、预览信息、封装库等。编译输出文件,如下图所示,至于“Select Folder for Objects”选项,默认情况下就是下图中的”test“文件夹(工程名不同,此问价夹名称不同)。文档里之所以提醒将“Output”和“Listing”两栏中的“Select Folder for Objects”选项选择output文件夹,是因为文档的“8.1.4 魔术棒设置 ”小节的讲解不是基于STM32CubeMx生成的基本框架代码讲解的,我们往前翻一番可以看出(“8.1.2 新建一个工程框架 ”),它的项目基本框架都是在keil中新建生成,与我们使用的STM32CubeMx生成的基本框架有所区别。

    总结一下:本项目的“Output”和“Listing”两栏使用默认的就好,不用修改里面的参数。

    image-20230603201055946

  3. 在“设置 Listing 选项卡 ”小节里,如果我们不点击“Select Folder for Listings”对输出路径进行设置,它的默认输出路径也是在output那里提到的test文件夹。所以这里我们不动就好。

    Listing 选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

    界面如下:

    image-20230603213302172

  4. 在“设置 C/C++选项卡 ”小节里,配置界面如下:

    image-20230604183321772

    “图 8.1.4.4 设置 C/C++选项卡 ”中序号2保持默认的就行;关于“图 8.1.4.4 设置 C/C++选项卡 ”中序号3优化等级的选择,可以看一下这篇介绍:(115条消息) 对 keil Mdk 优化等级的理解_keil优化等级_zhuimeng_ruili的博客-CSDN博客 文档里选择的是“Level 0(-O0)”,我选的是“<default>”,这两个都可以。序号5“设置头文件包含路径”,一开始我们不需要改动,保持默认的就行,如果后来我们为了实现某个功能,在项目中新建了一些“.c”和“.h”文件,可以把我们建立的“.h”文件的路径加入到这里。

    例如:为了实现小车运动的控制,我新建了两个文件(thread_run.c和thread_run.h),在main.c中需要包含“thread_run.h”这个头文件,

    image-20230604173527888

    如果直接在main.c中写#include “thread_run.h”,会报错:“fatal error:’thread_run.h’ file not found”。

    image-20230604173845449

    这时候我们就需要在”C/C++”那栏的“includes paths”里添加该头文件的所在文件夹的相对路径,下面是添加之前的样子,我们照着样子给加上。

    image-20230604173307735

    加上…/MDK-ARM,之后就看到报错消失了,编译也没报错。(找这么看在我们没添加这个路径之前给我们报错也可能说明我们新建的这俩文件thread_run.c和thread_run.h也许没放对地方,如果我们保存在原有的头文件包含路径里,也就不会报错)

    image-20230604174947420

    C/C++选项卡详细界面各项介绍请看官方说明:µVision User’s Guide (arm.com)

  5. 在“设置 Debug 选项卡 ”小节里,本项目和文档里的配置一致,配置结果如下:

    image-20230604183118948

    此选项卡界面被中间一条竖线一分为二,左边是软件调试,右边是硬件调试,两边参数选项种类相同。使用软件调试可以观看PWM输出波形,烧录程序时必须设置成硬件调试。

    我建议先看一下Debug 选项卡详细界面各项介绍请看官方说明:

    µVision User’s Guide (arm.com)

    µVision User’s Guide (arm.com)

    关于“选择用于调试目标硬件的驱动程序”及其有右边的“Setting”设置的介绍:CMSIS-DAP Debugger User’s Guide (arm.com)

    image-20230604192751355

    此选项卡最主要的配置就是硬件调试的“选择用于调试目标硬件的驱动程序”及其有右边的“Setting”设置。

    “一.3.8)PWLINK2 Lite 调试下载器“部分讲了stm32单片机常见的下载器有哪些,买的下载器不同,“选择用于调试目标硬件的驱动程序”那里选择也不同,我买的PWlink,使用的DAPLink的方式进行烧录,所以我选择“CMSIS-DAP Debugger”

    烧录器类型 选项 学习链接
    stlink ST-Link Debugger
    daplink CMSIS-DAP Debugger CMSIS-DAP Debugger User’s Guide (arm.com)
    jlink J-LINK/J-TRACE Cortex
    usb转ttl 不用设置,usb转ttl不使用keil烧录
  6. 在“设置 Utilities 选项卡 ”小节里,本项目和文档里的配置一致,配置结果如下:

    image-20230604193553961

    image-20230604193621739

补充:

Utilities选项卡里默认的选项如下图所示,文档中的配置相较于默认配置,只多勾了个“Reset and Run”,这个选项可以帮助我们每次点击烧录之后自动地“按”一下复位键,使板子中地程序重新启动。如果不勾选此项,我们每次烧录完需要手动按一下板子上地Reset按钮,板子才能正常工作。

image-20230604194751963

到这里恭喜你,你已经学习了很多知识,在STM32CubeMx和Keil中的配置基本完成,之后如果需要修改也只是微调,很多东西设置完一次后面就不用再改了。现在,我们已经使用STM32CubeMx生成了项目开发的基本框架,又到keil中编写了控制STM32F103C8T6最小系统板板载绿色LED每隔100ms闪烁一次的程序,在keil中的编译,调试,烧录选项也已经配置好了,接下来拿出我们的烧录器(我买的PWLINK Lite2),将烧录器一段与STM32F103C8T6最小系统板连接,一端与电脑连接,看一眼“Debug-Settings-SW Device”那里有没有检测到设备:

image-20230604200519135

如果有,我们先点击部分编译按钮,再点击烧录按钮,如下图。

image-20230604195716597

若烧录成功,你会看到绿色板载LED灯如期闪烁。

image-20230604200643144

补充:

如果烧录时报如下错,可以看一下这篇文章:(115条消息) STM32F103C8T6程序烧录方法_stm32f103c8t6烧录_流浪法师解剖鱼的博客-CSDN博客

image-20230604201134924

如果对于烧录时STM32F103C8T6最小系统板的两个BOOT引脚(boot0,boot1)的跳线帽如何放置还有疑问,

至此第三阶段的内容告一段落,准备下一阶段的学习。

第四阶段 让小车全速动起来

上一阶段讲了给STM32烧程序,控制小灯闪烁,这一阶段主要讲配置单片机的四个GPIO口,连接电机驱动和电机,调试一下电机转动。

1. 电机控制实现

如果给电机调速,使用四个GPIO口,连接L298N电机驱动的IN1,IN2,IN3,IN4四个接口,就可以驱动电机全速正反转。

image-20230607151710456

先来在STM32上选出4个GPIO口,参考“一.3.4).STM32F103C8T6最小系统板”的一张图:STM32F103C8T6引脚定义表

2. 电机控制在STM32CubeMx中的配置

打开工程目录下的“.ioc”文件

image-20230607153237072

做如下配置:

STM32上的GPIO口 功能 IO口输出方式
PA10 IN1 Output Push Pull(推挽输出)
PA9 IN2 Output Push Pull(推挽输出)
PB9 IN3 Output Push Pull(推挽输出)
PB6 IN4 Output Push Pull(推挽输出)

先在芯片图上选择PA10、PA9、PB9、PB6的GPIIO模式为输出模式:

image-20230607154730049

再来到“System Core-GPIO”,对PA10、PA9、PB9、PB6进行配置,默认情况下这些IO口都是初始输出低电平、推挽输出、不上拉不下拉、Maximum output speed:Low,我们只要给每个IO口设个User Lable就行。

image-20230607155231860

照着上图配置好之后,点击“GENERATE CODE”按钮生成代码,之后“Open Project”。

image-20230607155852159

3. 在Keil中编写控制小车运动的代码

打开之后我们可以看到上次编写的代码,注意:一定要将代码写在BEGIN 和END之间,不然下次我们点击“GENERATE CODE”按钮生成代码时,之前没有写在BEGIN 和END之间的代码会消失。

image-20230607160525580

在编写控制小车全速正反转的代码之前,先来认识一下HAL库开发的一些基础函数,参考(118条消息) STM32cubeide中文笔记_小 羊 君的博客-CSDN博客

//PA5设置高电平、低电平
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
//引脚别名为LED的引脚io口输出低电平
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
//延时200ms,单位ms
HAL_Delay(200);
//PA5引脚状态翻转:1变0,0变1
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
//读取PQ10口电平状态
HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10);

除此之外,如果想学习或查询全面的函数知识,ST 官方给我们提供了快速查找 API 函数的帮助文档。在路径:
“ STM32Cube_FW_F1_V1.8.4\Drivers\STM32F1xx_HAL_Driver” 下有几个 chm 格式的文档,如下图

image-20230607162641803

参考这两篇文章

(118条消息) STM32不同容量,标准库和HAL库对应的启动文件总结:_stm32小容量启动文件_小飞侠123!的博客-CSDN博客

(118条消息) STM32F103小容量、中容量和大容量单片机介绍_stm32f103r6单片机介绍_王小琪0712的博客-CSDN博客

得知STM32F103C8T6和STM32F103xB被归为中等容量产品,固件库里没有STM32F103x8的.chm文件,我们可以参考STM32F103xB的.chm文件,打开“STM32F103xB_User_Manual.chm”。

作为新手,我只是用到一些基本函数,有不理解的地方就到里面去搜函数名,看里面的介绍。

正点原子的开发指南也介绍了这部分如何使用,具体操作请参考**正点原子资料**的“STM32F103 MINI开发指南V1.3.pdf”的“7.3.2 HAL库文件介绍” 的第86页下方开始:

image-20230607165332485

这几个函数用的挺多的

image-20230607165733884

main.c中编写的控制小车运动的代码如下:

/* USER CODE BEGIN 0 */
void Forward()
	{
    
    
    	//左正转
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
    	//右正传
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
	}
	
void Backward()
	{
    
    
    	//左反转
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
    	//右反传
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
	}
void Leftward()
	{
    
    
    	//左反转
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
    	//右正传
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
	}
void Rightward()
	{
    
    
    	//左正转
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); 
    	//右反传
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
	}
/* USER CODE END 0 */

将上面我们自己编写的函数放在“USER CODE BEGIN 0”和”USER CODE END 0“之间

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

main函数中的While(1)循环更改为下面代码:

while(1)
{
    
    
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);//板载绿色LED小灯引脚电平反转
	Forward();
	HAL_Delay(1000);
	Leftward();
	HAL_Delay(1000);
	Rightward();
	HAL_Delay(1000);
	Backward();
	HAL_Delay(1000);	
}

然后点击部分编译,编译成功。

image-20230607172429690

接着准备烧录到STM32F103C8T6中,因为之前已经弄过使用PWlink烧录的参数配置,这次烧录直接用PWlink连接电脑和STM32F103C8T6之后,再编译一遍无误后就可以点击烧录。

image-20230607212945137

4. 实物连接并通电测试

烧录完开始进行实物的连接,之前已经给四个tt直流电机焊接好电线,并各引出两根引线接到L298N的OUT1、OUT2、OUT3、OUT4上,

电池采12V锂电池供电,L298N的GND引出两根线,一根与锂电池连接,一根接到面包板上与stm32f103c8t6的GND连接。L298N供电方式如下:

image-20230601205621618

实物图如下:

image-20230608172506521

至此第四阶段的内容告一段落,准备下一阶段的学习。


如果采用充电宝给L298N电机驱动供电,实物连接图如下:

STM32-L298N-充电宝-电机

我们可以发现,小车的车轮转动缓慢,感觉上这种速度只有使用12V锂电池供电时的30%(目测),这还是小车上没加啥东西的情况,如果到了后期,随着零件的增多,小车的重量也将增大,运动时所受摩擦力也增大,到时候可能会卡住不动,所以我经实验建议使用额外的12V锂电池供电。

实物图如下:

image-20230607231936320


第五阶段 PWM给小车调速

上一阶段讲了如何使小车全速地正转、反转、左转、右转,但没办法调速,这一阶段主要讲如何使用PWM给小车调速。

1. PWM常见输出方式

1.新手(菜鸟)级别while循环中,阻塞延时,控制IO口高低输出:

image-20230608221455731

阻塞延时可以是:软件模拟延时,定时器阻塞延时等。

2.入门(初级)级别while循环中,非阻赛延时,控制IO口高低输出:

image-20230608221526776

非阻赛延时可以是:定时器标识检测、RTOS(系统)延时等。

3.熟悉(中级)级别定时器中断控制IO高低电平输出:

定时器中断配置 ——> 启动定时器 ——> 响应中断,控制IO高低电平···

4.熟练(中级+)级别

定时器PWM硬件控制输出:

配置PWM对应的IO,以及定时器PWM输出 ——> 启动PWM自动输出···

image-20230608221546937

比较:
上面几种PWM输出方式,前面三种都会CPU干预PWM的输出,也就是会占用CPU资源,特别是前面两种方式,不仅占用CPU,误差还比较大。

使用第三种中断方式,如果频率比较高,CPU消耗的也比较严重。这种情况适合于没有硬件PWM输出的单片机。

第四种就是单片机自带硬件PWM输出功能,只需要简单配置就可以自动输出PWM波形,无需CPU干预。

2. PWM实现原理

显然,我们就学PWM控制转速,先来了解一下PWM原理知识:

提问:pwm用timx实现的原理?

PWM(Pulse Width Modulation)是一种常用的模拟信号生成技术,可以通过改变信号的占空比来控制输出信号的电平。在STM32中,可以使用TIM(定时器)模块来实现PWM信号的生成。

具体实现原理如下:

  1. 配置TIMx的时钟源和分频系数,使其产生一个固定的计数频率。

  2. 配置TIMx的计数模式,使其以向上计数模式计数。

  3. 配置TIMx的自动重载寄存器ARR,使其等于计数器的最大值,即计数器溢出时自动重置。

  4. 配置TIMx的比较寄存器CCR,使其等于占空比所对应的计数值。

  5. 配置TIMx的输出比较模式,使其在计数器计数到CCR时输出高电平,在计数器溢出时输出低电平。

  6. 启动TIMx计数器,即可生成PWM信号。

需要注意的是,TIMx的时钟源和分频系数、ARR和CCR的值的设置需要根据具体的应用场景进行调整,以满足不同的需求。

补充:

  1. TIMx的时钟源(可以查看“STM32F0xxx参考手册(中文).pdf”)image-20230609210541475

  2. TIMx的自动重载寄存器ARR是一个16位的寄存器,用于存储TIMx计数器的自动重载值。当TIMx计数器计数达到ARR的值时,计数器会自动重置为0,并产生一个更新事件。这个寄存器的值可以通过编程的方式进行设置,以控制TIMx的计数范围和计数周期。在计数器向上计数模式下,ARR的值决定了计数器从0开始计数到最大值的范围;在计数器向下计数模式下,ARR的值决定了计数器从最大值开始计数到0的范围。需要注意的是,ARR的值不能超过TIMx计数器的最大值,否则会导致计数器溢出。

  3. TIMx的比较寄存器CCR(Capture/Compare Register)在PWM信号输出中的作用是设置PWM波形的占空比。占空比是指PWM信号中高电平所占的时间比例,通常用百分比表示。CCR寄存器的值决定了PWM波形的占空比,具体计算公式为:

占空比 = (CCR值 / ARR值)* 100%

其中,ARR是自动重载寄存器的值,决定了PWM波形的周期。CCR的值可以通过软件编程或者硬件捕获来设置,从而实现不同的占空比。通过改变CCR的值,可以改变PWM信号的占空比,从而控制输出的电平。

  1. img

PWM的一个周期

  定时器从0开始向上计数
  当0-t1段,定时器计数器TIMx_CNT值小于CCRx值,输出低电平
  t1-t2段,定时器计数器TIMx_CNT值大于CCRx值,输出高电平
  当TIMx_CNT值达到ARR时,定时器溢出,重新向上计数...循环此过程
  至此一个PWM周期完成

每个定时器有四个通道,每一个通道都有一个捕获比较寄存器,

将寄存器值和计数器值比较,通过比较结果输出高低电平,便可以实现脉冲宽度调制模式(PWM信号)

TIMx_ARR寄存器确定PWM频率,周期是频率倒数,也可以说TIMx_ARR寄存器确定PWM信号周期

TIMx_CCRx寄存器确定占空比

因此想要得到PWM输出信号,只要一个TIMx的一个通道进行一些配置即可实现。简单来说,需要我们确定三个参数,第一个是用于计数的时钟源,随着时钟源波形信号的不断输出,TIMx_ARR寄存器的值不断增加(或减少,减少时为向下计数);第二个参数是TIMx_ARR寄存器的最大值,此参数控制着输出的PWM信号的频率;第三个参数是TIMx_CCRx寄存器的值,这个值就像一条分割线,高于此值时PWM信号输出高电平,低于此值的时候PWM输出低电平,以此来控制PWM信号的占空比。通过这三个参数,可以确定PWM信号的频率(周期也定了)、占空比。

3. PWM信号在STM32CubeMx中的配置

本小节我们讲解时以TIM1的Channel4这个PA11为例。当使用PWM信号控制小车调速时需要再加上一个PWM信号,在“6. 通过PWM信号实现小车调速”小节给出配置的页面。

选择TIM1的时钟源为“Internal Clock”,选择TIM1的Channel4为“PWM Generation CH4”,(TIM1的Channel4是引脚PA11的复用功能,对他操作就是对PA11操作)如下图:

image-20230609213225346

配置好PWM输出引脚后和时钟源后,还要设置STM32CubeMx中的三个参数来控制PWM信号的频率和占空比

在Parameter Settings中设置好Prescaler(预分频系数)、Counter Period(重装载值)、Pulse(脉冲时长)

PWM信号产生中的Prescaler(预分频系数)是指将输入时钟分频的系数,用于控制PWM信号的频率,分频的意思就是将时钟源的频率等分为多少份。

Counter Period(重装载值)是指计数器的最大值,当计数器计数达到该值时,计数器会自动清零并产生一个PWM周期,对应着上面讲到的TIMx_ARR寄存器的最大值。

Pulse(脉冲时长)是指PWM信号的高电平时间,也就是占空比的一部分,对应着上面讲到的的TIMx_CCRx寄存器的值。通过调整Prescaler、Counter Period和Pulse等参数,可以实现不同频率和占空比的PWM信号输出。

疑问解答:

  1. 什么是分频,为什么要分频?

    PWM信号产生中的分频是指将系统时钟或者其他高频信号分频后得到一个低频信号,用于控制PWM信号的频率。在PWM信号产生中,通常需要将高频信号分频后得到一个低频信号,然后再将低频信号与占空比控制信号进行比较,从而产生PWM信号。分频的大小决定了PWM信号的频率,一般情况下,分频系数越大,PWM信号的频率越低。

    PWM信号产生中的分频是指将输入的时钟信号分频后再用于PWM信号的产生。分频的目的是为了降低PWM信号的频率,以适应不同的应用场景。对于一些应用场景,需要较低的PWM频率,比如电机控制、LED调光等,而一些应用场景需要较高的PWM频率,比如DC-DC变换器、无线充电等。因此,通过分频可以将输入的时钟信号降低频率,以适应不同的应用场景。

  2. 电机控制为什么要较低的PWM频率?

    在电机控制中,较低的PWM频率可以减少电机的噪声和振动,同时也可以降低电机的损耗和温升。此外,较低的PWM频率还可以提高电机控制的精度和稳定性,使得电机的转速和转矩更加准确和可控。因此,较低的PWM频率在电机控制中是非常重要的。

STM32CubeMx中可以这样设置:psc=1440-1,arr=100-1,Pulse=50,这样设置的效果是PWM信号的频率为500Hz,周期为2ms。

image-20230611220206129

上面的设置参考这个视频:5-PWM输出和MDK如何仿真_哔哩哔哩_bilibili

4. 从公式的角度来理解PWM

符号说明:

符号 含义
Tout PWM周期。定时器溢出时间(单位为s)
Fpwm PWM频率。PWM周期的倒数(单位为Hz)
Tclk Tclk是定时器时钟源的时钟频率(单位为Hz)(未被分频的原始时钟频率)
arr 全拼:Counter Period (AutoReload Register),(重装载值)是指计数器的最大值,TIMx_ARR寄存器的最大值
psc 全拼:Prescaler,预分频系数
Tclk Tclk是定时器时钟源的时钟频率(单位为Hz)(未被分频的原始时钟频率)

有的人喜欢将PSC设置为主频-1(例如主频为72MHz),这样分频后频率为1MHz,一个周期刚好是1us,然后将ARR设置为1000-1 , 这样每个周期就是1ms,频率也就为1KHz。而Pulse设置为占空比,设置为500,刚好为1000的一半,则占空比为50%。(这句话相信看了下面的公式会很好理解)

计算公式:

PWM周期:Tout = ( (arr+1) * (psc+1) ) / Tclk (单位:s)

PWM频率:Fpwm = 1 / Tout = Tclk / ( (arr+1) * (psc+1) ) (单位:Hz)

比如 时钟源频率Tclk = 72Mhz ,arr=99 ,psc=1439 ,那么PWM频率就是72000000/100/1440= 500Hz,即Fpwm = 500Hz,Tout = 1 / 500 = 2ms。

占空比:

PWM信号的占空比是指在一个周期内,高电平所占的时间与周期时间的比值。通常用百分比表示,例如50%的占空比表示高电平持续时间占周期时间的50%。

模式 描述
PWM模式1 向上计数:CNT<CCR时,REF置有效电平,CNT≥CCR时,REF置无效电平 向下计数:CNT>CCR时,REF置无效电平,CNT≤CCR时,REF置有效电平
PWM模式2 向上计数:CNT<CCR时,REF置无效电平,CNT≥CCR时,REF置有效电平 向下计数:CNT>CCR时,REF置有效电平,CNT≤CCR时,REF置无效电平
在STM32CubeMx中的参数说明:

Counter Mode 这个参数影响CNT的计数方式。
PWM Mode 这个参数影响比较CNT和Pulse的比较方式。mode=1时,当CNT < Pulse时,比较结果为1、反之为0,mode=2时差不多就是比较结果反一反;
CH Polarity 这个参数影响信号输出电平。设置为High时,当比较结果为1时输出高电平,结果为0时输出低电平;设置为Low时则输出电平反一反;

Counter Mode PWM Mode CH Polarity
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m2i7fuGi-1688999688888)(https://article.biliimg.com/bfs/article/233eafbd4383ad1a3b65c00a285e534c9184204b.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jjsHzy4M-1688999688888)(https://article.biliimg.com/bfs/article/2c6fe2a1ba91928b0c97bb934a71de9d9b434a4c.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0izZJ35Z-1688999688889)(https://article.biliimg.com/bfs/article/26e21d63a8ce958ec6515a4784f0976adf9678e9.png)]

表中图片均为默认设置。CH Polarity一般保持默认为High,无需改动。若在STM32CubeMx中不对“Counter Mode”、“PWM Mode”、“CH Polarity”三个参数进行改动,占空比计算公式默认为:占空比 = CCR / (ARR+1) (单位:%)

占空比计算公式:
  1. 占空比 = CCR / (ARR+1) (单位:%)(推荐使用)

    Counter Mode为向上计数(计数值不断增大),PWM Mode为PWM mode 1,CNT<CCR时,REF置有效电平,CNT≥CCR时,REF置无效电平。如下图:

    image-20230612000113507

    例如:ARR=499,CCR=100 则pwm的占空比为20% 。

  2. 占空比 = ((ARR+1) - CCR) / (ARR+1) (单位:%)

    Counter Mode为向上计数(计数值不断增大),PWM Mode为PWM mode 2,CNT<CCR时,REF置无效电平,CNT≥CCR时,REF置有效电平。如下图:

    img

    例如:ARR=499,CCR=100 则pwm的占空比为80% 。

改CCR可以修改占空比,修改ARR可以修改频率。


补充:

TIM1和TIM2的“Clock Source”有两种选择,“Internal Clock”和“ETR2”,如下图

image-20230609202808286

TIM3和TIM4的时钟源只有一种选择:“Internal Clock”,如下图

image-20230609202953837

13.3.4 时钟选择

image-20230609210950994

(123条消息) STM32的ETR引脚计数功能_DingUXiu的博客-CSDN博客


5. 通过PWM信号实现呼吸灯小实验

经过前面“3. PWM信号在STM32CubeMx中的配置”的讲解,跟着配置TIM1的Channel4即PA11,那节里所作的操作就是:

选择TIM1的时钟源为“Internal Clock”,

选择TIM1的Channel4为“PWM Generation CH4”,

Prescaler、Counter Period和Pulse等参数设置为:psc=1440-1,arr=100-1,Pulse=50。

这样PA11就会输出率为500Hz,周期为2ms的PWM信号。我们据通过这个PWM信号来完成呼吸灯的小实验。

在STM32CubeMx中配置好后,点击生成代码。

在完成以上设置之后,我们需要使能 TIM1 并且使能 TIM1_CH4输出。在 HAL 库中,函数 HAL_TIM_PWM_Start 可以用来实现这两个功能,函数声明如下:

  /* USER CODE BEGIN 2 */
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);	//开启定时器1的通道4的PWM输出
  /* USER CODE END 2 */

这个函数放在TIM1初始化函数之后就行,如下图:

image-20230612005613417

main.c中代码编写:
	/* USER CODE BEGIN WHILE */
	static int a=0;
	while(1)
	{
    
    
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);//板载绿色LED小灯引脚电平反转
		HAL_Delay(100);	
		while(a<99){
    
    
			__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,a++);//设置占空比
			HAL_Delay(10);
		}
		while(a){
    
    
			__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,a--);//设置占空比
			HAL_Delay(10);
		}		
	}
认识使用__HAL_TIM_SET_COMPARE()函数
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,a++);//设置占空比

代码里的这个函数的功能是设置CCR的值,以此来改变PWM信号占空比。我们在STM32CubeMx中设置使用默认的占空比计算公式:占空比 = CCR / (ARR+1) (单位:%),所以CCR的值越大,占空比越大,CCR最小为0,最大为ARR+1 。经过我的实验,发现当CCR大于ARR+1时,无论大多少,从电机转动的效果来看,都是100%的占空比输出。

image-20230612125139621

代码编写完成之后,点击编译,烧录,然后连接实物,将PA11接到LED小灯的正极,LED的负极接STM32F103C8T6的GND,最后就能看到渐亮渐灭的呼吸灯了。

video_20230611_22365 -small-original

6. keil中软件仿真查看PWM波形是输出

1. 首先设置好仿真环境

之前硬件调试时在“Debug”一栏的设置我们做一个备份,

Dialog DLL Parameter
DCM.DLL -pCM3

如下图:

image-20230613173127681

现在配置软件仿真的环境,参数设置如下:

Dialog DLL Parameter
DARMSTM.DLL -pSTM32F103C8

软件中配置如下图所示:

image-20230613174126247

2. 点击debug按钮,进入debug后按run或者F5

image-20230613174613296

再点击一下会回到代码编写的窗口。

3. VIEW->Analysis Windows->Logica Analyzer打开逻辑分析仪

image-20230613174814809

4. 添加要观察的PWM信号变量

image-20230613175859852

我们在代码中设置的两个PWM信号为PA6和PA11,在新建变量时输入“PORTA.6”后点击回车和输入“PORTA.11”后点击回车.如下图:

image-20230613175705486

5. 点击全速运行,观察示波器

image-20230613182428190

波形输出:

image-20230613182135747

我们在软件中设置的PA6和PA11的占空比一开始都是50%,然后通过While()循环,不断对占空比从0开始增大,到达最大值后再一点点减小,因此,我们在波形输出中可以看到一开始的波形的占空比为50%,接着占空比从0开始不断增大,然后不断减小,再不断增大,再。。。重复此过程。

7. 通过PWM信号实现小车调速

关于小车调速的PWM信号的频率设为多少让人犯难,网上众说纷纭,有的说要“大于1KHz,才能稳定地控制电机”,有的说“一般是10~20khz,如果电机电气时间常数小于0.1ms,要考虑用更高的频率”,有的说“据说在1khz之内就好,否则芯片会发热”,有的说[“频率越高驱动就越耗电,自然器件也就越热,频率高对主滤波储能电容的高频性能要求也越高,所以我觉得15K~25K就好”](用pwm控制直流电机速度,多大频率合适 (amobbs.com 阿莫电子论坛 - 东莞阿莫电子网站)),所以我打算自己实验一下,看看那种频率合适。

测试数据(占空比:50%):

psc,arr,Pulse设置 PWM频率(kHz) 效果 点击是否正常启动
psc=1440-1,arr=100-1,Pulse=50 0.5 良好
psc=72-1,arr=2000-1,Pulse=1000 0.5 良好
psc=72-1,arr=1000-1,Pulse=500 1 良好
psc=72-1,arr=666-1,Pulse=333 1.5 良好
psc=72-1,arr=500-1,Pulse=250 2 良好
psc=72-1,arr=200-1,Pulse=100 5 不适合
psc=72-1,arr=100-1,Pulse=50 10 不适合
psc=72-1,arr=66-1,Pulse=33 15 不适合
psc=72-1,arr=50-1,Pulse=25 20 不适合

说明:Pulse就是CCR,Tclk是72MHz,测试时占空比都为50%,Fpwm =Tclk / ( (arr+1) * (psc+1) ) ,改CCR可以修改占空比,修改ARR可以修改频率

效果动图如下所示:

22226月12日-PWM各频率电 -middle-original

经过上表的测试,控制电机的PWM信号频率可以控制在2kHz以内为好。后来又测试了10%占空比的数据,此时电机都无法正常转动,原因可能和电机有关,电机的设计存在缺陷,转动时需要克服不小的摩擦力后才开始转动,所以在占空比为10%时,电机原理上本应该转动的却没有转动。

测试数据(占空比:10%):

psc,arr,Pulse设置 PWM频率(kHz) 效果 点击是否正常启动
psc=1440-1,arr=100-1,Pulse=10 0.5
psc=72-1,arr=2000-1,Pulse=200 0.5
psc=72-1,arr=1000-1,Pulse=100 1
psc=72-1,arr=666-1,Pulse=66 1.5 不适合
psc=72-1,arr=500-1,Pulse=50 2 不适合

占空比为50%时,Fpwm=0.5、1、1.5、2时效果相差不大,都可以使用,我在代码中以psc=1440-1,arr=100-1,Pulse=50,Fpwm=0.5为例进行设计。

控制小车左右两侧的电机转动需要两个PWM信号,所以除PA11之外,我们再增加一个引脚:PA6(TIM3_CH1)

在STM32CubeMx中配置PA6:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uKny7e6o-1688999688889)(https://article.biliimg.com/bfs/article/675b1046743e82e63a008534513ff0a1888c3cb0.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-epYg6HE6-1688999688890)(https://article.biliimg.com/bfs/article/12914ea0bba6a8c4611df90ae1ef02a4579651bd.png)]

之后生成代码,在完成以上设置之后,我们需要在keil中使能 TIM1_CH4输出和TIM3_CH1输出。在 HAL 库中,函数 HAL_TIM_PWM_Start 可以用来实现这两个功能,函数声明如下:

  /* USER CODE BEGIN 2 */
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);	//开启定时器1的通道4的PWM输出
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);	//开启定时器3的通道1的PWM输出
  /* USER CODE END 2 */

这个函数放在TIM1初始化函数之后就行,如下图:

image-20230612212500421

main.c中代码编写:
	/* USER CODE BEGIN WHILE */
	static int a=0;
	//小车前进初始化
	Forward();
	while(1)
	{
    
    
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);//板载绿色LED小灯引脚电平反转
		HAL_Delay(100);
		while(a<99){
    
    
			__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,a++);//设置占空比
			__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,a++);//设置占空比
			HAL_Delay(10);
		}
		while(a){
    
    
			__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,a--);//设置占空比
			__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,a--);//设置占空比
			HAL_Delay(10);
		}		
	}

之后编译,烧录。L298N接线如下图所示:

image-20230613183256774

连接实物,通电运行。实物运转效果如下所示:

2222222222222222222 -middle-original

至此第五阶段的内容告一段落,准备下一阶段的学习。

第六阶段 变身蓝牙遥控小车

截至上一阶段,我们的项目已经可以正常全方位运动了,这一阶段主要讲如何给小车加上蓝牙模块,变身为蓝牙遥控小车,主要是HC-05蓝牙模块的学习。

1. 手机与电脑通过HC-05通信

在“一.3.6). HC-05 蓝牙模块”那里学习HC-05蓝牙模块的使用,跟着该模块“(1). 进入AT模式设置波特率”的内容修改HC-05蓝牙模块的波特率。

设置完通信的比特率为115200后,断开连接,重新上电,再次上电时不用长按按钮,不过使用pwlink连接时,每次都要配置pwlink的VDD引脚为5v。再次上电后,HC-05小灯一直快闪。

电脑端操作:

打开电脑端串口调试助手,进行连接。在电脑端串口调试助手中需要做如下设置:选择串口号、修改波特率为115200、停止位为1、数据位为8、校验位为None,然后点击“打开串口”,

手机端操作:

手机那头也打开蓝牙调试器“xnj.lazydog.btcontroller.apk”(在**资料链接**里下载),连接HC-05,之后就可以通信了。

关于蓝牙调试器“xnj.lazydog.btcontroller.apk”的更多操作请阅读:提高开发效率-蓝牙调试器 - 简书 (jianshu.com)

在手机端的APP的底部栏选项“设备连接”里连接HC-05,在“对话模式”里发送“t”,此时在电脑端的串口调试助手里可以看到手机发送的数据“t”。在电脑端的串口调试助手里填写字母“a”然后发送,此时在手机端也能收到电脑发送的信息。如此,完成了手机与电脑通过HC-05通信的小实验。数据发送如下图所示:

image-20230529200018760

2. 手机与STM32F103C8T6通过HC-05通信并控制小车运动

1). 原理说明

stm32f103c8t6接收HC-05的数据可以通过USART串口通信实现。下面是基于STM32F103C8T6的USART接收HC-05数据的步骤:

  1. 配置USART串口通信

首先需要配置USART串口通信,包括波特率、数据位、停止位、奇偶校验位等参数。可以使用STM32CubeMX工具进行配置,也可以手动编写代码进行配置。

  1. 使能USART接收中断

在USART初始化的代码中,需要使能USART接收中断。可以使用HAL库提供的函数HAL_UART_Receive_IT()来实现。

  1. 实现USART接收中断处理函数

当USART接收到数据时,会触发USART接收中断。此时需要在中断处理函数中读取接收到的数据。可以使用HAL库提供的函数HAL_UART_Receive_IT()来实现。

以下是一个简单的示例代码:

#include "stm32f1xx_hal.h"
UART_HandleTypeDef huart1;
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
     
     
    if (huart == &huart1) {
     
     
        // 读取接收到的数据
        uint8_t data = huart1.Instance->DR;
        // 处理接收到的数据
        // ...
        // 继续使能USART接收中断
        HAL_UART_Receive_IT(&huart1, &data, 1);
    }
}
int main(void)
{
     
     
    // 初始化USART串口通信
    // ...
    // 使能USART接收中断
    HAL_UART_Receive_IT(&huart1, &data, 1);
    while (1) {
     
     
        // 主循环
        // ...
    }
}

在上面的代码中,HAL_UART_RxCpltCallback()函数是USART接收中断处理函数,当USART接收到数据时会自动调用该函数。在该函数中,可以读取接收到的数据并进行处理。最后,需要再次调用HAL_UART_Receive_IT()函数来继续使能USART接收中断。

此外,正点原子的“STM32F103 MINI开发指南V1.3.pdf”这个文件,在“第十七章 串口通信实验”,读者可以自行阅读,弥补一些欠缺的知识。在这里,我摘抄一些易理解且对项目有帮助的知识点,其他的知识能看懂越多越好,不理解影响也不大:

  1. 数据通信方式

    按数据通信方式分类,可分为串行通信和并行通信两种。串行和并行的对比如下图所示:

    image-20230613201936429
    串行通信的基本特征是数据逐位顺序依次传输,优点是传输线少、 布线成本低、 灵活度高等优点,一般用于近距离人机交互, 特殊处理后也可以用于远距离, 缺点就是传输速率低。而并行通信是数据各位可以通过多条线同时传输,优点是传输速率高,缺点就是布线成本高,抗干扰能力差因而适用于短距离、高速率的通信。

  2. 数据传输方向
    根据数据传输方向,通信又可分为全双工、半双工和单工通信。全双工、半双工和单工通信的比较如下图所示:

    image-20230613202209488

    单工是指数据传输仅能沿一个方向,不能实现反方向传输,如校园广播。半双工是指数据传输可以沿着两个方向,但是需要分时进行, 如对讲机。全双工是指数据可以同时进行双向传输,日常的打电话属于这种情形。
    这里注意全双工和半双工通信的区别:半双工通信是共用一条线路实现双向通信,而全双工是利用两条线路,一条用于发送数据,另一条用于接收数据。

  3. 数据同步方式
    根据数据同步方式,通信又可分为同步通信和异步通信。同步通信和异步通信比较如下图所示:

    image-20230613202506011

    同步通信要求通信双方共用同一时钟信号,在总线上保持统一的时序和周期完成信息传输。
    优点:可以实现高速率、大容量的数据传输,以及点对多点传输。
    缺点: 要求发送时钟和接收时钟保持严格同步,收发双方时钟允许的误差较小,同时硬件复杂。

    异步通信不需要时钟信号,而是在数据信号中加入开始位和停止位等一些同步信号,以便使接收端能够正确地将每一个字符接收下来,某些通信中还需要双方约定传输速率。
    优点:没有时钟信号硬件简单,双方时钟可允许一定误差。
    缺点: 通信速率较低,只适用点对点传输。

2). 在STM32CubeMx中配置串口

image-20230613203841988

序号1处

串口1(USART1)被标红了,显示为不可使用的状态,原因是USART1的USART1_TX(PA9)、USART1_RX(PA10)被占用了,在之前控制电机转动配置时,PA9作为IN2,PA10作为IN1,控制着左侧电机的转动方向。所以在这里无法使用串口1来与HC-05连接使用。串口2和串口3仍可以使用。

image-20230613204050671

序号2处

此处的配置为数据同步方式的配置,我们选择异步传输方式”Asynchronous“。

image-20230613204656737

Mode 描述 硬件引脚 支持外设
Asynchronous 异步模式 TXD、RXD USART、UART
Synchronous 同步模式 TXD、RXD、CK USART
Single Wire (Half-Duplex) 半双工单线模式 TXD USART、UART
Multiprocessor Communication 多处理器通讯模式 TXD、RXD USART、UART
IrDA 红外解码通信 TXD、RXD USART、UART
LIN 总线通信 TXD、RXD USART、UART
SmartCard 智能卡模式 TXD USART、UART
SmartCard with Card Clock 带时钟智能卡模式 TXD、CK USART

其中UART(Universal Asynchronous Receiver/Transmitter)为通用异步收发器

USART:(Universal Synchronous/Asynchronous Receiver/Transmitter)通用同步/异步串行接收/发送器USART是一个全双工通用同步/异步串行收发模块,该接口是一个高度灵活的串行通信设备。

原文链接:https://blog.csdn.net/qq_45699195/article/details/119912517

序号3处

此处的配置为数据同步方式的配置,我们选择”Disable“。

image-20230613205406458

关于硬件流控制的相关知识相信看完下面这两篇文章便会有个大致的了解,读者在此处可以选择使用硬件流控制,让数据传输更可靠。

stm32串口USART 硬件流控 --学习笔记 - 简书 (jianshu.com)

(128条消息) UART串口流控制(Flow control)_uart流控_夏沫の浅雨的博客-CSDN博客

序号4处

此处为串口通信的参数设置,因为之前在“一.3.6).(1). 进入AT模式设置波特率”处讲解过蓝牙模块通信参数设置的原因就是为了与这里的默认设置保持一致,所以此处我们不做改动。

序号5处

一篇介绍NVIC的文章:(128条消息) STM32的NVIC和中断的总结_stm32g0中断分组_wuyuzun的博客-CSDN博客

image-20230613210215067

在这里我们需要使能串口2的全局中断,表示此串口采用中断的方式处理收发数据。

因为我们没使用DMA这种数据传输方式,所以”DMA Settings“选项无需设置。

“GPIO Settings”这栏保持默认就行。

至此在STM32CubeMx中配置串口的工作完成,随后点击生成代码,进入到Keil中编写代码。

3). 在Keil中编写代码

先来了解下HAL_UART_Init、HAL_UART_Receive_IT、HAL_UART_IRQHandler、 UART_Receive_IT、HAL_UART_RxCpltCallback

先来看看正点原子关于其中三个函数的说明:(130条消息) HAL_UART_Init、HAL_UART_Receive_IT、HAL_UART_IRQHandler说明_quxuexi的博客-CSDN博客

chatGPT回答:

  • HAL_UART_Init:用于初始化UART串口通信的相关参数,如波特率、数据位、停止位、校验位等。

  • HAL_UART_Receive_IT:用于开启UART串口接收中断,并启动接收数据。

  • HAL_UART_IRQHandler:用于处理UART串口接收中断,当有数据接收到时,会调用该函数进行处理。 -

  • UART_Receive_IT:是一个宏定义,用于调用HAL_UART_Receive_IT函数。 -

  • HAL_UART_RxCpltCallback:是一个回调函数,当UART串口接收完成时,会自动调用该函数进行处理。在该函数中,可以对接收到的数据进行处理,比如存储到缓冲区中。

在上一步生成的项目代码中均可以查到上述五个函数的定义,查看方法是:Ctrl+f调出查找窗口并按照图中指示查找即可:

image-20230614234443177

变量

uint8_t\uint_16_t\uint32_t\uint64_t

这些类型的来源:这些数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。也就是说,它们其实是我们已知的类型的别名。

在C99标准中定义了这些数据类型,具体定义在:/usr/include/stdint.h ISO C99: 7.18 Integer types

typedef signed char             int8_t;   //  范围 -128~127     (1 Byte)
typedef short int               int16_t;  //  范围  -32768 ~  + 32767    (2 Bytes) 
typedef int                     int32_t;  //  范围 -32768  ~  + 32767    (4 Bytes)
typedef long int                int64_t;  //  范围   -9223372036854775808  ~   +9223372036854775807

typedef unsigned char           uint8_t;  //  范围0~255     (1 Byte)
typedef unsigned short int      uint16_t; //  范围0~65536   (2 Bytes)
typedef unsigned int            uint32_t; //  范围0~4294967295   (4 Bytes)
typedef unsigned long int       uint64_t; //  范围0~18446744073709551615   (8 Byte)
函数
HAL_UART_Init

image-20230614231915330

HAL_UART_Receive_IT

image-20230614231742765

//串口轮询模式发送,使用超时管理机制
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout); 

//串口轮询模式发送,使用超时管理机制
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);

//串口中断模式发送
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);

//串口中断模式接收
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);

//串口DMA模式发送
HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);

//串口DMA模式接收
HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);

/**
  * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  *               the configuration information for the specified UART module.
  * @param  pData Pointer to data buffer (u8 or u16 data elements).
  * @param  Size  Amount of data elements (u8 or u16) to be received.
  * @param  Timeout Timeout duration
  * @retval HAL status
  */

HAL_UART_IRQHandler

image-20230614231847419

代码main.c中代码编写

先定义一个无符号字符型变量,用于接收串口收到的数据。

在串口中断模式接收函数中HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)第二个参数是uint8_t类型的指针,所以我们需要定义这样一个uint8_t类型的变量usart2ReceiveData来接收数据。

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t usart2ReceiveData; //范围0~255
/* USER CODE END PD */

在前面我们为PWM信号设置了一些参数:psc=1440-1,arr=100-1,Pulse=50,在设置PWM信号占空比的函数__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,a)中第三个参数的值(即CRR的值)影响着占空比,CCR的值越大,占空比越大,CCR最小为0,最大为ARR+1 。经过我的实验,发现当CCR大于ARR+1时,无论大多少,从电机转动的效果来看,都是100%的占空比输出。结合PWM信号的参数(psc=1440-1,arr=100-1,Pulse=50),控制PWM信号占空比的CCR的值的范围在0~100之间。

当我们定义的变量usart2ReceiveData值为0~100时,表示收到串口发来的数据为 0~100,这时usart2ReceiveData的值应该表示PWM信号的占空比。当usart2ReceiveData的值大于100时,可以表示其他含义,其对应关系如下表所示:

usart2ReceiveData的值(十进制) usart2ReceiveData的值(十六进制) 含义
0~100 00~64 PWM信号占空比
101 65 小车停止
102 66 小车前进
103 67 小车后退
104 68 小车左转
105 69 小车右转

ASCALL码表对照表

image-20230616002328686

image-20230616002417056

再编写中断回调函数:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
    
    
	if(huart->Instance == USART2){
    
    	
		if(usart2ReceiveData == 101) 	Stop_run();//停止
		if(usart2ReceiveData == 102) 	Forward();//前进
		if(usart2ReceiveData == 103)	Backward();//后退
		if(usart2ReceiveData == 104)	Leftward();//左转
		if(usart2ReceiveData == 105)	Rightward();//右转
		if(0 <= usart2ReceiveData && usart2ReceiveData <= 100){
    
    
			__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_4,usart2ReceiveData);//设置占空比
			__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,usart2ReceiveData);//设置占空比
		}

		HAL_UART_Transmit(&huart2,&usart2ReceiveData,1,100);通过蓝牙向外发送数据,主要作为收到数据的反馈。
		HAL_UART_Receive_IT(&huart2,&usart2ReceiveData,1);
	}
}
/* USER CODE END 0 */

中断处理函数在“…\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c(2616)”处定义,定义为 : __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart),函数前面有个__weak,weak 顾名思义是“弱”的意思,所以如果函数名称前面加上__weak 修饰符,我们一般称这个函数为“弱函数”。加上了__weak 修饰符的函数,用户可以在用户文件中重新定义一个 同名函数,最终编译器编译的时候,会选择用户定义的函数,如果用户没有重新定义这个函数,那么编译器就会执行__weak 声明的函数,并且编译器不会报错。

我们在main.c的“/* USER CODE END 0 */”这行注释前重写中断处理函数。

再开启串口中断模式接收

 /* USER CODE BEGIN 2 */
	//串口中断模式接收
	HAL_UART_Receive_IT(&huart2,&usart2ReceiveData,1);//HAL_UART_Receive_IT(&huart2, (uint8_t *)rx_buf,1);这是接收1个字节到rx_buf中	
  /* USER CODE END 2 */

在while循环中不用再加控制小车运动的代码,因为我们将要通过手机蓝牙发送运动指令控制小车运动。while只保留控制板载绿色LED灯闪烁的代码即可:

while(1)
	{
    
    
    /* USER CODE END WHILE */
    /* USER CODE BEGIN 3 */
		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);//板载绿色LED小灯引脚电平反转
		HAL_Delay(100);	
	}

至此代码部分编写完成,然后编译,烧录。此阶段的main.c文件全部代码可以见:(133条消息) “三、从零开始,上手操作.第六阶段 变身蓝牙遥控小车. 3). 在Keil中编写代码. 代码main.c中代码编写“处参考main.c文件代码_quxuexi的博客-CSDN博客

4). 实物连接

实物连接示意图如下:

加上蓝牙_bb

实物连接图如下:

image-20230616211234013

5). 手机端蓝牙调试器的设置

在手机端蓝牙调试器APP中的“按钮控制界面”,如下图:

image-20230616212334832

第二行的“自动巡逻”、“手动”、“避障”是我们之后会设置的功能,目前暂时用不到,可以不用设置这三个按钮。

其他按钮设置如下:(全速也可以设为占空比100%)

慢速(占空比:43%) 中速(占空比:60%) 全速(占空比:90%) 前进
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qBi07jlp-1688999688891)(https://article.biliimg.com/bfs/article/20373387bbc891b06c38e639a57977b0431929b1.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MXnRGgU3-1688999688891)(https://article.biliimg.com/bfs/article/9e237c74fdddaa268f5ff409dacd2a39c84e16df.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Wxukqtac-1688999688892)(https://article.biliimg.com/bfs/article/24926d5ae69fd52535cc63cc1aaa84aef8a25231.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yem0yXMn-1688999688892)(https://article.biliimg.com/bfs/article/8586b72d67c58318795d35b1b6cbea3bbe8627df.png)]
左转 停止 右转 后退
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i1xryuyP-1688999688892)(https://article.biliimg.com/bfs/article/fe0be1a33c4703061eef346f2c036a570fb26b85.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DCR3puMR-1688999688893)(https://article.biliimg.com/bfs/article/1812dec88e360847ca29f5f656d728a64e1e78e4.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dc7JDA7D-1688999688893)(https://article.biliimg.com/bfs/article/54d48ef213cc3e5c9fa5054688e0bc19c5b6b7bd.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tKFjuoHW-1688999688894)(https://article.biliimg.com/bfs/article/8c1d1aac33a53b511fe646e6daf7b62a4b14a250.png)]

像这样通过按钮控制速度无法做到精确的控制PWM占空比,我们也可以学习自己在APP里新建一个工程,专门来控制小车。

蓝牙调试器中新建工程的方法见:提高开发效率-蓝牙调试器 - 简书 (jianshu.com)

如果不想新建工程,却又想精确地控制PWM占空比,我们也可以在使用的过程中动态地修改其中一个按钮发送的数据,发送0~100之间的数都表示发送的是占空比的值。比如我们修改“慢速”按钮的值,经过实验,我买的零件只有当占空比大于2B(十六进制)时小车电机才能正常转动。如果我们想设置占空比为50%,我们就可以将“慢速”按钮的值改为32(我们输入的值都是16进制,进制之间的转换可以看上面的ASCALL码表对照表)。

至此,我们万事具备,只欠东风。跟着上面的步骤完成了代码编写,编译,烧录,实物连接,手机APP设置等,现在就可以通电,蓝牙连接,手机端点击按钮发送指令控制小车运动。

至此第六阶段“变身蓝牙遥控小车”的内容告一段落,准备下一阶段的学习。

第七阶段 乒乓球识别

上一阶段讲了如何变身蓝牙小车,这一阶段主要讲乒乓球识别模块。

1. K210模块的入手,环境搭建(建议看完再动手)

主要参考这个视频: K210入门_哔哩哔哩_bilibili

跟着这个视频学,先是打开K210文档:https://wiki.sipeed.com/soft/maixpy/zh/index.html

然后下载了MaixPy IDE,我用的芯片是maix dock,查看文档后发现我的CH340驱动还没装:Maix Dock USB 驱动安装 - Sipeed Wiki,然后在文档里点击下载了CH340驱动的安装程序,安装时显示驱动预安装成功,感觉不对劲,就查了查安装方法:(53条消息) win10下如何装CH340驱动_坤小白的博客-CSDN博客

预安装成功是什么?

如果你安装驱动的时候没有接硬件,驱动装上但不会立刻使用,提示你是预安装成功,等你插上硬件的时候,系统会自动安装驱动程序。但如果你插上硬件时再点击安装驱动,仍然是预安装成功,可能就有些问题了。

原来需要插上芯片才能解决驱动的问题,插上后果然自动安装了驱动,拔掉芯片后,驱动仍然存在。

image-20230616225724514

接着去更新固件,照着文档的操作,我下载的固件是最新的那版,

image-20230616225732859

如果此时又有了新的版本,也可以进到最新此时最新的那个目录里去下载固件。

点进去,下载下面这个:

image-20230618201403350


小插曲

可是为什么要下载这个固件呢?之前我下载的是另一个固件,如下图:

image-20230618201744225

这个固件的含义:

image-20230616230100726

突然一天早上看了一篇博客:(55条消息) K210快速上手教程(色块、人脸、声源识别等)_k210教程_Smalld_cat的博客-CSDN博客

复制他的“识别色块并且串口输出最大色块中心坐标”这块代码,运行时报错:

image-20230616230212851

后来查了原因:(55条消息) K210(1)解决一些新上手出现的问题_image’ object has no attribute 'cpu__-ARCHER-_的博客-CSDN博客

知道应该是固件版本不对,这篇博客给了一个建议:MaixPy IDE中的错误-有问必答-CSDN问答

“下载的固件的问题,它官网提供的一些固件是不支持外设和相关算法的,直接用出厂带的固件或者下载“maixpy_vx.y.z_x_xxx*.bin”这个固件就行”。

于是到固件下载的网址:下载站 - Sipeed

下载了这个版本的固件:

image-20230616230233621

果然,问题被解决了,好耶!!!


然后打开固件下载软件(绿色软件,无需安装),选择固件,芯片类型,端口就可以下载了

image-20230616230111539

下载完会提示成功。

image-20230616230122050

下面是maixPy IDE的使用:

“要使用 MaixPy IDE , 固件必须是 v0.3.1 版本以上, 否则MaixPyIDE 上会连接不上, 使用前尽量检查固件版本和 IDE 版本,都更新到最新版以保障能正常使用”

我下载的固件是v0.6.2版本的,符合使用要求:

image-20230618201403350

打开maixPy IDE,默认有一个测试程序,可以打开摄像头

image-20230616230158968

接下来解决识别乒乓球的问题,观看这个教学视频(15:00处)[k210] MaixPy 上手教程_哔哩哔哩_bilibili

讲解了如何设置色块阈值。

设置完白色和黄色球体的阈值后,再运行就可以识别乒乓球了。

2. K210模块代码编写

K210与单片机的连接

记录一下MaixPy IDE上的初始程序:

# Hello World Example
#
# Welcome to the MaixPy IDE!
# 1. Conenct board to computer
# 2. Select board at the top of MaixPy IDE: `tools->Select Board`
# 3. Click the connect buttion below to connect board
# 4. Click on the green run arrow button below to run the script!

import sensor, image, time, lcd

lcd.init(freq=15000000)
sensor.reset()                      # Reset and initialize the sensor. It will
                                    # run automatically, call sensor.run(0) to stop
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
    lcd.display(img)                # Display on LCD
    print(clock.fps())              # Note: MaixPy's Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.

关于k210和maixPY的学习使用主要参考这两个官方中文文档:

MaixPy 文档简介 - Sipeed Wiki

序言 · OpenMV中文入门教程

首先需要了解我买的板子:M1W Dock,它属于Maix-I,W表示支持wifi功能

板子介绍:Sipeed M1/M1W - Sipeed Wiki

在这里找到了板子的一些文件资料:下载站 - Sipeed

K210的串口是任意映射的,所以我可以任意选择两个引脚作为输入和输出串口。

K210(Micro额外供电) STM32F103C8T6
GND GND
IO10(TXD) Rx(PB11)
IO9(RXD) Tx(PB10)

现在编写一下完整的将要保存到k210里的代码;主要实现的功能是:检测两种颜色的乒乓球,然后根据乒乓球在屏幕中的位置向单片机发送左转、右转、前进的命令。

在这里我放出来最后我使用的代码,中间过程及一开始的代码我放在一篇文章里:(134条消息) k210乒乓球识别模块乒乓球识别程序-初版_quxuexi的博客-CSDN博客

import sensor, image, time,lcd,utime
from machine import UART
from fpioa_manager import fm

fm.register(10, fm.fpioa.UART1_TX, force=True)#映射串口引脚
fm.register(9, fm.fpioa.UART1_RX, force=True)#映射串口引脚

uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=4096)

clock = time.clock()
lcd.init() #显示屏初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) #QQVGA: 160x120、QVGA: 320x240、
sensor.run(1)
sensor.skip_frames(20)#跳过刚启动开始的30帧照片
lcd.rotation(2)#LCD屏幕翻转,范围是0~3,上和下反转,左和右反转
sensor.set_hmirror(True)#摄像头镜像
sensor.set_auto_gain(False) # must be turned off for color tracking
#sensor.set_auto_whitebal(False) # must be turned off for color tracking,关闭白平衡

#sensor.set_brightness(3) #-3至+3,亮度越大,图片越亮
#sensor.set_saturation(2)#-3至+3,饱和度越大,色彩越鲜艳
#sensor.set_contrast(2)#-3至+3,对比度越大,颜色之间的反差越大

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob   

#存放不同的颜色阈值,依次为白色、黄色、绿色(白天光的上流:(82, 100, -3, 8, -3, 2))(晚上:(62, 100, -30, 4, -9, 31))
color = ((82, 100, -3, 8, -3, 2),(50, 96, -28, 18, 48, 90))
size_threshold = 4500 #若检测到的小球框的面积小于size_threshold表明小球距离小车太远,小车需要前进。这个参数的大小需要根据实际情况修改
target=()
flag = 0 #用于标记一个while循环中是否检测到小球,falg==1表示本轮发现了小球
count = 0
bug_open = False # True or False
previous_ball_location = 'r'
location_count=0 #用来防止误识别,保持识别结果的稳定性

def stable_output(location):
    global location_count,previous_ball_location
    if previous_ball_location != location:
        location_count = 1
        previous_ball_location = location
    else:
        location_count = location_count + 1
    if location_count >=10:
        uart.write(location) #通过串口向外发送数据,控制小车向location方向运动
        
clock.tick()#开始追踪运行时间。

while(True):
    sensor.set_auto_whitebal(False) # must be turned off for color tracking,检测黄球时,关闭白平衡,检测白球时开启白平衡
    img = sensor.snapshot().histeq(adaptive=True, clip_limit=3)
    img.lens_corr(1.8) #镜头畸变矫正(lens correction),lens_corr 为了去除畸变, 1.8 是默认参数,可以根据自己实际情况调整
    
    #专门检测黄色小球
    blobs = img.find_blobs([color[1]])
    if blobs:
        max_blob = find_max(blobs)
        x_error = max_blob[5]-img.width()/2#若x_error<0,则说明小球在左边,小车需要左转,若x_error>0,则说明小球在右边,小车需要右转
        h_error = max_blob[2]*max_blob[3]-size_threshold#若h_error<0,则小车距离小球还有距离,需要前进。
        img.draw_circle(max_blob.x()+int(max_blob.w()/2), max_blob.y()+int(max_blob.w()/2), int(max_blob.w()/2), color = (255, 255, 0),thickness=5)#识别到的白色圆形用白色的圆框出来
        #print("blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
        #给黄球标注框框
        #for b in blobs:
            #img.draw_circle(b.x()+int(b.w()/2), b.y()+int(b.w()/2), int(b.w()/2), color = (255, 255, 0),thickness=5)#识别到的白色圆形用白色的圆框出来
        if(x_error<-10 and max_blob.density()>0.7 and 70>max_blob.w()>8):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
            #stable_output('l')
            uart.write('l')
            flag = 1 #标记本轮发现过目标
            count = 0 #发现目标,count置为0,从头开始计数
            lcd.draw_string(10, 10, "Left", lcd.RED, lcd.YELLOW)
            
            if bug_open == True:
                print("黄球,串口发送l,","blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())

        if(x_error>10 and max_blob.density()>0.7 and 70>max_blob.w()>8):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
            #stable_output('r')
            uart.write('r')
            flag = 1 #标记本轮发现过目标
            count = 0 #发现目标,count置为0,从头开始计数
            lcd.draw_string(10, 20, "Right", lcd.RED, lcd.WHITE)
            
            if bug_open == True:
                print("黄球,串口发送:r,","blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
            
        if(-10<x_error<10 and h_error<0 and max_blob.density()>0.7 and 70>max_blob.w()>8):
            #stable_output('h')
            uart.write('h')
            flag = 1 #标记本轮发现过目标
            count = 0 #发现目标,count置为0,从头开始计数
            lcd.draw_string(10, 30, "Head", lcd.RED, lcd.GREEN)
            
            if bug_open == True:
                print("黄球,串口发送:h,","blob.density(): ", max_blob.density(),"blob.w(): ", max_blob.w())
    else:
        stable_output('t')
        if bug_open == True:
            print("无黄,串口发送:t,")
        
        #检测白球时,开启白平衡效果会更好一点
        sensor.set_auto_whitebal(True) # must be turned off for color tracking,开启白平衡
        img = sensor.snapshot().histeq(adaptive=True, clip_limit=3)
        img.lens_corr(1.8) #镜头畸变矫正(lens correction),lens_corr 为了去除畸变, 1.8 是默认参数,可以根据自己实际情况调整
        #思想:先识别出圆形,再识别圆的颜色
        #img.find_circles:控制从霍夫变换中监测到的圆。
        for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
                r_min = 2, r_max = 100, r_step = 2):
            #area为识别到的圆的区域,即圆的外接矩形框
            area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())    
            ##保存检测到的其中一个圆的一些参数,用于控制小车方向的计算
            #target = (c.x(), c.y(), c.r())          
            
            #像素颜色统计,roi 是感兴趣区域的矩形元组(x,y,w,h)。如果未指定,ROI即整个图像的图像矩形。操作范围仅限于 roi 区域内的像素。                
            statistics = img.get_statistics(roi=area)
            #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
            if color[0][0]<statistics.l_mode()<color[0][1] and color[0][2]<statistics.a_mode()<color[0][3] and color[0][4]<statistics.b_mode()<color[0][5]:#if the circle is red
                img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 255),thickness=5)#识别到的白色圆形用白色的圆框出来
                #保存检测到的其中一个圆的一些参数,用于控制小车方向的计算
                target = (c.x(), c.y(), c.r())
            #elif color[1][0]<statistics.l_mode()<color[1][1] and color[1][2]<statistics.a_mode()<color[1][3] and color[1][4]<statistics.b_mode()<color[1][5]:#if the circle is red
                #img.draw_circle(c.x(), c.y(), c.r(), color = (255, 255, 0))#识别到的黄色圆形用黄色的圆框出来
                ##保存检测到的其中一个圆的一些参数,用于控制小车方向的计算
                #target = (c.x(), c.y(), c.r())
            else:
                img.draw_circle(c.x(), c.y(), c.r(), color = (0, 0, 0),thickness=5)#识别到的白色圆形用白色的圆框出来
                target = (c.x(), c.y(), c.r())#其实这里应该不加这一句,但为了演示效果更好,让小车也追一下圆形
                
        if target:
            flag = 1 #标记本轮发现过目标
            #一张图只保留一个小球的信息,只追一个小球
            x_error = target[0]-img.width()/2 #若x_error<0,则说明小球在左边,小车需要左转,若x_error>0,则说明小球在右边,小车需要右转
            h_error = 2*target[2]*2*target[2]-size_threshold #若h_error<0,则小车距离小球还有距离,需要前进。
            
            if(x_error<-10):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
                #stable_output('l')
                uart.write('l')
                count = 0 #发现目标,count置为0,从头开始计数
                if bug_open == True:
                    print("白球,左转,发串口:l")
                lcd.draw_string(10, 10, "Left", lcd.RED, lcd.YELLOW)
            if(x_error>10):#设为-5是留有一定的误差空间,因为不一定小球非要在正中间才能被收集
                #stable_output('r')
                uart.write('r')
                count = 0 #发现目标,count置为0,从头开始计数
                if bug_open == True:
                    print("白球,右转,发串口:r")
                lcd.draw_string(10, 20, "Right", lcd.RED, lcd.WHITE)
            if(10>x_error>-10 and h_error<0):
                #stable_output('h')
                uart.write('h')
                count = 0 #发现目标,count置为0,从头开始计数
                if bug_open == True:
                    print("白球,前进,发串口:h")
                lcd.draw_string(10, 30, "Head", lcd.RED, lcd.GREEN)
        else:
            stable_output('t')          
            if bug_open == True:
                print("无白色球,发串口:t")
        target=()#清空target
        
    #控制视野内无球时,自动慢速右转
    if flag == 0:
        count = count+1
        if count%10 == 0:
            uart.write('s')
            utime.sleep_ms(10)
            uart.write('n') #通过串口向外发送数据,没有检测到黄色或白色小球
            
            if bug_open == True:
                print("视野无球,发串口:n")
            lcd.draw_string(250, 10, "FPS %f" % clock.fps(), lcd.BLACK, lcd.RED)
            count = 0
    flag = 0 #重置标记
    if bug_open == True:
        print("FPS %f" % clock.fps())#停止追踪运行时间,并返回当前FPS(每秒帧数)。在调用该函数前始终首先调用 tick 
        print("flag:",flag,"count:",count)
    lcd.display(img) #LCD 显示图片
    

如何提高小球检测成功率:

在最后检测的阶段,发现对于小球的检测存在不小的误差,经过实验,黄色小球直接使用色块查找比先找圆形在检验颜色的方法成功率高,所以黄色球直接颜色匹配。白色小球两种方法的效果都不大好,主要因为灯光和环境颜色的因素,导致白色小球和环境融合度高,不易被检测到,因此考虑能否调节亮度看看能不能改善一下效果。

查看了摄像头模块的库函数,可以设置对比度、亮度、饱和度、自动增益等,

(69条消息) 【K210】K210学习笔记一——sensor_k210摄像头模块_欠羽的博客-CSDN博客这篇文章探讨了自动增益,白平衡对图像呈现效果的影响。得出的结论是,颜色识别要关闭白平衡、自动增益要手动设置一个合适的值。

下面要上传脚本到开发板:参考上传脚本&模块到开发板 - Sipeed Wiki

image-20230618205017858

image-20230618205033858

运行这两行来检查是否保存成功:

import os 
os.listdir()

若运行之后并没有显示出boot.py文件,如下图:

image-20230618205134831

我都怀疑是出现问题了,小找了一下但没找到原因,后来自己试出来的方法:在显示完保存脚本成功后需要按一下板子的reset键,之后再上电,脚本就会直接运行了。所以其实脚本已经上传到开发板上了。

然后就是k210与stm32的连接了:

关于k210的供电问题:https://www.bilibili.com/video/BV1eS4y1h7r1/?spm_id_from=333.337.search-card.all.click&vd_source=ec7c0e8776ec5da069e2d080157bdbc2

这个视频的一条评论给出了答案(心酸)

image-20230618205226664

虽然理论上可以通过5V引脚给板子供电,一开始我这样做的,也确实能够正常工作,但做到后面,有时会出现k210的那块显示屏突然一闪然后不显示摄像头画面,板子上有电却无法正常工作。因为并没有找到是什么原因导致地这一现象,但后来我使用Micro口供电后这一问题便迎刃而解了。

K210(Micro额外供电) STM32F103C8T6
GND GND
IO10(TXD) Rx(PB11)
IO9(RXD) Tx(PB10)

3. 在Keil中main.c中编写代码

接下来,在单片机stm32上新创建一个用于收发的串口,之前用于蓝牙通信使用了串口2、这次我使用串口3,串口3的配置参照串口2的配置。

main.c中代码如下:

新定义一个变量用于接收串口三发送的数据,和控制串口2优先的标志(因为串口2直接与用户接触,优先级更高,串口2可以理解为手动模式控制,串口3为巡逻模式自动捡球,当处于手动模式时,禁止自动捡球)

uint8_t usart3ReceiveData;
int openUsart3Flag= 0;//openUsart3Flag为1表示开启巡逻模式自动捡球,为0表示不开启

image-20230617095740811

开启串口3中断

HAL_UART_Receive_IT(&huart3,&usart3ReceiveData,1);

image-20230617092710935

在中断回调函数中补充串口3的内容


小插曲

在编写中断回调函数的时候,我想加一个HAL_delay()函数,结果程序烧录后,小灯就不亮了,显然程序不知道卡在哪里了,搜索后找到解决办法:

(72条消息) STM32 中断中调用HAL_Delay卡死的原因及解决方法_delay中断优先级_BigProgrambug的博客-CSDN博客

 t=3127;  while(t--);  

大致相当于HAL_Delay(1);即延时一毫秒;


4). 手机端蓝牙调试器的设置

对“自动巡逻”、“手动”两个按钮进行设置

image-20230617103845038

编辑页面如下图:

自动巡逻 手动
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c8qnQlRy-1688999688895)(https://article.biliimg.com/bfs/article/9e03cd0da49d4540663cdb116d2cb7cb86a80d08.png)] [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zuNatUin-1688999688903)(https://article.biliimg.com/bfs/article/437f862e9fd22c28ae26a5b9fefa2a930bc0dac5.png)]

至此第七阶段的内容告一段落,准备下一阶段的学习。

第八阶段:学习原理图的绘制

到这里,本项目的大部分工作已经完成,只剩下一些小tesk。这一阶段主要讲如何给项目绘制它的原理图。

学习使用在线使用的嘉立创的EDA绘制原理图,网址:嘉立创EDA(标准版) - 免费、易用、强大的在线电路设计软件 (lceda.cn)

image-20230618210257547

在嘉立创EDA中导出的PNG图像在拉伸时容易模糊,这个视频的20:00介绍在Altium Designer Summer中导出不模糊的方法:(在文件里的打印预览里进行复制后粘贴,不过颜色是灰色)

https://www.bilibili.com/video/BV1PB4y1W7sc/?spm_id_from=333.337.search-card.all.click&vd_source=ec7c0e8776ec5da069e2d080157bdbc2

补充:想改为彩色可以右键预览图,点击Page Setup,有个color set,将其选中Color即可。

后来我发现一个方法:导出为svg格式的文件,然后直接将svg文件复制,粘贴到word中。

不过我还是觉得学习一下Altium Designer Summer软件的使用总没错,搜了一个学习教程:(83条消息) Altium Designer绘制stm32f103c8t6最小系统原理图_stm32f103c8t6原理图_牛奶yu茶的博客-CSDN博客

在将schlib中元件放到schdoc中时,元件老是跑到schdoc边框外面,导致无法选中,解决办法:ctrl+a全选,然后向下拖一点点。

参考:altium 画原理图添加的器件在边框外面挪不动进不了图框_百度知道 (baidu.com)

至此第八阶段的内容告一段落,准备下一阶段的学习。

第九阶段:亚克力板定制需要cad图纸

在淘宝上找人制作亚克力板时老板说必须要CAD图纸才能做,这一阶段主要讲如何绘制简单的CAD图。

AutoCAD安装教程:https://www.bilibili.com/video/BV1MY411J7ea/?spm_id_from=333.337.search-card.all.click&vd_source=ec7c0e8776ec5da069e2d080157bdbc2

安装包网盘地址:AutoCAD 2023 简体中文版_免费高速下载|百度网盘-分享无限制 (baidu.com)

网盘提取码:acad

照着视频安装完后打开软件,报错:

image-20230618210944012

我选择忽略这个错误,软件能用就行。

AutoCAD入门教程(我只看了p1、p2、p3、p4、p6):https://www.bilibili.com/video/BV1pR4y1C7AQ?p=1&vd_source=ec7c0e8776ec5da069e2d080157bdbc2

在标注尺寸时,需要额外设置,参考学习:CAD如何尺寸标注,标注样式怎样设置,大神教你快速标注秘诀!_哔哩哔哩_bilibili

学习笔记:

Dli:创建线性标注

Dal:斜着标注

Rec 空格 鼠标左键 d 空格 输入长度 空格 输入宽度 空格:创建矩形

C 空格 鼠标左键 d 空格 输入直径 空格:输入直径画圆

如何平分圆周,学习视频:https://www.bilibili.com/video/BV1YQ4y1Y7Z5/?spm_id_from=333.337.search-card.all.click&vd_source=ec7c0e8776ec5da069e2d080157bdbc2

画完结果:

image-20230618211713310

image-20230618211727470

上述图形的cad图文件在**其他资料**里可以找到

至此第九阶段的内容告一段落,准备下一阶段的学习。

第十阶段:红外避障模块的学习使用

这一阶段主要讲红外避障模块,之所以把它放在偏后面来说是因为我使用起来效果不大理想,传感器测距不精确,不好调节,而且在小车运动时产生震动,控制传感器测距的距离的螺丝容易松动,这导致可能上一秒刚调完只测范围30cm的障碍物,下一秒弹到测半米内的障碍物了,如果日后有机会可以尝试下超声波避障传感器的学习使用,这个用的挺多的。

一开始学习使用时,到网上搜索:“STM32 HAL库 红外避障”,找到一篇文章教学

(79条消息) 【STM32】标准库与HAL库对照学习教程外设篇–红外避障传感器_stm32红外避障传感器_修成真的博客-CSDN博客

我是照着这篇文章做的,但是里面的内容只是操作步骤,至于为什么没有讲解,就是没讲原理,但它提供了一个重要信息,GPIO_EXTI引脚的使用。

我查看红外避障原件的淘宝连接:红外壁障模块/避障小车避障传感器/黑白线识别 距离可调 光电开关-淘宝网 (taobao.com)

了解到:

连接方式:VCC-VCC;GND-GND;OUT-IO

当模块检测到前方障碍物信号时,电路板上绿色指示灯点亮电平,同时OUT端口持续输出低电平信号,反之,绿灯灭,引脚输出高电平,

红外避障元件就三个引脚,最值得注意的就是out引脚,这个引脚就要与stm32的GPIO_EXTI引脚来接收。

学习stm32的GPIO_EXTI引脚,看这篇文章:(79条消息) STM32CubeMX学习笔记(3)——EXTI(外部中断)接口使用_hal_gpio_exti_irqhandler_Leung_ManWah的博客-CSDN博客

在看到这里时:

image-20230618212540758

根据红外避障原件的特性:有障碍物发低电平,无障碍物发高电平。因此应该选择下降沿触发中断

但是硬件上已外部上拉这里我不清楚,

恶补下硬件知识:

硬件基础知识:上下拉电阻 - 知乎 (zhihu.com)

对于普通的单片机I/O,输入口具有上拉和下拉能力是什么意思啊?望高手解释下!_百度知道 (baidu.com)

查看了stm32f103系列的文档:

image-20230618212611346

image-20230618212624060

image-20230618212633277

这里的硬件上外部上拉是指有没有在单片机外面自己配置电阻,显然我没有电阻,所以应该选择内部上拉电阻。

image-20230618212655223

而且: 单个红外避障模块检测距离2~30cm,检测角度35°

我有三个元件,前面弄俩,后面弄一个。

照着这篇文档的操作步骤,我选择设置PA1、PA4、PA5为避障模块out的信号接收引脚PA1在前左、PA4在前中、PA5在前右

image-20230618212731058

然后生成代码。

在编写红外避障对应的回调函数时,有个问题,就是避障时应该关闭巡逻模式的串口数据传输,就是在躲避障碍物的时候,即使发现了小球,也应该优先去躲避障碍物,而不是避障的同时也执行着前去捡球的功能。

自然而然地想到了设置两个外部中断地优先级的方法来控制,(之前蓝牙串口和k210俩串口其实也可以这样来搞,但是当时没想到优先级,就设置了一个信号量来控制,这样也能实现)

但我觉得用优先级显得简单一点,逻辑更清晰一点。

恶补知识点:(79条消息) STM32 EXTI外部中断及NVIC的抢占优先级和响应优先级解释_一个旅者的博客-CSDN博客

设置如下:

image-20230618212751506

在main.c编写的避障模块回调函数:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    
    
	if(GPIO_Pin==out1_Pin)
	{
    
    
		infrared_signal1 = 'T';
		__HAL_GPIO_EXTI_CLEAR_IT(out1_Pin);  //清除中断标志
	}
	else if(GPIO_Pin==out2_Pin){
    
    
		infrared_signal2 = 'T';
		__HAL_GPIO_EXTI_CLEAR_IT(out2_Pin);  //清除中断标志
	}
	else if(GPIO_Pin==out3_Pin){
    
    
		infrared_signal3 = 'T';
		__HAL_GPIO_EXTI_CLEAR_IT(out3_Pin);  //清除中断标志
	}
}

编译后没有error,烧录到stm32上,连接引脚,上电运行。

一开始传感器绿灯一直亮,表示一直有障碍物,这时候可能是因为设置的检测距离太远,可以调节一下检测距离,调节方法:旋转传感器上的螺丝(旋钮),参考:(81条消息) E18-D80NK 红外避障传感器怎么调检测距离-其他-CSDN问答

至此第十阶段的内容告一段落,准备下一阶段的学习。

第十一阶段:给电源加上一个开关

为什么要加上一个开关呢?一是每次通电都要手动插上电源,有时候还插歪了,还有就是小车运动一段时间后,蓝牙和k210好几次都无故断电,不工作了,我怀疑可能是接线问题

于是搜相关问题,发现一篇文章讲了电源通电时注意不烧坏单片机的我觉得挺有道理的。就是“一定要先接通负极再接正极。“

参考链接:(94条消息) STM32接电机驱动,杜邦线供电,然后反烧问题_stm32供电最大电流_我与nano的博客-CSDN博客

于是后来给能源加上一个开关,的使用方法:

船型开关也叫翘板开关它的接线脚有两个接线柱和三个接线柱的,下面华琴小编带你了解船型开关、跷板开两脚和三脚的差异:

1、两脚船型开关、翘板开关

船型开关和通常的开关一样,起个断开和接通电路的效果。绝不允许将他的两个接线柱一个接正极一个接负极,那样会短路的。

准确的接法是,电源的正极进入船型开关的任意一接线柱都可以,然后它的另一接线柱引出接入负载后再回到电源负极。

2、三脚船型开关、翘板开关

三个接线柱的应当不会有指示灯,而是带常开常闭点的,通常中心的是公共点,两头的是常开或常闭(即是不同方位的开关)

做转换开关时,中心的是动触点,两头的是定触点;做单个开关时,可以只接中心柱和边上任意一柱。

参考链接:电源插座,船形开关,8字插座,品字插座,AC插座厂家,梅花插座,八字插座,东莞市华琴电子有限公司 (huaconn.com.cn)

至此第十一阶段的内容告一段落。

第十二阶段:STM32F103C8T6的一个引脚烧坏了怎么办?

新手学习单片机有可能会遇到将某个GPIO口烧坏的情况,此时不必惊慌,只需要用另一个具有同样功能的引脚代替,然后将keil中与之相关的代码也换成另一个引脚即可。这一阶段主要讲如何更换烧坏的引脚。

LED3中的PB5引脚烧坏,使用PB8替换这个引脚为LED4工程

做修改引脚的工作时,我先将LED3复制一份并命名为LED4,打开LED4的ioc文件,在cubeMAX中编辑引脚。我将无法使用的引脚(PB5)设为Reset_State状态,

image-20230618213545290

然后查看的STM32F103c8t6的引脚,再找一个GPIO口,我选择PB8这个引脚

image-20230618213708915

将PB8设为GPIO口,标签改为IN3

image-20230618213722441

其他的都不改,直接点击GENERATE CODE,生成代码,再选择open project

image-20230618213734537

来到keil编辑器中,使用快捷键Ctrl+f,查找所有的PB5,就搜“GPIO_PIN_5 “就行,

image-20230618213749601

得到结果:

image-20230618213801148

所以我们只需要修改thread_run.c和main.c中的代码即可,将PB5都改为PB8.

比如说这一个,我们只要将数字5改成8即可:

image-20230618213812594

改为:

image-20230618213826195

全部修改完之后,先编译一下,没毛病:

image-20230618213836963

然后就是烧录到单片机上运行看看,烧录时一定记得将锂电池的供电关闭(否则引脚又烧了别怪别人)

image-20230618213846250

烧录完就更改接线,将L298N的IN3接到PB8即可。

然后就是给小车通电。到此更换引脚的工作结束。全部修改完之后,编译,烧录,小车上电测试

至此第十二阶段的内容告一段落。

补充

1. 误把SWD调试接口禁用,导致无法烧录

image-20230616224059570

烧录时弹出的窗口,打开魔法棒发现设备通信失败:

image-20230616224313132

我怀疑的问题就出现在,有一次我新建了一个项目用来测试串口通信的,结果在sys设置那里没有设置Serial Wire,然后就稀里糊涂生成了代码,然后烧录,我也不记得有没有烧录成功,但后来就无法烧录了,使用之前可以烧录的项目进行烧录也无法烧录,我一度怀疑是不是板子或烧录器被我玩坏了。

image-20230616224351718

后来上网搜了问题的解决办法,发现应该是我把SWD调试接口禁用了,详情见文章:在MDK调试STM32出现“could not stop cortex-m device”解决办法 - 简书 (jianshu.com)

然后找了解决办法:

1.板子上电之前将BOOT0引脚接到3.3V高电平
2.板子上电
3.重新烧录程序
4.BOOT0置地电平

和原文档中说法有点不一样,我的情况和评论里一位大佬一样:

image-20230616224421779

(65条消息) keil提示仿真器SWD/JTAG Communication Failure的问题解决_y511374875的博客-CSDN博客

总之有惊无险地解决了问题,又可以烧录了。

这样设置一次烧录成功后,之后把boot0再次接到0v,也可以像往常一样烧录了。回归了正常。

附件

一、官网

keil官网:Keil Embedded Development Tools for Arm, Cortex-M, Cortex-R4, 8051, C166, and 251 processor families.

Openmv官方网站:追小球的小车 · OpenMV中文入门教程

Openmv函数资料:MicroPython 函数库 — MicroPython 1.9.2 文档 (singtown.com)

MaixPy 文档简介:MaixPy 文档简介 - Sipeed Wiki

二、B站视频

一、 去b战找视频做了。

库函数开发项目视频教学:[stm32入门教程][STM32实战项目]STM32智能小车教程-循迹-避障-蓝牙遥控-跟随-stm32f103c8t6-stm32最小系统-手把手入门教程_哔哩哔哩_bilibili

HAL库开发项目视频教学:STM32智能小车V3-STM32入门教程-STM32项目-STM32循迹小车 避障 蓝牙遥控 课设 毕设 电赛 嵌入式学习 PID控制算法 编码器电机 跟随_哔哩哔哩_bilibili

MaixPy 上手教程:[k210] MaixPy 上手教程_哔哩哔哩_bilibili

三、感兴趣的文档

(57条消息) K210快速上手教程(色块、人脸、声源识别等)_k210教程_Smalld_cat的博客-CSDN博客

找圆算法:

opencv —— HoughCircles 霍夫圆变换原理及圆检测

找圆算法((HoughCircles)总结与优化_我不是校长的博客-CSDN博客

色块识别的改善:(88条消息) openmv学习之旅②之色块追踪算法的改善_openmv不同颜色的一般阈值__杰杰_的博客-CSDN博客

这个连接也是做捡球小车的,他的小车的外貌,组装和捡球思路可以给我提供改进的思路。

捡球机器人 - DF创客社区 - 分享创造的喜悦 (dfrobot.com.cn)

image-20230618220635253

额外准备的小问题(部分)

一、STM32单片机的供电方式?

有两种方法:

第一,通过Micro USB接口供电;第二,通过5V或3.3V的引脚给单片机供电,5V供电时,单片机内部有调压器将5V调为3.3V。

关于电源的一些符号说明:(106条消息) STM32F103电源引脚分析对比_stm32f103供电架构_王小琪0712的博客-CSDN博客

2. How to deal with the jumper on the STM32 microcontroller?

boot0 and boot1, boot0 is mainly related to the burning method

3. Common sense

Working voltage of tt DC motor: 3V~6V

The function of the filter capacitor is to make the level more stable

Using a high-speed external clock generated by a crystal/ceramic resonator, the high-speed external clock (HSE) can be generated using an oscillator composed of a 4~16MHz crystal/ceramic resonator.

64K bytes of built-in flash memory for program and data storage, 20K bytes of built-in SRAM

image-20230529160943072

External Interrupt/Event Controller (EXTI)
The external interrupt/event controller contains 19 edge detectors for generating interrupt/event requests. Each interrupt line can independently configure its trigger event (rising edge or falling edge or both edges) and can be independently masked; there is a pending register to maintain the status of all interrupt requests.

RTC (real-time clock), universal synchronous/asynchronous receiver-transmitter (USART)

The STM32F103xx enhanced series products include 1 advanced control timer and 3 ordinary timers.

image-20230529161811170

Each GPIO pin of the general-purpose input and output interface (GPIO)
can be configured by software as an output (push-pull or open-drain), input (with or without pull-down or pull-down), or a multiplexed peripheral function port. Most GPIO pins are shared with multiplexed digital or analog peripherals. Except for ports with analog input functions, all GPIO pins have high current passing capabilities.

4. Schematic explanation:

Getting Started with STM32: Analysis of the smallest system board circuit of a microcontroller. You can also design it after watching it - Educational Video - Sohu Video (sohu.com)

STM32 second training (schematic explanation, welding precautions)_bilibili_bilibili

Guess you like

Origin blog.csdn.net/quxuexi/article/details/131649565