PIC interrupt 3

Source: Internet
Author: User

// 13-12-09 Tang xiangheng
// Master the same level of priority
/* Familiar with the interrupt function. When no interruption occurs, the first digital tube ra0 on the board is displayed, and 0-9 is displayed cyclically.
// When an External Interrupt (high priority) (rb0) is pressed, the digital display is paused. The buzzer sounds for 5 seconds, and then the digital tube flashes cyclically.
// For example, when Rb2 is pressed (defined as high or low priority), the buzzer clicks 5 times and then the digital tube flashes cyclically */
/* Device: More than 8.2 mplab software, mcc6 compiler, MCD-II simulator, engineering board, chip pic18f4520 */
// Experiment problem: when the program runs on the board, the lower priority can only be executed once! Then there will be no response when we press Rb2, but the priority is always effective.
# Include <p18cxxx. h>

Const char table [] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,0x92,0x82, 0xf8, 0x80, 0x98}; // 0-9 co-anode settings

Void pic18f_high_isr (void); // interrupt the Service Program
Void pic18f_low_isr (void );
Void inio (); // port initialization program
Void buzze (); // buzzer Function
Void delay_onesecond (); // one-second delay function
Void need_delay (unsigned X); // you can obtain twice the number of seconds through parameters.
Void delay (unsigned int X); // delay function
Void buzze2 (void); // high-priority alarm

Void delay (unsigned int X)
{
Unsigned int I;
I = X;
For (I = x; I> 0; I --);

}

Void buzze2 (void) // high-priority alarm
{
Int I = 5;
For (I = 5; I> 0; I --)
{
Trisc = 0;
Portc = 0x04; // RC2 = 1 voice

Delay (100000); // 100 ms subtle
Portcbits. RC2 = 0; // do not speak
Delay (100000); // 100000us 100 ms millisecond
}
}

Void delay_onesecond () // one-second delay function number 1 the time required to execute + 1 or-1 in this chip is 0.2us (subtle)
{// The value required for that 1 second is (1 us equals 5 digits) 5000 000
Unsigned int I;
For (I = 10000000; I> 0; I --);
}

Void need_delay (unsigned X) // you can obtain a multiple of seconds through parameters.
{
Unsigned int I;
For (I = x; I> 0; I --)
{
Delay_onesecond (); // call the 1-second function
}
}
Void inio () // port initialization program
{
Trisa = 0x7e; // ra0 can be bright
Trisd = 0x00; // Port D is set to output
Adcon1 = 0x07; // set port A to a common I/O port (Analog-to-analog conversion register ).
Trisc = 0; // set the C port to the output. The buzzer is valid at a high level.

Porta = 0x00;
Portd = 0xfe; // enables low-level 1st digital Tubes
}
//************************************** *********
Void buzze () // buzzer Function
{
Trisc = 0;
Portcbits. RC2 = 1; // voice
Need_delay (5); // voice latency: 5 seconds
Portcbits. RC2 = 0; // do not speak
}
//************************************** ********
# Pragma code high_vector_section = 0x8 // # pragma Code specifies the starting address of the program following the ROM as 0x8 and ends at 0x17.
Void high_vector ()
{
_ ASM goto pic18f_high_isr _ endasm // run this command to escape from the interrupted service program.
}
//************************************** ************
# Pragma code low_vector_section = 0x18
Void low_vector ()
{
_ ASM goto pic18f_low_isr _ endasm // run this command to escape from the interrupted service program.
}
//************************************** **************************
# Pragma code // return the default code segment

# Pragma interrupt pic18f_high_isr // # pragma interrupt keyword jump to the high interrupt service program. The following must be the next program
Void pic18f_high_isr () // remember to clear the interrupt flag
{
// Determine the same level of priority-flag bit
If (intconbits. int0if = 1)
{
Buzze (); // buzzer Function
Intconbits. int0if = 0; // iint0 External Interrupt flag intcon1 register written as intcon
}
If (intcon3bits. int2if = 1)
{
Buzze2 ();
Intcon3bits. int2if = 0; // int2 External Interrupt flag clear intcon1 register to write intcon
}
}

# Pragma interruptlow pic18f_high_isr // jump to the low-interrupt service program and remember to clearly identify the interrupt flag
Void pic18f_low_isr ()
{
// Buzze2 (); // low-priority alarm
// Intcon3bits. int2if = 0; // intcon3 register

}

Void main ()
{
Char num;
Inio (); // port initialization program
Intconbits. gie = 0; // disable Global interrupt intcon1
// Initialize the program
Trisb = 0x05; // rb0, Rb2 is set as input. It is automatically executed when a key is pressed.
Intcon2bits. rbpu = 0; // enables the uplink pulling potential of port B
// Portbbits. rb0
Rconbits. IPEN = 1; // enable the rcon priority

// Set the priority to 1
Intcon2bits. intedg0 = 0; // enable intcon2 to trigger edge interruption (source descent)
Intconbits. int0if = 0; // clear the priority of the External Interrupt mark
Intconbits. int0ie = 1; // enable int0 (for use by rb0) External Interrupt enable bit
// Intcon3bits. int0ip = 0; // the priority of the external interrupt is high. The int0ip does not exist. The default value is

// Set high priority 2
Intcon2bits. intedg2 = 0; // enable intcon2 to trigger edge interruption (source descent)
Intcon3bits. int2if = 0; // clear the priority of the External Interrupt mark
Intcon3bits. int2ip = 1; // the priority of an external interrupt is intcon3.
Intcon3bits. int2ie = 1; // enable interruption
Intconbits. giel = 1; // all peripheral interruptions allow low-priority enabling

Rconbits. IPEN = 1; // enable the interruption priority.
Intconbits. gie = 1; // enable global interrupt
// Main program
/* Conclusion: In the interrupt module, remember three registers: Flag, enable, and priority. In the program, close them first before opening */
While (1) // loop display 0-9
{
For (num = 0; num <10; num ++)
{
Portd = table [num];
Delay_onesecond (); // 1 second delay
}
}
}

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.