Crazy Shell AI open source drone GPIO (LED flight status light, signal light control)
[Copy link]
1. LED
1.1 Introduction to LED
LED (Light Emitting Diode) is the abbreviation of light-emitting diode. LED is a very common lighting device in life. There are many types of LED in our lives, as shown in the figure below.
Although LEDs come in many forms in our lives, all of them are represented in circuits using the symbols shown in the figure below.
1.2 LED Lighting Principle
The most important light-emitting structure of LED is the bean-sized lamp bead inside the lamp. Although it is small in size, it has a lot of secrets. The following figure shows the internal structure of LED.
This structure is extremely complex and is divided into several layers: the top layer is called the P-type semiconductor layer, the middle layer is the light-emitting layer, and the bottom layer is called the N-type semiconductor layer.
To understand it from a physics perspective: when current passes through the chip, the electrons in the N-type semiconductor and the holes in the P-type semiconductor collide and recombine violently in the light-emitting layer to produce photons, which emit energy in the form of photons (that is, the light we see).
2. GPIO
2.1 GPIO Introduction
GPIO (General-purpose input/output), short for general-purpose input and output, is a pin of a microcontroller that can be used freely by the user and can be configured as output or input. The output can be a "high level" or a "low level". In electronic circuits, a "high level" is a high voltage state, which is represented by logic 1, and a "low level" is a low voltage state, which is represented by logic 0.
The STM32F103 series is a 32-bit microcontroller based on the ARM Cortex M3 core from STMicroelectronics. The main core used in the flight control is the 48-pin STM32F103CBT6, and its pinout is shown in the figure below.
The GPIO of STM32F103CBT6 has many functions, including four groups of GPIO, A, B, C, and D. Each GPIO port in each group can be used as an output port, and can also be used as a multiplexed pin, such as the pins of special interfaces such as serial port, I2C, and SPI. However, it should be noted that the multiplexing function of each pin is limited, so when connecting the hardware, you need to pay attention to the multiplexing functions of each pin, which can be checked in the data sheet of STM32F103. There are 8 modes for the GPIO port, namely: floating input, pull-up input, pull-down input, analog input, open-drain output, push-pull output, push-pull multiplexing function, and open-drain multiplexing function, as shown in the following table.
STM32F103GPIO working mode
GPIO working mode
|
Input Mode
|
Output Mode
|
Maximum output speed
|
(1) GPIO_Mode_IN_FLOATING Floating input
(2) GPIO_Mode_IPU Pull-up input
(3) GPIO_Mode_IPD Pull-down input
(4) GPIO_Mode_AIN Analog input
|
(1) GPIO_Mode_Out_OD open-drain output (with pull-up or pull-down)
(2) GPIO_Mode_AF_OD multiplexed open-drain output (with pull-up or pull-down)
(3) GPIO_Mode_Out_PP push-pull output (with pull-up or pull-down)
(4) GPIO_Mode_AF_PP multiplexed push-pull output (with pull-up or pull-down)
|
(1) 10MHZ
(2) 2MHZ
(3) 50MHZ
|
We will not introduce these 8 functions one by one. If you are interested, you can search online to learn more. Here we mainly explain the difference between open-drain output and push-pull output.
(1) Open-drain output:
The output end is equivalent to the collector of the transistor. A pull-up resistor is required to obtain a high level. The driving capability of the external pull-up resistor is used to reduce the internal driving of the IC. The driving capability is strong and suitable for current-type driving, which can reach 20mA.
(2) Push-pull output:
It can output high and low levels and connect digital devices. It is composed of two transistors or MOSFETs with the same parameters connected in a push-pull manner, each responsible for the waveform amplification task of the positive and negative half cycles. When the circuit is working, only one of the two symmetrical power switch tubes is turned on at a time, so the conduction loss is small and the efficiency is high, which not only improves the load capacity of the circuit, but also improves the switching speed.
To summarize: push-pull output can output strong high and low levels and connect digital devices; while open-drain output can only output low levels, and high levels must be pulled up by external resistors. The output end is equivalent to the collector of the transistor, and a pull-up resistor is required to obtain a high level state. It is suitable for current-type driving and has a relatively strong ability to absorb current (generally within 20ma).
2.2 GPIO related registers
Each GPIO port of the STM32F103 has: two 32-bit configuration registers (GPIOx_CRL and GPIOx_CRH), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit position/reset register (GPIOx_BSRR), a 16-bit reset register (GPIOx_BRR), and a 32-bit lock register (GPIOx_LCKR).
(1) GPIOx_CRL register (x = A ~ G)
This register is used to configure the input and output mode and rate setting of IO0~7.
(2) GPIOx_CRH register (x = A ~ G)
This register is used to configure the input and output mode and rate setting of IO8~15.
(3) GPIOx_IDR register (x=A~G)
IDR is the input data register of GPIO. The status of IO can be read through the IDR register. It should be noted that the IDR register can only be read in word (16-bit) form.
(4) GPIOx_ODR register (x = A ~ G)
ODR is the output data register of GPIO. High and low levels can be output through the ODR register.
(5) GPIOx_BSRR register (x = A ~ G)
BSRR is the port bit set/clear register of GPIO. The upper 16 bits of the BSRR register are used to clear the IO bit, and the lower 16 bits are used to set the IO bit. It should be noted that the corresponding function is only effective when BSRR is written with 1, and writing 0 is invalid.
(6) GPIOx_BRR register (x=A~G)
BRR is the port bit clear register of GPIO. The corresponding function is effective only when 1 is written to BRR, and writing 0 is invalid.
(7) GPIOx_LCKR register (x = A ~ G)
LCKR is the port configuration lock register of GPIO. The LCKR register is used to lock the configuration of the IO port. After setting, the IO state can no longer be configured except after reset.
2.3 GPIO Experiment
The content of this experiment is to periodically light up the drone's flight status lights and signal lights. The flight status lights are under the four propellers, and the signal lights are on both sides of the switch.
Looking at the schematic diagram, we can see that the drone's flight status light is connected to the NPN transistor Q1, and the base of the transistor is connected to PA8 of the microcontroller; the two signal lights are connected to PC13 and PC14 respectively.
The idea of writing code is shown in the following table
1
|
Pin Configuration
|
1. Define the structure;
2. Enable the clock;
3. Filling structure;
4. Load the structure.
|
2
|
Logical Processing
|
Cycle LED
|
According to the code idea, write the code (by calling the official library) as shown below:
After completing the configuration, you only need to cycle on and off the LED.
The delay is shown in the figure below.
Save, compile, and download, as shown in the figure below, 1 is save, 2 is compile, and 3 is download.
After downloading the code to the flight controller, you can see the LED indicator of the flight controller and the flight status lights under the four propellers flashing periodically, as shown in the figure below.
|