/* The mcc6 compiler is familiar with the interrupt and timer0 functions. When no interruption occurs, the first digital tube on the board is ra0,
When the External Interrupt (rb0) is pressed, the digital display is paused.
Device: More than 8.2 mplab software, mcc6 compiler, MCD-II simulator, engineering board, chip pic18f4520
XT oscillator, timed guard off, low voltage programming off */
# 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 inio () // port initialization program
{
Trisa = 0xfe; // 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 ).
Trisb = 0x01; // rb0 is input
Portb = 0x01; // port B is initially high, and port B is valid (Press)
Intcon2bits. rbpu = 0; // The pulling potential of port B.
Porta = 0x00;
Portd = 0xfe; // enables low-level 1st digital Tubes
}
//************************************** ********
# Pragma code high_vector_section = 0x8 // # pragma Code specifies the starting address of the program following the ROM as 0x8 and as for 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
{
Int I = 0; I ++; // execute this statement when you press it for the first time.
If (portbbits. rb0 = 0) // when you press rb0 again, clear the interrupt and return to the main program
{
Intconbits. int0if = 0;
}
# Pragma interruptlow pic18f_high_isr // jump to the low-interrupt service program and remember to clearly identify the interrupt flag
Void pic18f_low_isr ()
{
// Buzze (); // buzzer Function
// Int2if = 0; // intcon3 register
}
Void main ()
{
Char num;
Inio (); // port initialization program
Intconbits. gie = 0; // disable Global interrupt intcon1
// Timer0 delayed interruption
T0con = 0x45; // disable timer0 enabling. t0 is an 8-bit scheduled time./pre-divider allocated for the extension period. 1: 64
Tmr0l = 22; // The initial latency value of us
// An interruption occurs when the delay is reached.
Intconbits. tmr0if = 0; // clear the interrupt flag, without audible interruption
Intcon2bits. tmr0ip = 1; // enabling intcon2 with a high priority is equal to 0
Intconbits. tmr0ie = 1; // timer0 interrupt overflow allowed
Rconbits. IPEN = 1; // enable the rcon priority
Intcon2bits. intedg0 = 0; // enable intcon2 to trigger edge interruption
Intcon2bits. RBIP = 1; // The Key of port B is of high priority.
Intconbits. int0if = 0; // clear the priority of the External Interrupt mark intcon3
Intconbits. int0ie = 1; // enable int0 External Interrupt enable bit
Intconbits. gie = 1; // enable global interrupt
T0conbits. tmr0on = 1; // start t0 Timer
// 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];
T0conbits. tmr0on = 1; // start t0 Timer
If (intconbits. tmr0if = 1) // delay to 3000us
{
Intconbits. tmr0if = 0; // clear the interrupt flag
}
}
}
}