STM8S---Long key recognition for external interrupt application

Publisher:数字梦想Latest update time:2018-07-24 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM8 common interrupt instructions

  • Open the total interrupt 

    • _asm("rim");

  • Disable interrupts 

    • _asm("sim");

  • Entering shutdown mode 

    • _asm("halt");

  • Interrupt return 

    • _asm("iret");

  • Waiting for interrupt 

    • _asm("wfi");

  • Software interrupts 

    • _asm("trap");

STM8S common interrupt mapping

Interrupt Mapping Table

When using an interrupt function, you can find the corresponding interrupt vector number in the figure above, and the name of the interrupt function can be customized.

/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices

 * Copyright (c) 2007 STMicroelectronics

 */


typedef void @far (*interrupt_handler_t)(void);


struct interrupt_vector {

    unsigned char interrupt_instruction;

    interrupt_handler_t interrupt_handler;

};


@far @interrupt void NonHandledInterrupt (void)

{

    /* in order to detect unexpected events during development, 

       it is recommended to set a breakpoint on the following instruction

    */

    return;

}


extern void _stext(); /* startup routine */

extern @far @interrupt void EXTI2_Hand_Fun(void);

extern @far @interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void);



struct interrupt_vector const _vectab[] = {

    {0x82, (interrupt_handler_t)_stext}, /* reset */

    {0x82, NonHandledInterrupt}, /* trap */

    {0x82, NonHandledInterrupt}, /* irq0 */

    {0x82, NonHandledInterrupt}, /* irq1 */

    {0x82, NonHandledInterrupt}, /* irq2 */

    {0x82, NonHandledInterrupt}, /* irq3 */

    {0x82, NonHandledInterrupt}, /* irq4 */

    {0x82, EXTI2_Hand_Fun}, /* irq5 */

    {0x82, NonHandledInterrupt}, /* irq6 */

    {0x82, NonHandledInterrupt}, /* irq7 */

    {0x82, NonHandledInterrupt}, /* irq8 */

    {0x82, NonHandledInterrupt}, /* irq9 */

    {0x82, NonHandledInterrupt}, /* irq10 */

    {0x82, TIM1_UPD_OVF_TRG_BRK_IRQHandler}, /* irq11 */

    {0x82, NonHandledInterrupt}, /* irq12 */

    {0x82, NonHandledInterrupt}, /* irq13 */

    {0x82, NonHandledInterrupt}, /* irq14 */

    {0x82, NonHandledInterrupt}, /* irq15 */

    {0x82, NonHandledInterrupt}, /* irq16 */

    {0x82, NonHandledInterrupt}, /* irq17 */

    {0x82, NonHandledInterrupt}, /* irq18 */

    {0x82, NonHandledInterrupt}, /* irq19 */

    {0x82, NonHandledInterrupt}, /* irq20 */

    {0x82, NonHandledInterrupt}, /* irq21 */

    {0x82, NonHandledInterrupt}, /* irq22 */

    {0x82, NonHandledInterrupt}, /* irq23 */

    {0x82, NonHandledInterrupt}, /* irq24 */

    {0x82, NonHandledInterrupt}, /* irq25 */

    {0x82, NonHandledInterrupt}, /* irq26 */

    {0x82, NonHandledInterrupt}, /* irq27 */

    {0x82, NonHandledInterrupt}, /* irq28 */

    {0x82, 



NonHandledInterrupt}, /* irq29 */

};


External interrupt long key recognition related configuration


  STM8S has five interrupt vectors specifically allocated for external interrupt events:


PortA's 5 pins: PA[6:2]

PortB port 8 pins: PB[7:0]

Port C port 8 pins: PC[7:0]

7 pins of PortD: PD[6:0]

8 pins of PortE port: PE[7:0]

  PD7 is the highest priority interrupt source (TLI);


Interrupt IO settings


  Here we select EXTI2 (Port C external interrupt). Then we need to set the interrupt-triggered IO (PC5) to pull-up input or interrupt pull-up input. If the input is suspended, it is easy to be disturbed.


/*PC5 is set as pull-up input*/

void Init_EXTI2_GPIO(void)

{

    PC_DDR &= 0XDF; 

    PC_CR1 &= 0XDF;

    PC_CR2 |= 0x20;

}


External interrupt register configuration


CPU CC register interrupt bit:


  I1 and I0 cannot be written directly, but can only be written by turning on or off interrupts. The default value is 11 when powered on. When the interrupt is turned on by instruction (_asm("rim\n");), it is 00. When an interrupt occurs, the current interrupt (ITC_SPRx) is loaded into I[1:0], which is mainly used for interrupt priority. Exiting the interrupt automatically clears it to 0. Therefore, when writing EXTI_CR1, ITC_SPRx needs to be configured to 11, or an interrupt disable instruction needs to be added.


EXTI_CR1:


  Configure triggering methods;


Test code


#include


char keyFlag;       

char keyPressStatus = 1;

unsigned int keyCount;      


/*Output Pin*/

_Bool PD2 @PD_ODR:2;

_Bool PC7 @PC_ODR:7;

_Bool PC6 @PC_ODR:6;

_Bool PC3 @PC_ODR:3;

/*Input Pin*/

_Bool PC5 @PC_IDR:5;


/*battery indicator*/

#define LED1 PD2

#define LED2 PC7

#define LED3 PC6

#define LED4 PC3

/*button*/

#define KEY PC5



/*The main clock frequency is 8Mhz*/

void Init_CLK(void)

{

    CLK_ICKR |= 0X01; //Enable internal high-speed clock HSI

    CLK_CKDIVR = 0x08; //16M internal RC is divided by 2 and the system clock is 8M

    while(!(CLK_ICKR&0x02)); //HSI is ready 

    CLK_SWR=0xE1; //HSI is the main clock source 

}


void Init_LED(void)

{

    /*LED configured as push-pull output*/

    PD_DDR |= 0X04; //PD2 output mode, 0 is input mode

    PD_CR1 |= 0X04; //PD2 analog open drain output

    PD_CR2 &= 0XFB; //PD2 output speed is up to 2MHZ, CR1/CR2 floating input


    PC_DDR |= 0XC8;

    PC_CR1 |= 0XC8;

    PC_CR2 &= 0X27;

}


/*PC5 is set as pull-up input*/

void Init_EXTI2_GPIO(void)

{


    PC_DDR &= 0XDF; 

    PC_CR1 &= 0XDF;

    PC_CR2 |= 0X20;

}


void Init_EXTI2(void)

{

    EXTI_CR1 |= 0x30; // rising edge and falling edge trigger

}



void Init_TIM1(void)

{

    TIM1_IER = 0x00;

    TIM1_CR1 = 0x00;


    TIM1_EGR |= 0x01;

    TIM1_PSCRH = 199/256; // 8M system clock is pre-divided by f=fck/(PSCR+1) TIM1 is a 16-bit divider 

    TIM1_PSCRL = 199%256; // PSCR=0x1F3F, f=8M/(200)=40000Hz, each counting cycle is 1/40000ms


    TIM1_CNTRH = 0x00;

    TIM1_CNTRL = 0x00;      


    TIM1_ARRH = 400/256; // Automatic reload register ARR=400

    TIM1_ARRL = 400%256; // Generate an interrupt every 400 counts, i.e. 10ms


    TIM1_CR1 |= 0x81;

    TIM1_IER |= 0x01;

}


unsigned int Key_Scan_Test(void)

{

    unsigned int count = 0;

    unsigned char keyMode;


    if(0 == keyPressStatus)

    {

        keyFlag = 1;

        while(!keyPressStatus);

        keyFlag = 0;

        count = keyCount;

        keyCount = 0;

    }

    else

    {

        count = 0;

    }

    /*10ms * 150 = 1.5s*/

    if(count >= 150)keyMode = 2; //Long press

    else if(count >= 4)keyMode = 1; //short press

    else keyMode = 0; //shake


    return keyMode;

}


main()

{  

    char keynum = 0;


    _asm("sim");

    Init_CLK();

    Init_LED();

    Init_EXTI2_GPIO();

    Init_EXTI2();

    Init_TIM1();

    _asm("rim");

    while (1)

    {

        keynum = Key_Scan_Test();

        if(1 == keynum)LED3 = ~LED3;

        if(2 == keynum)LED4 = ~LED4;

    }

}


@far @interrupt void EXTI2_Hand_Fun(void)

{

    keyPressStatus = !keyPressStatus;

    LED1 = ~LED1;

}


@far @interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)

{

    static unsigned int i = 0;


    TIM1_SR1 &= ~(0X01);


    ++i;

    if(50 == i)

    {

        LED2 = ~LED2;

        i = 0;

    }


    /*Within Key Press Hand*/

    if(1 == keyFlag)

    {

        ++keyCount;

    }

}


Notice:

The interrupt vector needs to be declared. Add the following to stm8_interrupt_vector.c:

extern @far @interrupt void EXTI2_Hand_Fun(void);

extern @far @interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void);

{0x82, EXTI2_Hand_Fun}, /* irq5 */

{0x82, TIM1_UPD_OVF_TRG_BRK_IRQHandler}, /* irq11 */


  See also Long key recognition without external interrupt: Recognize long key without external interrupt


Keywords:STM8S Reference address:STM8S---Long key recognition for external interrupt application

Previous article:STM8S's pitfalls in setting the highest TIM frequency
Next article:STM8S timer basic interrupt timing

Recommended ReadingLatest update time:2024-11-16 13:00

Solution to the STM8S bootloader interrupt vector redirection INTVEC exceeds maximum size problem
I recently reinstalled the system and then reinstalled the IAR for STM8 software (because I couldn't find the previous installation software, I used the IAR installation package provided by the Sabretooth Development Board). I found that the bootloader project file that was previously compiled normally had the followi
[Microcontroller]
Solution to the STM8S bootloader interrupt vector redirection INTVEC exceeds maximum size problem
STM8S Window Watchdog
The .h file is as follows:   #ifndef __WWDG_H #define __WWDG_H #include "stm8s.h" void Delay();  void WWDG_Configuration(void) ;   void Refresh_WWDG_Window(void); #endif The .c file is as follows: #include "wwdg.h" #include "stm8s_wwdg.h"   #define CounterInit 0x7f #define window      0x77   void Delay() //delay
[Microcontroller]
Punctual Atom on STM8S Precise Delay Function
I have finished writing the system files for STM8S (same as stm32, including sys.c, delay.c and uart.c). Here I will share delay.c and delay.h. I wanted to imitate STM32 and use a timer, but STM8S provides an 8-bit timer, which is really tasteless... After all the calculations, it is not easy to use this 8-bit timer
[Microcontroller]
STM8S hardware IIC card is busy, what should I pay attention to?
I won't post the specific code. There are many examples in 21IC, but I couldn't get it to work when I applied it to my own project. I gave up and used analog IIC instead. Today I tried for a long time and it worked. I specially wrote this article to commemorate it and help people who have the same confusion. 2. T
[Microcontroller]
Design of serial communication between STM8S microcontroller and smart phone via Bluetooth
Serial communication has a long history and is the most basic and simplest communication method. Even in the current era of mobile and wireless interconnection, serial communication is indispensable. For example, for wireless transmission of small data volumes, although the physical link uses Bluetooth or WIFI for tra
[Microcontroller]
Design of serial communication between STM8S microcontroller and smart phone via Bluetooth
Using STM8S internal clock to generate PWM (TIM2)
  1 Description   Use STM8S internal clock (HSI);   PWM mode 2;   The duty cycle is 50% and the frequency is 2Hz (convenient for testing LED lights);   PD2 port is connected to an external LED light, and PD2 port outputs PWM wave;   System clock initialization is very important: CLK_CKDIVR |= 0x08;   2 Code
[Microcontroller]
Using STM8S internal clock to generate PWM (TIM2)
Analysis of the principle of STM8S's universal asynchronous receiver and transmitter UART
Serial communication is one of the most basic and important functions of microcontroller learning. Serial communication can be used indirectly as a debugging interface to achieve communication between the microcontroller and the computer. Of course, it can communicate with some modules (such as Bluetooth, WiFi), and c
[Microcontroller]
Analysis of the principle of STM8S's universal asynchronous receiver and transmitter UART
The process and method of STM8S microcontroller development
The STM8S MCU development environment consists of three parts, including the development software on the PC (integrated development software, driver, etc.), the debugger & programmer ST-LINK, and the target MCU circuit board. As shown in the figure below: 1. Develop software on PC The software on the PC includes: ST
[Microcontroller]
The process and method of STM8S microcontroller development
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号