STM8S_001_GPIO basics

Publisher:科技火箭Latest update time:2017-09-16 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Ⅰ. 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


Keywords:STM8S Reference address:STM8S_001_GPIO basics

Previous article:STM8S_002_TIM precise delay (blocking)
Next article:STM8S learning GPIO operation

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号