stm8s---Long key recognition for external interrupt applications

Source: Internet
Author: User

STM8 Common Interrupt Instructions
    • Open Total Interrupt
      • _asm ("rim");
    • Disable interrupts
      • _asm ("Sim");
    • Enter down mode
      • _asm ("Halt");
    • Interrupt return
      • _asm ("Iret");
    • Wait for interrupt
      • _asm ("WFI");
    • Software interrupts
      • _asm ("trap");
Stm8s Common Interrupt Mappings

如使用中断函数时,可以通过在中查找相对应的中断向量号,而中断函数的名字可以自定义

/* BASIC INTERRUPT VECTOR TABLE for STM8 devices * Copyright (c) STMicroelectronics */typedef void @far(*interrupt_handler_t) (void);structinterrupt_vector {unsignedCharInterrupt_instruction; interrupt_handler_t Interrupt_handler;};@far @interrupt voidNonhandledinterrupt (void){/ * In order to detect unexpected events during development, it's recommended to set a breakpoint on the follow ing instruction * *    return;}extern void_stext ();/ * Startup routine * /extern @far @interrupt voidExti2_hand_fun (void);extern @far @interrupt voidTim1_upd_ovf_trg_brk_irqhandler (void);structInterrupt_vectorConst_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 identification related configuration

?? The stm8s specifically allocates five interrupt vectors for external interrupt events:

    • 5 Pins for PortA port: Pa[6:2]
    • 8 Pins for PORTB port: pb[7:0]
    • 8 Pins for PORTC port: pc[7:0]
    • 7 Pins for PORTD port: pd[6:0]
    • 8 Pins for Porte port: pe[7:0]

?? PD7 is the highest-priority interrupt source (TLI);

Interrupt IO Settings

?? EXTI2 (external interrupt on port c) is selected here. Then it is necessary to set the interrupt-trigger IO (PC5) to the pull-up input or interrupt the pull-up input, which is easily disturbed by the hover input.

/*PC5设置为上拉输入*/void Init_EXTI2_GPIO(void){    0XDF;     0XDF;    0x20;}
External Interrupt Register Configuration
CPU CC寄存器中断位:

?? I1? I0 can not write directly, can only be written by open interrupt or shutdown interrupt, power on default is 11, when the interrupt with the command (_asm ("rim\n");), is 00; When an interrupt occurs, the current interrupt (ITC_SPRX) is loaded into i[1:0], mainly used to do the interrupt priority; Exit interrupt Auto-Clear 0 Therefore, in writing EXTI_CR1, you need to configure the ITC_SPRX to 11, or join the no interrupt command.

EXTI_CR1:

?? Configure the way to promote the hair;

Test code
#include <stm8s003f3p.h>CharKeyflag;CharKeypressstatus =1;unsigned intKeycount;/*output pin*/_boolPD2 @PD_ODR:2;_boolPC7 @PC_ODR:7;_boolPC6 @PC_ODR:6;_boolPC3 @PC_ODR:3;/*input pin*/_boolPC5 @PC_IDR:5;/ * Battery indicator * /#define LED1 PD2#define LED2 PC7#define LED3 PC6#define LED4 PC3/ * key * /#define KEY PC5/ * Main clock frequency is 8mhz*/voidINIT_CLK (void) {Clk_ickr |=0x01;//Enable internal high-speed clock HSICLK_CKDIVR =0x08;//16m Internal RC via 2, System clock is 8M     while(! (clk_ickr&0x02));//hsi ready .Clk_swr=0xe1;//HSI Primary Clock source}voidInit_led (void){/*led configured for push-pull output * /Pd_ddr |=0x04;//pd2 Output mode, 0 for input modePD_CR1 |=0x04;//PD2 analog Open-drain outputPd_cr2 &=0XFB;//pd2 output speed up to 2MHZ,CR1/CR2 suspension inputPc_ddr |=0XC8; PC_CR1 |=0XC8; Pc_cr2 &=0X27;}/*PC5 set to pull-up input * /voidInit_exti2_gpio (void) {pc_ddr &=0XDF; PC_CR1 &=0XDF; Pc_cr2 |=0X20;}voidInit_exti2 (void) {EXTI_CR1 |=0x30;//rising edge and falling edge to promote hair}voidINIT_TIM1 (void) {Tim1_ier =0x00; TIM1_CR1 =0x00; Tim1_egr |=0x01; TIM1_PSCRH =199/ the;//8M system clock f=fck/(pscr+1) TIM1 16-bit dividerTIM1_PSCRL =199% the;//pscr=0x1f3f,f=8m/(=40000hz), each counting cycle 1/40000msTIM1_CNTRH =0x00; Tim1_cntrl =0x00; Tim1_arrh = -/ the;//Auto Reload Register arr=400TIM1_ARRL = -% the;//400 times per number of interrupts, i.e. 10msTIM1_CR1 |=0x81; Tim1_ier |=0x01;}unsigned intKey_scan_test (void){unsigned intCount =0;unsigned CharKeymode;if(0= = Keypressstatus) {Keyflag =1; while(!keypressstatus); Keyflag =0;        Count = Keycount; Keycount =0; }Else{count =0; }/*10ms * = 1.5s*/    if(Count >= Max) Keymode =2;//Long Press    Else if(Count >=4) Keymode =1;//Short Press    ElseKeymode =0;//Jitter    returnKeymode;} Main () {CharKeynum =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 @interruptvoidExti2_hand_fun (void) {keypressstatus =!keypressstatus; LED1 = ~led1;} @far @interruptvoidTim1_upd_ovf_trg_brk_irqhandler (void){Static unsigned inti =0; TIM1_SR1 &= ~ (0x01); ++i;if( -= = i) {LED2 = ~led2; i =0; }/*within Key Press hand*/    if(1= = Keyflag) {++keycount; }}
注意:中断向量需声明,在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 no external interrupt long key recognition: No external interrupt to identify long keys

Copyright NOTICE: This article for Bo Master [original] article, without BO Master permission can be reproduced, annotated blog Source: [Http://blog.csdn.net/FreeApe]

stm8s---Long key recognition for external interrupt applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.