/******************************** Press the Independent button to make the digital tube plus 1********************************/ #include <reg52.h > #define uchar unsigned char sbit KEY = P1 ^ 0; //defines the initial state of the bit keysta = 1; //button for a standalone button uchar code dispcode[] = {0xc0,0xf9,0xa4,0xb0, //Digital Tube 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e};void timer0init ();//Timer initialization function void keyprocess ();//Key handling function Void main () { Timer0init (); Keyprocess ();}void timer0init () {tmod |= 0x01; th0 = 0xf8; //Timing 2mstl0 = 0xcd; ea =1; et0 = 1; tr0 = 1;} Void keyprocess () {bit lasttime = 1; //defines the previous key value, initialized to 1;uchar count = 0; The //count value is initialized to 0;key = 0x01; //key initialization; p0 = dispcode[count]; //digital tube initialization; while ( 1) {if (keysta != lasttime) //if the "Current key state" is different from "previous key state", it indicates that the key has action;{if (lasttime == &NBSP;1) //If the button has an action, then the previous key state is "bounce" (lasttime == 1), indicating that when the key is pressed, it responds to the action;{count++;if (count >=&NBSP;16) //Count added to 16 clear, only with a co-yang digital tube; {count = 0;} p0 = dispcode[count]; //Digital tube Display the corresponding value;}lasttime = keysta; //if the button has an action, The value of the previous key is updated;}}}VOID&NBSP;TIMER0IRQ () interrupt 1//2ms interrupts once {static keybuf = 0xff;// Defines a static key cache variable with a value of FF, primarily a staged key state;th0 = 0xf8; //re-writes the initial value;tl0 = 0xcd;keybuf = (keybuf << 1) | key; //left one bit, so that the current key state moved to the key cache;if (KEYBUF&NBSP;==&NBSP;0)//If moved 8 times, the key state is 0 , indicating that the key is actually pressed; {keysta = 0; //determines that the key state is a key;}else if (keybuf == 0xFF)//If you move 8 times, the key state is 1, indicating that the key is not pressed {keysta = 1;}}
This article is from the "Yoko" blog, make sure to keep this source http://9832751.blog.51cto.com/9822751/1834589
Simple timer to achieve the key to shake, but also hope that you more guidance!