PIC Interrupt
PIC Interrupt
01/03/13
what is an interrupt?
an interrupt is a process or a signal that stops a microprocessor /microcontroller from what it is doing so that something else can happen. E.g. Suppose you are sitting at home, chatting to someone. Suddenly the telephone rings. You stop chatting, and pick up the telephone to speak to the caller. When you have finished your telephone conversation, you go back to chatting to the person before the telephone rang. You can think of the main routine as you chatting to someone, the telephone ringing causes you to interrupt your chatting, and the interrupt routine is the process of talking on the telephone. When the telephone conversation has ended, you then go back to your main routine of chatting.
Lecture 21 -PIC Architecture 2
01/03/13
Sources of interrupt
The PIC has 4 sources of interrupt They can be split into two groups Two are sources of interrupts that can be applied externally to the PIC, while the other two are internal processes.
01/03/13
01/03/13
RB0 is obviously Port B bit 0. The INT symbolizes that it can also be configures as an external interrupt pin. Port B bits 4 to 7 can also be used for interrupts. Before we can use the INT or other Port B pins, we need to do two things. First we need to tell the PIC that we are going to use interrupts. Secondly, we need to specify which port B pin we will be using as an interrupt and not as an I/O pin.
Lecture 21 -PIC Architecture 5
01/03/13
Inside the PIC there is a register called INTCON, and is at address 8Bh.
Within this register there are 8 bits that can be enabled or disabled. Bit 7 of INTCON is called GIE. This is the Global Interrupt Enable. Setting this to 1 tells the PIC that we are going to use an interrupt. Bit 4 of INTCON is called INTE, which means INTerrupt Enable. Setting this bit to 1 tells the PIC that RB0 will be an interrupt pin. Setting bit 3, called RBIE, tells the PIc that we will be using Port B bits 4 to 7. Now the PIC knows when this pin goes high or low, it will need to stop what its doing and get on with an interrupt routine. Now, we need to tell the PIC whether the interrupt is going to be on the rising edge (0V to +5V) or the falling edge (+5V to 0V) transition of the signal. In other words, do we want the PIC to interrupt when the signal goes from low to high, or from high to low. By default, this is set up to be on the rising edge.
01/03/13
The bit we are interested in is bit 6, which is called INTEDG. Setting this to 1 will cause the PIC to interrupt on the rising edge (default state) and setting it to 0 will cause the PIC to interrupt on the falling edge. If you want the PIC to trigger on the rising edge, then you dont need to do anything to this bit. Now, unfortunately, the Option register is in Bank 1, which means that we have to change from bank 0 to bank 1, set the bit in the Option register, then come back to bank 0. The trick here is to do all of the Bank 1 registers in one hit, such as setting up the port pins, then coming back to Bank 0 when you are finished.
01/03/13 Lecture 21 -PIC Architecture 7
Interrupt Flag
INTCON register, bit 1 is the interrupt flag, called INTF. Now, when any interrupt occurs, this flag will be set to 1. While there isnt an interrupt, the flag is set to 0. While this flag is set to 1, the PIC cannot, and will not, respond to any other interrupt. The flag will be set to 1, and the PIC will go to our routine for processing the interrupt. If this flag wasnt set to 1, and the PIC was allowed to keep responding to the interrupt, then continually pulsing the pin will keep the PIC going back to the start of our interrupt routine, and never finishing it. There is a slight drawback to this flag. Although the PIC automatically sets this flag to 1, it doesnt set it back to 0! That task has to be done by the programmer. This is easily done, as We are sure you can guess, and has to be done after the PIC has executed the interrupt routine.
01/03/13
Memory Location
When we first power up the PIC, or if there is a reset, the Program Counter points to address 0000h, which is right at the start of the program memory. However, when there is an interrupt, the Program Counter will point to address 0004h. So, we first of all have to tell the PIC to jump over address 0004h, and keep the interrupt routine which starts at address 0004h separate from the rest of the program. First, we start our program with a command called ORG. This command means Origin, or start. We follow it with an address. Because the PIC will start at address 0000h, we type ORG 0000h. Next we need to skip over address 0004h. We do this by placing a GOTO instruction, followed by a label which points to our main program. We then follow this GOTO command with another ORG, this time with the address 0004h. It is after this command that we enter our interrupt routine. Now, we could either type in our interrupt routine directly following the second ORG command, or we can place a GOTO statement which points to the interrupt routine. To tell the PIC that it has come to the end of the interrupt routine we need to place the command RTFIE at the end of the routine. This command means return from the interrupt routine. When the PIC see this, the Program Counter points to the last location the PIC was at before the interrupt happened.
01/03/13 Lecture 21 -PIC Architecture 9
Interrupt code
ORG 0000h ;PIC starts here on power up and reset GOTO start ;Goto our main program ORG 0004h ;The PIC will come here on an interrupt : ;This is our interrupt routine that we : ;want the PIC to do when it receives : ;an interrupt RETFIE ;End of the interrupt routine start ;This is the start of our main program.
01/03/13
10
Points to remember
* if you are using the same register in your main program and the interrupt routine, bear in mind that the contents of the register will probably change when the interrupt occurs. For example, lets you are using the w register to send data to Port A in the main program, and you are also using the w register in the interrupt routine to move data from one location to another. If you are not careful, the w register will contain the last value it had when it was in the interrupt routine, and when you come back from the interrupt this data will be sent to Port A instead of the value you had before the interrupt happened. The way round this is to temporarily store the contents of the w register before you use it again in the interrupt routine.
01/03/13
11
Points to remember
There is a delay between when one interrupt occurs and when the next one can occur. As you know, the PIC has an external clock, which can either be a crystal or it can be a resistor-capacitor combination. Whatever the frequency of this clock, the PIC divides it by 4 and then uses this for its internal timing. For example if you have a 4MHz crystal connected to your PIC, then the PIC will carry out the instructions at 1MHz. This internal timing is called an Instruction Cycle. Now, the data sheet states (admittedly in very small print) that you must allow 3 to 4 instruction cycles between interrupts. The reason for the delay is the PIC needs time to jump to the interrupt address, set the flag, and come back out of the interrupt routine. So, bear this in mind if you are using another circuit to trigger an interrupt for the PIC.
01/03/13
12
Interrupt code
cont..
Now we need to tell the PIC that we are going to use interrupts, and we are using RB0 pin 6 as an interrupt pin:
bsf bsf INTCON,7 INTCON,4 ;GIE Global interrupt enable (1=enable) ;INTE - RB0 interrupt enable (1=enable)
We are going to clear the interrupt flag just in case (We never trust anything!) bcf INTCON,1 ;INTF - Clear flag bit just in case
01/03/13
13
Interrupt code
cont..
Now we need to set up our two ports. Remember that as we are using RB0 as an interrupt pin, this must be set up as an input:
bsf movw movwf movlw movwf bcf STATUS,5 0x01 TRISB 0x10 TRISA STATUS,5 ;Switch to Bank 1 ; ;Set RB0 as input ; ;Set the first 4 pins on PortA as output ;Come back to Bank 0
01/03/13
14
;Move the contents of COUNT into W ;Now move it to Port A ;Keep on doing this ;End of our program
01/03/13
15