Small problem of STM8s using library to configure port

Publisher:温馨阳光Latest update time:2020-09-23 Source: eefocusKeywords:STM8s Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  When used, the PA2 port needs to be set to push-pull output to control an external power switch. The port initialization procedure is as follows:

    GPIO_DeInit(GPIOA);

    GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_OUT_PP_HIGH_SLOW);

  After the setting is completed, the port will immediately output a high level, so another sentence is added:

    GPIO_WriteLow(GPIOA,GPIO_PIN_2);

  After completion, it was found that the powered device would be triggered when it was powered on for the first time, and the program did not output a high level on PA2. This powered device is equipped with a power supply battery and will start if a high level is detected on the port.

Using an oscilloscope to observe the PA2 port, I found that there was a short pulse at the moment of power-on, which seemed to be the problem. I checked the source code of the library function:

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_Mode_TypeDef GPIO_Mode)
{

  assert_param(IS_GPIO_MODE_OK(GPIO_Mode));
  assert_param(IS_GPIO_PIN_OK(GPIO_Pin));

  /* Reset corresponding bit to GPIO_Pin in CR2 register */
  GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin));

  /*-----------------------------*/
  /* Input/Output mode selection */
  *-----------------------------*/

  if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x80) != (uint8_t)0x00) /* Output mode */
  {
    if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x10) != (uint8_t)0x00) /* High level */
  {
    GPIOx->ODR |= (uint8_t)GPIO_Pin;
  }
  else /* Low level */
  {
    GPIOx->ODR &= (uint8_t)(~(GPIO_Pin));
  }
  /* Set Output mode */
    GPIOx->DDR |= (uint8_t)GPIO_Pin;
  }
  else /* Input mode */
  {
    /* Set Input mode */
    GPIOx->DDR &= (uint8_t)(~(GPIO_Pin));
  }

/*------------------------------------------------------------------------*/
/* Pull-Up/Float (Input) or Push-Pull/Open-Drain (Output) modes selection */
/*------------------------------------------------------------------------*/

  if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x40) != (uint8_t)0x00) /* Pull-Up or Push-Pull */
  {
    GPIOx->CR1 |= (uint8_t)GPIO_Pin;
  }
  else /* Float or Open-Drain */
  {
    GPIOx->CR1 &= (uint8_t)(~(GPIO_Pin));
  }

  /*-----------------------------------------------------*/
  /* Interrupt (Input) or Slope (Output) modes selection */
  /*-----------------------------------------------------*/

  if ((((uint8_t)(GPIO_Mode)) & (uint8_t)0x20) != (uint8_t)0x00) /* Interrupt or Slow slope */
  {
    GPIOx->CR2 |= (uint8_t)GPIO_Pin;
  }
  else /* No external interrupt or No slope control */
  {
    GPIOx->CR2 &= (uint8_t)(~(GPIO_Pin));
  }
}

  From the source code, CR2 is cleared first. Then write the DDR direction register 1 for output, 0 for input, and then CR1. This can be used with DDR to determine 4 modes: when DDR is 0, CR1 is 0 for floating input, when CR1 is 1 for pull-up input, when DDR is 1, CR1 is 0 for open-drain output, and when CR1 is 1 for push-pull output. When DDR is 0, CR1 is the switch for external interrupts, 1 is on, 0 is off, and when DDR is 1, it controls the slew rate. If a steeper edge is required, it can be set to 1.

  I don't know why this happens. Hardware simulation found that CR1 will be pulled high after it is set. Later, I used the register method to set it normally. The code is as follows:

  GPIOA->CR2 |= 0x04;
  GPIOA->DDR |= 0x04;
  GPIOA->CR1 |= 0x04;


Keywords:STM8s Reference address:Small problem of STM8s using library to configure port

Previous article:STM8s window watchdog
Next article:STM8S103F3--EEPROM

Recommended ReadingLatest update time:2024-11-16 12:26

STM8S AD conversion
//Not much to say, let's go straight to the program void ADC1_DeInit(void) {     ADC1- CSR  = ADC1_CSR_RESET_VALUE;     ADC1- CR1  = ADC1_CR1_RESET_VALUE;     ADC1- CR2  = ADC1_CR2_RESET_VALUE;     ADC1- CR3  = ADC1_CR3_RESET_VALUE;     ADC1- TDRH = ADC1_TDRH_RESET_VALUE;     ADC1- TDRL = ADC1_TDRL_RESET_VALUE;     A
[Microcontroller]
stm8s development (nine) Use of EEPROM: Use EEPROM to store data!
EEPROM is a memory often used in microcontroller application systems. It is mainly used to save some data that needs to remain unchanged after power failure. In previous microcontroller systems, an EEPROM chip was usually added outside the microcontroller. This method not only increases costs, but also reduces reliabi
[Microcontroller]
stm8s development (V) Use of TIMER: timing!
STM8S provides three types of TIM timers: advanced control type (TIM1), general type (TIM2/TIM3/TIM5) and basic timer (TIM4/TIM6). Although they have different functions, they are all based on a common architecture. This common architecture makes it very easy and convenient to design applications using various timers
[Microcontroller]
stm8s development (V) Use of TIMER: timing!
stm8s: GPIO configuration, light up the LED! PB4, PB5 pin LEDs do not light up
I have been developing STM8S103 for the past two days. After adjusting the hardware, I started to write the software. The first step was to light up the LED light. It turned out to be embarrassing that it just wouldn't light up! Later, I searched Baidu and carefully checked the data sheet, only to find a big pitfall.
[Microcontroller]
stm8s: GPIO configuration, light up the LED! PB4, PB5 pin LEDs do not light up
STM8S---Long key recognition for external interrupt application
STM8 common interrupt instructions Open the total interrupt  _asm("rim"); Disable interrupts  _asm("sim"); Entering shutdown mode  _asm("halt"); Interrupt return  _asm("iret"); Waiting for interrupt  _asm("wfi"); Software interrupts  _asm("trap"); STM8S common interrupt mapping When using a
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号