STM8 study notes---external interrupt implementation

Publisher:温馨小屋Latest update time:2019-12-27 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

insert image description here

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:

insert image description here

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.

insert image description here

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.

Keywords:STM8 Reference address:STM8 study notes---external interrupt implementation

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

STM8 serial UART debugging record
  Originally, UART debugging is very simple, but it took me several days to debug on STM8. I will record it here, hoping that friends who encounter the same problem can avoid it, work smoothly and live happily!        Problem Description:   Using STM8's UART1, baud rate 9600, send interrupt disabled, receive interru
[Microcontroller]
IIC protocol based on STM8--Example--Clock module (DS3231) reading
1. Overview   From the previous blog, you can know how to implement the IIC protocol with code. This blog does not involve the content of the protocol, but only explains how to use it.   The sensor for this experiment is: DS3231 (clock module). I will not introduce the specific information of the clock module. You
[Microcontroller]
IIC protocol based on STM8--Example--Clock module (DS3231) reading
Build STM8 development environment under Linux
Use SDCC+STM8Flash+STLink to build Linux development STM8 development environment. Corresponding MCS51, LPC and the like can also use SDCC 1. Install SDCC $ sudo apt-get install sdcc 2. Install stm8flash git clone https://github.com/vdudouyt/stm8flash.git cd stm8flash make sudo make install 3. Install STlink dri
[Microcontroller]
stm8 duty cycle measurement of motor speed and display on 1602 LCD display process debugging
/******************************************************************* Experiment name and content: Speed ​​display LCD screen Supporting book: "Introduction to STM8 MCU, Advanced and Application Examples" Experimental platform and programmer: hardcore novice / #include "iostm8s208mb.h" //header file /port/pin defini
[Microcontroller]
stm8 duty cycle measurement of motor speed and display on 1602 LCD display process debugging
STM8 library function development manual (2) //Interrupt, timer 4
Part 1 Interrupt Controller Library itc.c 1.ITC_GetCPUCC() //Read CC register u8 u8_value;u8_value = ITC_GetCPUCC(); 2.ITC_DeInit() //Restore ITC related registers to default values 3. ITC_GetSoftIntStatus() //Returns the value of the software interrupt priority bit (I1, I0) in the CC register u8 u8_value;u8_
[Microcontroller]
What is the MVR inside STM8?
The power of STM8, MVR is the internal main voltage regulator,
[Microcontroller]
What is the MVR inside STM8?
stm8 I/O port mode configuration
Default configuration after reset: After reset, all pins are in floating input mode. However, a few pins may have a different behavior. Refer to the datasheet pinout description for all details. When the pin is used as a regular IO port, it can be directly configured as input or output mode: for example, PA0 is conf
[Microcontroller]
The same STM8 project generates inconsistent HEX checksums after changing the directory location
When using IAR FOR STM8 for 003 development, I found that the same project and the same file, after being copied to another directory, the HEX generated by recompilation is inconsistent with the original one. This is because there is directory information at the end of the HEX file, so the checksum will change when th
[Microcontroller]
The same STM8 project generates inconsistent HEX checksums after changing the directory location
Latest Microcontroller Articles
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号