1. General Appeal
Maybe the microcontroller seems to be a difficult thing to you, but as far as I know, the microcontroller is nothing more than controlling its GPIO port, so it can be seen that learning how to operate and control the GPIO port is a very important thing for using the microcontroller.
In the MCU equipped with STM8, there are 12 I/O working modes. In fact, the concept here is also the same as other MCUs such as STM32. If you understand these 12 states, you can basically understand most of the I/O ports.
2. Introduction to GPIO of STM8S103
GPIO (English: General-purpose input/output), the abbreviation of general-purpose input/output, its pins can be freely used by users through program control. The pins can be used as general-purpose input (GPI) or output (GPO) or general-purpose input and output (GPIO) according to the actual reference quantity.
Open the library file stm8s_gpio.h in the official IAR routine and you can find all the IO modes of STM8.
typedef enum
{
GPIO_MODE_IN_FL_NO_IT = (uint8_t)0x00, /*!< Input floating, no external interrupt
GPIO_MODE_IN_PU_NO_IT = (uint8_t)0x40, /*!< Input pull-up, no external interrupt */
GPIO_MODE_IN_FL_IT = (uint8_t)0x20, /*!< Input floating, external interrupt */
GPIO_MODE_IN_PU_IT = (uint8_t)0x60, /*!< Input pull-up, external interrupt */
GPIO_MODE_OUT_OD_LOW_FAST = (uint8_t)0xA0, /*!< Output open-drain, low level, 10MHz */
GPIO_MODE_OUT_PP_LOW_FAST = (uint8_t)0xE0, /*!< Output push-pull, low level, 10MHz */
GPIO_MODE_OUT_OD_LOW_SLOW = (uint8_t)0x80, /*!< Output open-drain, low level, 2MHz */
GPIO_MODE_OUT_PP_LOW_SLOW = (uint8_t)0xC0, /*!< Output push-pull, low level, 2MHz */
GPIO_MODE_OUT_OD_HIZ_FAST = (uint8_t)0xB0, /*!< Output open-drain, high-impedance level,10MHz */
GPIO_MODE_OUT_PP_HIGH_FAST = (uint8_t)0xF0, /*!< Output push-pull, high level, 10MHz */
GPIO_MODE_OUT_OD_HIZ_SLOW = (uint8_t)0x90, /*!< Output open-drain, high-impedance level, 2MHz */
GPIO_MODE_OUT_PP_HIGH_SLOW = (uint8_t)0xD0 /*!< Output push-pull, high level, 2MHz */
}GPIO_Mode_TypeDef;
In the chip data, we can see that the basic structure of the I/O port is as follows:
Since we use library functions for development, this article will not explain the specific register operations. If you want to know more, please refer to the manual of the STM8 chip.
3. Mode Introduction
3.1 Input Floating
Floating input has the word IN_FL in the IO mode, such as: GPIO_MODE_IN_FL_NO_IT, GPIO_MODE_IN_FL_IT.
Floating input is also called suspended input. Generally, floating input and pull-up input are compared for learning. The level of floating input is uncertain. Even a small external input signal will change it. If the pin is set to floating, the level of the port is uncertain.
3.2 Input pull-up
The pull-up input has the word IN_PU in the IO mode, such as: GPIO_MODE_IN_PU_NO_IT, GPIO_MODE_IN_PU_IT.
When the input is pulled up, there is a pull-up resistor inside the pin that is connected to the power supply VDD through a switch. When the pin is not connected to an external circuit, the I/O pin level set to the pull-up input mode is a definite high level.
3.3 Output open-drain
Open-drain input has the word OUT_OD in the IO mode, such as: GPIO_MODE_OUT_OD_LOW_FAST, GPIO_MODE_OUT_OD_LOW_SLOW, GPIO_MODE_OUT_OD_HIZ_FAST, GPIO_MODE_OUT_OD_HIZ_SLOW.
Open drain output means no output voltage, it is grounded when low level, and not grounded when high level. If an external pull-up resistor is connected, the voltage will be pulled to the power supply voltage of the pull-up resistor when the output is high level. This method is suitable when the voltage of the connected peripheral is lower than the voltage of the microcontroller.
The two are very similar and the working principle is the same. The difference is that the field effect tube used in the open drain output needs to be added with a pull-up resistor when used.
3.4 Output push-pull
Push-pull input has the word Output push-pull in the IO mode, such as: GPIO_MODE_OUT_PP_LOW_FAST, GPIO_MODE_OUT_PP_LOW_SLOW, GPIO_MODE_OUT_PP_HIGH_FAST, GPIO_MODE_OUT_PP_HIGH_SLOW.
Push-pull output can output high and low levels and connect digital devices; push-pull structure generally refers to two transistors being controlled by two complementary signals, and one transistor is always turned on while the other is turned off. The high and low levels are determined by the power supply of the IC.
3.5 Interrupts and Output Speed
Interrupts are marked with the word IT in IO mode. Interrupts only exist in IO input, because setting an interrupt in output does not make any sense. Interrupts mean to stop the current work and then perform another task, and then come back to perform the original task after the execution is completed.
The output speed also only exists in IO output. The output speed of IO can be adjusted to divide them into levels, such as: low level, 10MHz, low level, 2MHz, high-impedance level, 10MHz, high level, 10MHz, high-impedance level, 2MHz, high level, 2MHz.
3.6 Initial Level
When we initialize GPIO, there will be an initial level operation. For example, GPIO_MODE_OUT_OD_LOW_FAST, GPIO_MODE_OUT_OD_HIZ_FAST, and GPIO_MODE_OUT_PP_HIGH_FAST contain LOW, HIZ, and HIGH, which are low level, high impedance level, and high level respectively.
4. Routines
4.1 Compilation environment:
My compilation environment is IAR, which is the mainstream platform for STM8 and is highly recommended. However, I plan to wait until STCubeMX is updated with a more convenient version before using Keil5, because I used Keil5 when I was using STM32, which is indeed very convenient. You can also learn to use it.
4.2 Main chip:
My main chip is 103 in the STM8S series. Among them, STM8S 003, 005, 103, 105 have the same configuration (peripheral and CPU frequency, FLASH), and can be burned if the code is the same.
4.3 Code
First define the corresponding LED lead angle parameters in the header file.
/* Define --------------------------------------------------------------------*/
/*LED*/
#define Led_Opt_Pin GPIO_PIN_5
#define Led_Opt_GPIO_Port GPIOB
In the main function, call the MX_GPIO_Init() function to initialize the IO pin, and then call the GPIO_TogglePin() function to flip the IO. If you flip an LED light directly, you can see obvious changes.
/*******************************************************************************
* Function Name : MX_GPIO_Init
* Description : GPIO_Init
* Input : None
* Output : None
* Return : None
********************************************************************************/
void MX_GPIO_Init(void)
{
//LED mode is push-pull high-speed output
GPIO_Init(Led_Opt_GPIO_Port,Led_Opt_Pin, GPIO_MODE_OUT_PP_HIGH_FAST);
}
/*******************************************************************************
* Function Name : GPIO_TogglePin
* Description : None
* Input : None
* Output : None
* Return : None
********************************************************************************/
void GPIO_TogglePin(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
{
GPIOx->ODR ^= GPIO_Pin;
}
LED off
LED bright
5. Conclusion
The description and reference of STM8's GPIO ends here. Thank you for your clicks.
Previous article:STM8S103 series IO port simulates serial port communication (realizes real serial port)
Next article:STM8S103 debugging points
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- 【Silicon Labs BG22-EK4108A Bluetooth Development Review】I. Hardware Appreciation and Development Environment Introduction
- [ESP32-S2-Kaluga-1 Review] 4. LCD example compilation pitfalls
- Several issues on key-controlled 8X8LED dot matrix screen displaying graphics program
- FAQ: PolarFire SoC FPGA Secure Boot | Microchip Security Solutions Seminar Series 12
- Some Problems on Measuring AC Current with Current Transformer
- Simulation of staying up late is harmful to health
- [RVB2601 Creative Application Development] + RTC Clock Display Experiment
- From traditional substation to smart substation
- [Raspberry Pi Pico Review] Xiaohui Review
- Analog Circuit Design Handbook: Advanced Application Guide