stm8s---Independent key IO port setting and pressing event issues

Source: Internet
Author: User

    • Gpio settings
    • Key detection
      • 1 continuous key detection short press long press
    • Key code

1 Gpio settings

???????????????? STM8 I/O port pin configuration table

px_ddr PX_CR1 Px_cr2 I/O mode PIN Status
0 0 0 Input Floating input
0 0 1 Input Pull-up input
0 1 0 Input Interrupt Suspension Input
0 1 1 Input Interrupt Pull-up input
1 0 0 Output Open-Drain output
1 1 0 Output Push-Pull output
1 X 1 Output Output (fastest speed is 10MHZ)

For the IO configuration of stm8s, we only need to operate five registers on the line:

    • Output Data register (ODR)
    • Input Data Register (IDR)
    • Data Direction Register (DDR)
    • Control register 1 (CR1)
    • Control Register 2 (CR2)
2 Key detection

?? When doing independent key detection, set to pull up input can not realize the function, set to interrupt the suspension input can be.
?? The time interval between two short keys is approximately between 300ms~600ms. The time of a short button press is approximately between 14ms~26ms;

2.1 Continuous key detection (short press + long Press)

Timer TIM1 + key = Continuous key detection (short button + Long button)
Two tags:

    • Short press + Long press-FLAG0
    • Short press after time within the specified range-FLAG1

If all two markers are met, the power on/off is on/off, and the key is activated each time;

How do I get the time between the first (short press) and the second (long Press) button when the time interval between 300ms~600ms is two times?
?? If a short press is judged, the timing is turned on, and the first short press flag is set one, more than 600ms stop time and clear, wait for the second key; after the second keystroke, the timer is interrupted in the short-hit flag condition, and the time interval within the specified range is determined, and the Flag1 is placed , and then determine whether the button is a long press or short press, if it is a long press, then the FLAG0 will be set to meet the FLAG0, Flag1 are placed one, is a continuous key.

3 Key Codes
/*return = 0:no key Presses 1:single Key Press 2: Push and hold for a single key press function 3 : Double Key press*/UnsignedintKey_scan (void) {unsignedint Count=0;if(0= = KEY) {Delay (2);if(0= = KEY) {if(1= = Keytimesflag) {Afteronceshortpressflag =0;if((Afteronceshortpresscount <= -) && (Afteronceshortpresscount > the) {Issettimeflag =1; }ElseIssettimeflag =0; } Keyflag =1; while(!            KEY); Keyflag =0;Count= Keycount; Keycount =0; }Else{Count=0; }    }if(Count>= $)    {if(1= = Issettimeflag) {Issettimeflag =0; Keytimesflag =0;return 3; }Else{Keytimesflag =0;return 2; }    }Else if(Count>=4) {Afteronceshortpressflag =1; Afteronceshortpresscount =0; Keytimesflag =1;return 1; }Else return 0;}

In a timer interrupt function with a timer period of 10ms:

@far @interrupt voidTim1_upd_ovf_trg_brk_irqhandler (void) {TIM1_SR1 &=~ (0x01);if(1= = Keyflag) {++keycount; }Else;if(1= = Afteronceshortpressflag) {++afteronceshortpresscount;if(Afteronceshortpresscount > the) {Afteronceshortpressflag =0; Afteronceshortpresscount =0; Keytimesflag =0; }    }Else;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

stm8s---Independent key IO port setting and pressing event issues

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.