Ⅰ. Write in front
The beginning is always the hardest, as long as you are willing to work hard; the master can lead you to the door, but the practice depends on yourself; when you see this article, it means you are lucky, and the author will launch a series of STM8S tutorials that will help you step into the world of STM8S.
This article is the beginning of the STM8S tutorial, written for those who are just getting started with STM8S. Before studying this article, it is recommended that you first master the basic knowledge of C language and understand the basic knowledge of microcontrollers.
The author combines theoretical knowledge with software engineering source code to explain the relevant knowledge of STM8S. If you have mastered the basic knowledge of microcontrollers, the content is relatively easy. The tutorial focuses on combining the "STM8S Reference Manual" to explain the relevant theoretical knowledge.
For your convenience, the content of this article has been organized into a PDF file:
http://pan.baidu.com/s/1i5uWhJR
Author:strongerHuang
All rights reserved. Any other commercial use is prohibited without permission!!!
Ⅱ. GPIO Basics
GPIO: General Purpose Input Output
Each port is assigned an output data register, an input pin register, a data direction register, a select register, and a configuration register. Whether an I/O port works as input or output depends on the state of the data direction register of the port.
Ø Input mode: floating input and input with pull-up;
Ø Output mode: push-pull output and open-drain output;
Tip: The input and output modes can be configured by software. STM8S does not have input pull-down.
Each IO can be configured as an external interrupt and can be enabled and disabled individually;
When used as analog input, the input Schmitt trigger can be turned off to reduce power consumption.
III. GPIO Software Engineering Description
In order to help everyone understand what specific functions software engineering implements, we will briefly describe a few important points.
This article is about basic software engineering, mainly describing the software engineering related instructions, software flow and focusing on GPIO configuration.
Before studying this article, it is recommended to study the following two articles:
IAR for STM8 introduction, download, installation and registration
IAR for STM8 series tutorial (I)_Detailed process of creating a new software project
The article ends with downloads of: STM8S data and the corresponding software engineering source code.
IV. Software Engineering Source Code
1. About the project
The project uses the latest IAR for STM8 (EWSTM8) integrated development environment and the latest standard peripheral library of STM8S.
This project is suitable for STM8S and STM8AF series chips, including:
STM8S208、STM8S207、STM8S007、STM8AF52Ax、STM8AF62Ax、STM8S105、
STM8S005、STM8AF626x、STM8AF622x、STM8S103、STM8S003、STM8S903
Unless otherwise specified, the project is suitable for the above chips. You only need to modify the configuration to the corresponding chip.
Modify two places:
1.Device chip model: Project -> Options -> General Options -> Target -> Device
2. Predefine chip model: Project -> Options -> C/C++ Compiler -> Preprocessor -> Defined Symbols
2. Software Process
This article provides a relatively simple process for STM8S software engineering:
Configure clock, initialize GPIO, while loop
3. Code Analysis Description
A.Configure the clock
By default, the 16MHz high-speed internal RC oscillator (HSI) is used for 8-division, that is, the system clock defaults to 2M (2M = 16M / 2). We configure it to 16M through software, that is, 1-division.
The code for configuring the clock is as follows:
void CLK_Configuration(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //HSI = 16M (divide by 1)
}
The CLK_PRESCALER_HSIDIV frequency division value parameter can be viewed by tracking the code:
typedef enum {
CLK_PRESCALER_HSIDIV1 = (uint8_t)0x00, /*!< High speed internal clock prescaler: 1 */
CLK_PRESCALER_HSIDIV2 = (uint8_t)0x08, /*!< High speed internal clock prescaler: 2 */
CLK_PRESCALER_HSIDIV4 = (uint8_t)0x10, /*!< High speed internal clock prescaler: 4 */
CLK_PRESCALER_HSIDIV8 = (uint8_t)0x18, /*!< High speed internal clock prescaler: 8 */
CLK_PRESCALER_CPUDIV1 = (uint8_t)0x80, /*!< CPU clock division factors 1 */
CLK_PRESCALER_CPUDIV2 = (uint8_t)0x81, /*!< CPU clock division factors 2 */
CLK_PRESCALER_CPUDIV4 = (uint8_t)0x82, /*!< CPU clock division factors 4 */
CLK_PRESCALER_CPUDIV8 = (uint8_t)0x83, /*!< CPU clock division factors 8 */
CLK_PRESCALER_CPUDIV16 = (uint8_t)0x84, /*!< CPU clock division factors 16 */
CLK_PRESCALER_CPUDIV32 = (uint8_t)0x85, /*!< CPU clock division factors 32 */
CLK_PRESCALER_CPUDIV64 = (uint8_t)0x86, /*!< CPU clock division factors 64 */
CLK_PRESCALER_CPUDIV128 = (uint8_t)0x87 /*!< CPU clock division factors 128 */
} CLK_Prescaler_TypeDef;
B.GPIO Configuration
I defined an IO for an LED light, using macro definition (for easy modification):
#define LED_GPIO_PORT GPIOD
#define LED_GPIO_PIN GPIO_PIN_4
The initial configuration is push-pull high-speed output, and the output default value is low:
GPIO_Init(LED_GPIO_PORT, (GPIO_Pin_TypeDef)LED_GPIO_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
There are several specific configuration parameters:
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;
The basic knowledge above has already talked about the types of GPIO input and output modes: Output classification: push-pull output and open-drain output. Push-pull output has output drive capability and is more common. Open-drain output has no output capability and is used in special occasions, such as the DATA bus of I2C. The specific meanings of these two outputs can be searched online.
C. Specific implementation functions
The while in the main function is the specific function implemented by the source code of this article, which outputs an LED light (IO) alternately high and low to achieve the effect of LED turning on and off.
Code:
while(1)
{
LED_ON; //LED亮
SoftwareDelay(0x6000);
LED_OFF; //LED off
SoftwareDelay(0x6000);
}
There is no need to explain the SoftwareDelay function here, it mainly delays the software for a period of time.
Well, I hope the above basic content will be helpful to you as a beginner.
Ⅴ. Download
STM8S information:
http://pan.baidu.com/s/1o7Tb9Yq
Software engineering source code (STM8S-A01_GPIO basics):
http://pan.baidu.com/s/1c2EcRo0
Previous article:STM8S_002_TIM precise delay (blocking)
Next article:STM8S learning GPIO operation
- 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
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- TI's crystal-free SimpleLink wireless MCU helps you easily achieve crystal-free
- Open source sharing of a 4-axis brushless motor FOC control board project
- DM8148 development board compilation problem
- How to replace potentiometers with programmable devices in analog circuit design?
- BMW Cooling System and Electric Coolant Pump Components (Electronic Water Pump) Functional Characteristics and Standards
- Inline instructions for TMS320C66x study notes
- Electric mask solutions
- 【Project Source Code】FPGA-based Ethernet design tutorial and program source code
- [ESP32-Audio-Kit Audio Development Board Review] Part 3: Comments on the Installation of the Development Environment
- Caibo International Customer Service Life Quotes