In the previous articles, the use of GPIO: Light up the LED! only described the output of the IO port, not the input. Because I recommend using the interrupt method for IO as input, which can greatly reduce the CPU running time. The most commonly used function is the button function, of course, there is also the function of communicating with other chips.
The previous article mentioned that GPIO can be set as interrupt input through registers.
Configuration mode DDRCR1CR2 Configuration mode pull-up resistor
Input 000 Suspension input OFF
010 Pull-up input ON
001 Interrupt suspension input OFF
011 Interrupt pull-up input ON
Output 100 Open drain output OFF
110 push-pull output
1x1 output (maximum speed 10MHZ)
xxxTrue open-drain output is not used
Here we choose PC3 as the external interrupt
void EXIT_GPIO_Init(void)
{
PC_DDR &= 0xf7;
PC_CR1 |= 0x08; // Set PC3 to interrupt pull-up input
PC_CR2 |= 0x08;
EXTI_CR1 = 0x20; //PC is set to falling edge trigger
}
It should be noted here that the interrupt vector specifies the PC, that is, if several PC ports are set as interrupts, as long as one of the interrupts is responded to, the same function will be entered.
If you have several buttons on the same port, you can get the status of an IO port after entering the interrupt function, determine which IO is interrupted, and then perform corresponding operations.
#pragma vector = EXIT_PORTC_vector //0x07
__interrupt void EXIT_PORTC_RQHandler(void) //Interrupt service function
{
if((PC_IDR & 0x08)==0)
{
delay_nms(50); //delay 50ms, debounce
if((PC_IDR & 0x08)==0) // Check if it is pressed
{
while(!(PC_IDR & 0x08)); // Release detection
//ALL
}
}
}
Since mechanical buttons are used, there will generally be a mechanical jitter process, so after entering the interrupt function, we first do a jitter elimination process, so that we can determine the state of the button, and also prevent jitter from causing repeated interrupt functions. Finally, add a release detection to determine that the button action is completed. (TODO here you can add the operations you need)
Attached is the project of stm8s in IAR environment, including the initialization code of on-chip hardware such as SPI, IIC, PWM, AWU, USART, EEPROM, etc.
http://download.csdn.net/detail/devintt/9454188
Previous article:stm8s development (eight) use of IIC: IIC host communication!
Next article:STM8S external interrupt keeps going
Recommended ReadingLatest update time:2024-11-23 19:17
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Zarlink launches ultra-small DTV tuner with "all-standards" compatibility
- Simple and practical TDA2822M integrated power amplifier
- Has anyone used the LTC2941 single-cell fuel gauge? Is the ground terminal of the LTC2941 connected to the negative terminal of the battery B- or the GND of the circuit board?
- [NXP Rapid IoT Review] +3. Rapid IoT Studio online IDE Test 1 - BLE control indicator
- PIC16C7X A/D Converter Control Scheme
- TO220 package and heat sink fixing problem
- Design and implementation of a controller for a real vehicle collision speed control system
- The Road to OO System Designer--Analysis Model Series (2)--How to Make an Analysis Model
- First experience with the domestic RISC-V development board RV-STAR
- Design of software and hardware C delay program for MSP430