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.
Previous article:stm8s development (I) Create a new project using IAR
Next article:STM8 controls computer fan speed 4PIN PWM
- Popular Resources
- Popular amplifiers
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
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- What to do if the Quartus II driver is incompatible with the laptop blue screen?
- Working principle and function of encoder
- Could you please tell me what is the function of DDB file in PROTEL99? Are sch files and PCB files placed in the DDB file?
- NIWEEK is now NI Connect, join us online! Sign up now
- 【AT-START-F425 Review】Virtual Serial Port Test
- First common vulnerability discovered in LoRaWAN protocol stack
- ad How to draw a large area connection PCB diagram
- STM32G4 is here, do you still think the number of ADCs is not enough?
- Temperature, humidity and air pressure detection function of pedometer wristband based on F103 and X-NUCLEO-IKS01A3
- Electronic Components Search