The PIR1 and PIR2 registers

The two Peripheral Interrupt Request registers are helpful when you need to know the current state of the PIC's built-in peripheral devices. These are the analogue-to-digital converters, the USART receiver and transmitter, and the comparators. On being interrupted, the PIC goes to the interrupt service routine (ISR) and reads data from each of several sources to find out the reason for the interrupt. But reading from these devices before they have completed their current processing gives a false result. We must let them finish what they are doing, and this is where the PIR registers are important.

PIR1 has three flags indicating the readiness or otherwise of the currently operating A-to-D converter, the receiver and the transmitter. Bit <6>, known as ADIF (Analogue-to-Digital Interrupt Flag) goes high when the curent conversion is complete. If you read this bit and find it is 0, you must try again later. Bit <5>, or RCIF, goes high when the receiver buffer is full, a 0 indicates that data is still accumulating in the buffer, so try again later. This flag is automatically cleared when you read the buffer data held in RCREG (p. 126).

Bit <4>, or TXIF, goes high when the transmitter buffer is empty and is waiting to receive more data to be transmitted. Otherwise it is 0, indicating that the buffer is full, and waiting to transmit it.

PIR2 has two flags that indicate that the output of a comparator has changed. Bit <6>, or C2IF, goes high when the output of Comparator 2 has changed, while bit <5>, or C1IF, goes high at a change in Comparator 1.

The bits remain set until you reset them to 0 by a bit clear operation. This means that we do not have to read the bit as soon as a comparison has been made. We can access the bits at any later time. However, if you want to use these bits to monitor further comparisons you must clear the bits to 0 at some stage before then.

All these flag bits are set when the events occur, whether or not interrupts are enabled, so you can use them even if you are not using interrupts. They tell you when it is safe to read data from these peripherals. But if you switch interrupts on later in the program, remember to reset the flags before you do it.

Was this article helpful?

+1 0

Post a comment