Presentation is loading. Please wait.

Presentation is loading. Please wait.

USART interrupt.

Similar presentations


Presentation on theme: "USART interrupt."— Presentation transcript:

1 USART interrupt

2 Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status of the USART receive register at regular intervals In the code examples presented so far, the microcontroller continuously checks the USART receive register every fixed number of instruction cycles.

3 USART receive polling code
unsigned char getch() { /* retrieve one byte */ while(!RCIF) /* set when register is not empty */ continue; return RCREG; } //taken from usart.c

4 Disadvantages of polling
While the microcontroller is continuously checking the USART receive flag the microcontroller isn’t doing anything else. All the resources of the microcontroller are dedicated to waiting until data is received in the USART receive register. This is very wasteful of processing resources

5 USART receive using interrupt
Using the USART receive flag to invoke an interrupt (instead of continuously checking it) on the other hand is less wasteful of microcontroller resources. While the microcontroller is waiting for the USART receive flag to change state and invoke the Interrupt Service Routine, the microcontroller can undertake other tasks, such as monitoring port pins, performing calculations or processing a message that has already been received and stored in a buffer.

6 USART ISR Receive routine
//main.c #include <pic.h> // Include HITECH CC header file #include <stdio.h> // include stdio header for printf function #include "usart.h" __CONFIG (0x3f3a); void init_comms(void); void putch(unsigned char byte); unsigned char input; void interrupt ISR(void) { If (RCIF)//received an incoming signal If (OERR)//overflow error CREN=0; CREN=1; } Input=RCREG; RCIF=0; void main (void) unsigned char ; TRISD = 0; // initializing PORT D as an output PORTD = 0; // zeroing out PORT D Init_comms(); while (1) // endless loop PORTD=input; // the received data is sent to PORT D NOP(); void init_comms(void) { TRISB5 = 1; TRISB7 = 0; SPBRG = 129; // set the baud rate for 9600 using hard typed number //Continuous 8 bit asynchronous non inverted low speed communication RCSTA = 0x90; // SPEN and CREN bit = 1, RX9EN = 0 TXSTA = 0x24;//TXEN = 1, BRGH=1, SYNC = 0 - note for 9600 using asynchronous communication TXIE=0;//disable interrupt on transmit pin RCIE=1;//enable interrupt on receive pin //BAUDCTL = 0; //BRG16 = 0 //not used on new versions of hi-tech C } void putch(unsigned char byte) /* output one byte */ while(!TXIF) /* set when register is empty */ continue; TXREG = byte;

7 Buffering and Interrupts
A common way to process information received from a USART is to place the information into a buffer. A buffer is an array that contains data. Buffers can be of a fixed size or a dynamic size Buffering data from the USART receive register into a software based buffer is a good idea when the software developers intention is to have the microcontroller resources predominantly allocated to processing of the USART data.

8 When to buffer Assuming data comes in as a continuous stream at 9600 Baud, then 1 byte packet will be received continuously about every 1ms. If processing takes 2ms then data will build up in the USART hardware buffer. If more than 2 bytes are held in the hardware buffer, an overflow condition occurs and this can cause the USART to lock up. Such situations require a software buffer for the USART data and require the USART receive to be disabled while the data is processed from a software buffer at a latter time.

9 Buffering is also undertaken to minimize conflicts for microcontroller resources between obtaining USART receive data and processing the USART receive data. For example in a GPS unit uses a microcontroller to display latitude and longitude data on a LCD. The GPS continuously streams data and the microcontroller must continuously monitor its USART for the GPS data stream until the GPS is to send latitude and longitude data. From here the microcontroller must store the GPS latitude and longitude in a software buffer.

10 After the microcontroller has stored all the latitude and longitude data in a buffer, it must disable the USART and then processes the Location data buffer and displays the location on the LCD. The PIC microcontroller does not have enough speed resources to easily process the location data and display the data on the LCD while still receiving and monitoring the GPS stream. After the location has been displayed on the LCD the USART must be re-enabled and GPS stream monitored again until the GPS signals it is to send latitude and longitude data again.

11 The flow chart below is not viable with the PIC16F series as the PIC does not have the resources to process the data between signals sent in a GPS data stream

12 GPS algorithm using data buffer
The flow chart represents an algorithm using a software buffer to capture GPS data and display GPS data on an LCD without causing the USART hardware buffer to overflow by disabling the USART while the software buffer is being processed.

13 Interrupt Receive USART
Interrupts ISR receive is best suited for intermittent USART receive signals in an application where the microcontroller is expected to undertake other duties besides servicing the USART signal For example if a microcontroller had to receive data from an TC35 or SIM900 intermittently and also had to respond to the GSM message and also had to control a house Locking up the microcontroller while it polled the USART receive flag as it waited for a GSM message would prevent any control of the house.

14 Such an application involving GSM would best be suited to using an ISR triggered by USART receive flag. Another example would be a microcontroller alarm system that is connected to a GSM. The GSM enables the alarm to be activated or deactivated. The primary function of the alarm involves monitoring the sensors in the alarm system and if a sensor is activated sending a GSM message and sounding the buzzer.

15 If a polled USART receive was used in this example the alarm would cease to perform any tasks while the USART waited for a receive signal. By using a ISR to service a GSM signal, the alarm is able to undertake monitoring of the sensors and sounding the buzzer.

16 When to poll the USART receive register
When polling the receive register is used, the only activity that the microcontroller can do is check to see if a USART signal has been received. Only ever use the poll technique if you wish to have the microcontroller dedicated to waiting for an incoming USART signal. In lab 1 we were constructing a ‘dumb IO expander’ and the processing of the USART signal was fast enough to be able to undertake this between incoming USART receive signals. After processing the USART receive signal the dumb IO expander has no other tasks other than to wait for a new USART receive signal.

17 For a novice programmer, most USART applications will veer towards operating polling the USART receive register. In practice most application problems will involved ISR’s to service the USART receive flag AND/OR software buffering of USART signals and processing a USART software buffer.

18 Take care with interrupts
The PIC midrange family PIC16 series generally have one interrupt and while the single interrupt can service multiple interrupt flags that have been triggered this approach is considered a flawed. The general policy with interrupts on the microcontrollers that have one ISR is to use one source of interrupt. For example if it decided that the ISR is to service the USART receive flag, then it would not be good practice to introduce a second interrupt source (such as Timer 0 interrupt flag)

19 Having the ISR service the USART receive flag would complicate a time based embedded operating system using a time based ISR. To realise such a design on the PIC16 where the ISR serviced two interrupt sources (USART and timer flags) would be risky and require a high level of design and advanced programming technique.

20 Potential problems with 1 ISR and two interrupt sources
What happens if the first interrupt flag is set (the ISR is invoked and code servicing this flag is executed) and during the ISR the second flag is set? Does the ISR routine automatically run again and service the second flag? If it doesn’t how does one implement code to over come this?

21 The general rule of thumb is only one ISR flag source on the PIC16f series of MCUs.
If you to use multiple ISR sources consider a MCU that has multiple interrupt service routines and then assigns prioritizations to the different ISR’s.


Download ppt "USART interrupt."

Similar presentations


Ads by Google