STM8 MCU - Key detection circuit design

Publisher:Jinyu521Latest update time:2020-09-04 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The hardware environment uses STM8SF103, and the voltage is 3.3V. It should be noted that the IO of the STM8SF103 series can only be pulled up and suspended when used as an input port. Although it is a weak pull-up, it can still be pulled up to about 3.0V when the VCC voltage is 3.3V.


Therefore, one end of the IO port of the key detection circuit needs to be grounded instead of being pulled up externally. In the suspended mode, the IO will still have a voltage of about 0.89V, and the corresponding IO register IDR is still in a logic high state.

The software code is relatively simple, you only need to set the IO port to pull-up input. The polling code is as follows:


// Initialization only requires setting the IO port mode.

GPIO_Init (KEY2_PORT, KEY2_PIN, GPIO_MODE_IN_PU_NO_IT); //Pull-up input, no interrupt

//Key detection part

/*

#defineKEY10

#defineKEY1_PORT(GPIOD)

#defineKEY1_PIN(GPIO_PIN_4)

#defineKEY21

#defineKEY2_PORT(GPIOC)

#defineKEY2_PIN(GPIO_PIN_1)

*/

u8CheckKey(u8key)

{

if(key==KEY1)

{

if (KEY1_PORT->IDR&KEY1_PIN) //When a key is pressed, the IO port voltage will be pulled down

{

delay_5ms();

if (KEY1_PORT->IDR&KEY1_PIN) // low level still exists

{

while(!(KEY1_PORT->IDR&KEY1_PIN)); //Wait for release

returnKEY1;

}

}

return0;

}elseif(key==KEY2)

{

if (! (KEY2_PORT->IDR&KEY2_PIN)) //A key is detected

{

delay_5ms();

if (!(KEY2_PORT->IDR&KEY2_PIN)) // low level still exists

{

while (KEY2_PORT->IDR&KEY2_PIN); //Wait for release

returnKEY2;

}

}

return0;

}

return0;

}


In summary, the hardware circuit grounds one end of the key IO port. The software configuration cannot use the floating input method, as the voltage may be unstable in this way.

Keywords:STM8 Reference address:STM8 MCU - Key detection circuit design

Previous article:stm8s development (I) Create a new project using IAR
Next article:STM8 controls computer fan speed 4PIN PWM

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号