LPC2148 UART using INTERRUPT
In this tutorial, we are going to use interrupt to send and receive data from the computer via UART. I have already covered the basic UART interface, which was using the blocking mode. This tutorial will continue from where we left the previous one. If you haven’t checked it out, better take a look at the UART tutorial first.
Extra Registers that we are going to use
Other than the ones mentioned in the tutorial linked above, we are also going to use some additional set of registers here. These registers are related to interrupts
- U0IER is the Interrupt Enable Register. We will enable the Tx and Rx interrupts here
- U0IIR is the interrupt identification register. It is used to identify, which interrupt got triggered.
There are some more Vectored Interrupt Controller (VIC) Registers, which we have to use, generally for all types of interrupts.
- VICIntSelect selects the interrupt is either FIQ or IRQ. We will be using IRQ for most of our applications
- VICIntEnable enables the respective interrupt
- VICVectCntl0 sets the priority to the interrupt. 0 being highest, and 15 being lowest
- VICVectAddr0 sets the address to the ISR for the Slot 0
Some Insight into the CODE
Other than the basic initialization, as mentioned in the previous tutorial, we also have to enable the interrupts for the UART0 Tx and Rx. The initialization procedure is as follows:
- Enable the DLAB bit to enable the write access to the DLM and DLL registers
- Set the baud rate according to the PCLK
- clear the DLAB, set 8 bit character length, disable the priority, and select 1 stop bit
- Enable the RDR, and THRE interrupts of UART0
the main Function is given below
- Set the PINSEL register to enable the alternate function for the Rx and Tx pins
- Enable PLL and set the PCLK to 60 MHz
- Initialize the UART0 in the interrupt mode
- Enable UART0 in the vectored interrupt
- Set the UART0 interrupt as IRQ
- Give the priority slot to the UART0 interrupt
- Assign the ISR function to the slot
Inside the ISR (Inetrrupt Service Routine), we will check if the interrupt is triggered by the RDR. This would mean that there is some data available in the U0RBR register. If the condition is true, we will read this data and send it back to the UART, by writing it to the U0THR register.
Note that once we read the data from the U0RBR register, the interrupt bit (RDR) gets cleared automatically.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
In the above picture, the PINK color is the one we send to the MCU, and the BLACK color is the one which is received back from mcu.