51 single-chip microcomputer (AT89S52) one-click multi-function (short and long)

Source: Internet
Author: User

Short Press (Press duration less than 3 S) a digital display plus 1, while the flow lamp count plus 1.

Long press (Press duration greater than 3 S), the digital display per MS plus 1, while the flow lamp per Ms count plus 1.

#include<reg52.h>typedef unsigned char uchar;typedef unsigned int uint;#define NOKEY 0#define KEYDOWN 1#define KEYUP 2uchar keystate;sbit key=P2^7;void keyscan(void);uchar timeflag;uchar code table[]={0x80,0xf2,0x48,0x60,0x32,0x24,0x04,0xf0,0x00,0x20,0x10,0x06,0x8c,0x42,0x0c,0x1c};uchar i;void main(void){TMOD=0x10;TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;ET1=1;TR1=1;while(1){if(timeflag){timeflag=0;keyscan();}}}void keyscan(void){static uchar iscontinue=0;P2 |=0x80;switch(keystate){case NOKEY:if(key==0){keystate=KEYDOWN;return;}else{keystate=NOKEY;return;}case KEYDOWN:if(key==0){keystate=KEYUP;return;}else{keystate=NOKEY;return;}case KEYUP:if(key==0){keystate=KEYUP;iscontinue++;if(iscontinue==255)iscontinue=60;if(iscontinue>=60){if(iscontinue%10==0){P2+=1;P1=table[i++];if(i>=16)i=0;}}return;}else{keystate=NOKEY;if(iscontinue<60){P2+=1;P1=table[i++];if(i>=16)i=0;}iscontinue=0;return;}}}void timer1isr() interrupt 3{TH1=(65536-50000)/256;TL1=(65536-50000)%256;timeflag=1;}

 

 

 

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.