GPIO Operation Based on STM8---STM8-Chapter 1

Publisher:一直333Latest update time:2019-11-21 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM8 Reference address:GPIO Operation Based on STM8---STM8-Chapter 1

Previous article:STM8S103 series IO port simulates serial port communication (realizes real serial port)
Next article:STM8S103 debugging points

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号