USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a...

21
USART interrupt

Transcript of USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a...

Page 1: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

USART interrupt

Page 2: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 3: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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

Page 4: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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

Page 5: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 6: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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 outputPORTD = 0; // zeroing out PORT DInit_comms();while (1) // endless loop{PORTD=input; // the received data is sent to PORT DNOP();NOP();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 communicationRCSTA = 0x90; // SPEN and CREN bit = 1, RX9EN = 0TXSTA = 0x24;//TXEN = 1, BRGH=1, SYNC = 0 - note for 9600 using asynchronous communicationTXIE=0;//disable interrupt on transmit pinRCIE=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;

}

Page 7: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 8: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 9: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.

Page 10: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.

Page 11: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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

Page 12: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 13: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 14: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.

Page 15: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.

Page 16: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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.

Page 17: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.

Page 18: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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)

Page 19: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.

Page 20: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

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?

Page 21: USART interrupt. Polling The code examples presented in lecture 2 and 3 used USART polling Polling a USART is when the micro-controller checks the status.

• 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.