Use the button as interrupt trigger, press the button once, the LED light will turn over once. Firstly, the IO port is initialized, and the key is connected to the PC4 port. By default, the IO port is high level, and the IO port is low level after the key is pressed. The initialization code is as follows:

void EXTI_GPIO_Init( void ) { PC_DDR &= ~( 1 << 4 ); / / PC4 input PC_CR1 | = (1 < < 4); / / take pull resistance input PC_CR2 | = (1 < < 4); // Allow external interrupts}Copy the code

Set the PC4 port as the input port and select the pull-up resistor input so that the IO port is high by default. External interrupts are allowed because the interrupt function is used.

Let’s look at the interrupt related register:



It can be seen from the interrupt mapping table that the IO port of STM8 is interrupted. Each IO port has only one interrupt source, that is to say, the interrupt source of the PC4 port of the key is the external interrupt of port C. Look at the interrupt Settings register:



There are 5 interrupt related registers, because there is only one key interrupt, so there is no need to set the priority, just set the interrupt control register.



To set the PC port to low level trigger, set bits 4 and 5 of the registers to 0.

Interrupt setting only needs to set a register, interrupt initialization code is as follows:

void EXTI_Init( void ) { EXTI_GPIO_Init(); EXTI_CR1 &= ~( 3 << 4 ); / / 4 five reset 01 is PA, 23 is PB, 45 is PC, 67 is the 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}Copy the code

To make it easy to call in the main program, I/O port initialization and interrupt register initialization are placed in one function. First call IO port initialization, set the external interrupt control register 1 after the IO port initialization, the key is in PC4 port, so first set the bit of PC port to clear 0, then set the trigger mode, we are low level trigger, so 4 and bit is set to 0. After the initialization, interrupt program is down to write, because the PC4 mouth no single interruption entrance, use the PC port interrupt source, that is to say, the PC to any opening a drop in low level or interrupt source along the trigger the PC, so when the interrupt occurs in the interrupt function even when judge the PC4 mouth level, identified as PC4 raised interrupts. The interrupt code is as follows:

__interrupt void EXTI_PORTC_Handle(void) {if(EX_INT == 0) {LED =! LED; }}Copy the code

Interrupt port PC4 uses bit operations, as defined in the header file:

#ifndef __EXTI_H #define __EXTI_H #include "iostm8s103f3. h" #define EX_INT PC_IDR_IDR4 EXTI_GPIO_Init( void ); void EXTI_Init( void ); #endifCopy the code

Enter the PC interrupt service program, if the level of PC4 port is 0 at this time, it means that the key is pressed, so the LED lamp state is reversed. When pressing the button, the hand will shake, so the interrupt may be triggered many times. In order to avoid this situation, a filter capacitor can be added to the IO port of the key to filter out the burr generated when pressing the key. Is it possible to add the delay function to the key in the key experiment, and judge the level of PC4 port after a period of delay if PC4 port is low level in the interrupt? This is ok, but it’s not generally recommended. Because the interrupt program execution faster the better, if interrupt add delay, will affect the main program execution speed. If the interrupt occurs frequently, more than half of the program will wait in the delay, which will seriously affect the program execution efficiency. If an interrupt occurs in the process of waiting for a delay after entering the interrupt, it will continue to trigger the interrupt, forming an interrupt nesting. Each interrupt microcontroller to open up stack space, if the interrupt nesting too much, it is necessary to open up more stack space, may lead to the microcontroller internal space is insufficient, causing program abnormalities. Therefore, in general, the less code in interrupt functions is better, and the faster the code is executed, the better. The interrupt service routine is automatically executed after an interrupt occurs, so only initialization is required in the main program. The main program code is as follows:

#include "iostm8s103F3.h" #include "led.h" #include "exti.h" void SysClkInit( void ) { CLK_SWR = 0xe1; CLK_CKDIVR = 0x00; } void main(void) {SysClkInit(); // Clock initialization __asm("sim"); // disable interrupt LED_GPIO_Init(); // EXTI_Init(); // External interrupt initialization __asm("rim"); // Enable interrupt LED = 0; while( 1 ) { } }Copy the code

Because STM8 microcontroller a group of IO ports only one interrupt entrance, so if there are multiple external interrupt sources, it is best to set up in different groups of IO ports, so that the process will be more convenient.