Use the button as an interrupt trigger. When the button is pressed once, the LED light flips once.
First, initialize the IO port. The button is connected to the PC4 port. By default, it is at a high level, and it is at a low level when the button is pressed.
The initialization code is as follows:
void EXTI_GPIO_Init( void )
{
PC_DDR &= ~( 1 << 4 ); //PC4 input
PC_CR1 |= ( 1 << 4 ); //Input with pull-up resistor
PC_CR2 |= ( 1 << 4 ); // Enable external interrupt
}
Set PC4 port as input port, select pull-up resistor input, and make the IO port high level by default. Since the interrupt function is to be used, external interrupt is allowed.
Let's take a look at the interrupt related registers:
Through the interrupt mapping table, we can see the IO port interrupt of STM8. One IO port has only one interrupt source, that is, the interrupt source of the PC4 port of the key is the port C external interrupt. Let's take a look at the interrupt setting register:
There are 5 interrupt-related registers. Since there is only one key interrupt, there is no need to set the priority level. Just set the interrupt control register.
To set the PC port to low level trigger, set bits 4 and 5 of the register to 0.
Interrupt setting only requires setting one register. The interrupt initialization code is as follows:
void EXTI_Init( void )
{
EXTI_GPIO_Init();
EXTI_CR1 &= ~( 3 << 4 ); // 4 5 bits are cleared 01 is PA, 23 is PB, 45 is PC, 67 is PD pin
EXTI_CR1 |= ( 0 << 4 ); //PC rising edge trigger 00 is falling edge trigger 01 rising edge 10 falling edge 11 rising edge and falling edge
}
In order to facilitate calling in the main program, the IO port initialization and interrupt register initialization are placed in one function.
First, call the IO port initialization. After the IO port initialization is completed, set the external interrupt control register 1. The button is at the PC4 port, so first clear the PC port setting bit to 0, and then set the trigger mode. We are low-level triggering, so the 4th bit is set to 0.
After the initialization is completed, the next step is to write the interrupt program. Since the PC4 port does not have a separate interrupt entry, the PC port interrupt source is used. That is to say, a low level or a falling edge on any PC port will trigger the PC interrupt source. Therefore, when an interrupt occurs, the level of the PC4 port must be judged in the interrupt function to confirm that the interrupt is triggered by the PC4 port.
The interrupt code is as follows:
#pragma vector = 7 // The interrupt number in IAR needs to be increased by 2 to the interrupt number in STVD
__interrupt void EXTI_PORTC_Handle( void )
{
if( EX_INT == 0 )
{
LED = !LED;
}
}
The PC4 port uses bit operations in the interrupt, which are defined in the header file:
#ifndef __EXTI_H
#define __EXTI_H
#include "iostm8s103F3.h"
#define EX_INT PC_IDR_IDR4 //Define PC4 as interrupt input
void EXTI_GPIO_Init( void );
void EXTI_Init( void );
#endif
Enter the PC interrupt service program. If the level of PC4 port is 0 at this time, it means that the button is pressed, so the LED light status is inverted.
If your hands shake when you press a key, the interrupt may be triggered multiple times. To avoid this, you can add a filter capacitor to the key IO port to filter out the burrs generated when pressing the key.
Then, can we add a delay function to the button like in the button experiment, and also in the interrupt, if the PC4 port is at a low level, delay for a period of time and then judge the level of the PC4 port? This is possible, but it is generally not recommended. Because the interrupt program executes as fast as possible, if a delay is added to the interrupt, it will affect the execution speed of the main program. If interrupts occur frequently, more than half of the program will wait in the delay, which will seriously affect the efficiency of program execution. If another interrupt occurs during the waiting delay after entering the interrupt, the interrupt will continue to be triggered, forming a nested interrupt. Each time the interrupt microcontroller is interrupted, stack space must be opened. If there are too many nested interrupts, more stack space must be opened, which may lead to insufficient space inside the microcontroller and cause program exceptions. So in general, the less code in the interrupt function, the better, and the faster the code execution speed, the better.
The interrupt service routine is automatically executed after an interrupt occurs, so the main program only needs to be initialized. The main program code is as follows:
#include "iostm8s103F3.h"
#include "led.h"
#include "exti.h"
void SysClkInit( void )
{
CLK_SWR = 0xe1; //HSI is the main clock source 16MHz CPU clock frequency
CLK_CKDIVR = 0x00; //CPU clock divided by 0, system clock divided by 0
}
void main( void )
{
SysClkInit(); //Clock initialization
__asm( "sim" ); //Disable interrupts
LED_GPIO_Init(); //LED initialization
EXTI_Init(); //External interrupt initialization
__asm( "rim" ); // Enable interrupt
LED = 0;
while( 1 )
{
}
}
Since there is only one interrupt entry for a group of IO ports of the STM8 microcontroller, if there are multiple interrupt sources externally, it is best to set them in different groups of IO ports, so that the program processing will be more convenient.
Previous article:STM8 study notes---KEY
Next article:The MCU crashes unexpectedly, resets unexpectedly, or the program runs away
Recommended ReadingLatest update time:2024-11-15 07:23
- Popular Resources
- Popular amplifiers
- STM8 C language programming (1) - basic program and startup code analysis
- Description of the BLDC back-EMF sampling method based on the STM8 official library
- STM32 MCU project example: Smart watch design based on TouchGFX (8) Associating the underlying driver with the UI
- Introduction to Artificial Intelligence and Robotics (Murphy)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Does anyone have a good thermostat switch?
- Ultra-low power MCU-How to reduce the power consumption of MCU
- [MM32 eMiniBoard Review] Part 4: Questions about multi-channel ADC
- Modbus_RTU (RS485) Fieldbus Remote Inputs/Outputs Modules System Design Based on GD32E231
- Particle Xenon nRF52840 BLE Development Board
- Technical Science: The Essential Differences between DRAM and NAND
- Sugar glider ③ RSL10-SENSE-GEVK light sensor driver is written and shared with everyone
- When using the stc8 series for ADC multi-channel sampling, the sampling data of one channel is sometimes accurate and sometimes wrong.
- STM32F205 series MCU OTP problem
- 《GaN Transistors for Efficient Power Conversion》