External Interrupt (button) control digital display

Source: Internet
Author: User

/*************************************** *********************************
Function: button interrupt control digital tube plus/minus display (Independent display)
Device used: stc12c5a32s2
**************************************** ********************************/
# Include "config. H"

# Define uint unsigned int
# Define uchar unsigned char
# Define ulong unsigned long

# Define signal p3_5 // initial keyboard Signal

# Define key6 p3_2 // The key6 port is the int0 Port
# Define key2 p3_3

Int num;

// Latency is about (0.001 * n) seconds.
Void delay (unsigned char N)
{
Ulong I;
Do {
I = cpuclk/1000;
While (I --);
} While (-- n! = 0 );
}

/*************************************** **********
External Interrupt 0 Function
Function: Press key6 and add a digital number.
**************************************** *********/
Void int0 () interrupt 0 // do not judge whether it is key6 press, because the key6 key causes this interruption
{
If (key6 = 0)
{
Delay (10); // delay to eliminate key Jitter
If (key6 = 0) // confirm that a key is pressed
{
Num ++;
If (Num> 9)
Num = 0;
P0 = num;
}
While (! Key6); // wait for key1 to release
Delay (10); // delay to eliminate loose Jitter
}
}

/*************************************** **********
External Interrupt 1 Function
Function: Press key2 and subtract one digit from the digital control.
**************************************** *********/
Void int1 () interrupt 2
{
If (key2 = 0)
{
Delay (10); // delay to eliminate key Jitter
If (key2 = 0) // confirm that a key is pressed
{
Num --;
If (Num <0)
Num = 9;
P0 = num;
}
While (! Key2); // wait for key1 to release
Delay (10); // delay to eliminate loose Jitter
}
}

// Initialization function of the External Interrupt
Void int_init ()
{
Ea = 1; // total disconnection
Ex0 = 1; // 0
It0 = 0; // low level trigger mode for External Interrupt 0
Ex1 = 1;
It1 = 0;
}

// Main program entry
Void main (void)
{
Int_init ();
Signal = 0; // important !! Although signal and it1 are the same address, signal must be initialized
 
Num = 0;
P0 = num;
 
For (;;){
}
}

External Interrupt procedure design steps:

1. Total disconnection

2. Out-of-Service Interruption

3. Set the External Interrupt trigger mode

4. interrupt handling program

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.