EE Embedded Systems Fall 2015

Slides:



Advertisements
Similar presentations
Working with time: interrupts, counters and timers Chapter Six Dr. Gheith Abandah1.
Advertisements

Microprocessor and Microcontroller Based Systems Instructor: Eng.Moayed N. EL Mobaied The Islamic University of Gaza Faculty of Engineering Electrical.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir.
ECE 447 Fall 2009 Lecture 9: TI MSP430 Interrupts & Low Power Modes.
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 3: Input/output and co-processors dr.ir. A.C. Verschueren.
Introduction of Holtek HT-46 series MCU
68HC11 Polling and Interrupts
CSC Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
INTERRUPTS PROGRAMMING
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
System Clocks.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
MICROPROCESSOR INPUT/OUTPUT
PIC16F877 ISR Points to note Interrupt Triggers –Individual interrupt flag bits are set, regardless of the status of their corresponding mask bit, PEIE.
Microprocessors 1 MCS-51 Interrupts.
Timer Timer is a device, which counts the input at regular interval (δT) using clock pulses at its input. The counts increment on each pulse and store.
Chapter 2 Introducing the PIC Mid-Range Family and the 16F84A The aims of this chapter are to introduce: The PIC mid-range family, in overview The overall.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
1 © Unitec New Zealand Interrupt Lecture 6 Date: - 20 Sept, 2011 Embedded Hardware ETEC 6416.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Teachers Name : Suman Sarker Telecommunication Technology Subject Name : Microcontroller & Embedded System Subject Code : 6871 Semester : 7th Department.
ECE 447 Fall 2009 Lecture 7: MSP430 Polling and Interrupts.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
Working with Time: Interrupts, Counters and Timers
Interrupts ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Special Features. Device Configuration bits Revision Device Configuration bits Revision On-chip Power-on Reset (POR) Revision On-chip Power-on Reset (POR)
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
TIMERS.
Chapter 10 Interrupts. Basic Concepts in Interrupts  An interrupt is a communication process set up in a microprocessor or microcontroller in which:
Timer modules in PIC 16F877.  The PIC 16F877 basically has three independent timer modules,  denoted by the symbols, TIMER-0, TIMER1,andTIMER2. . These.
An Introduction to Embedded Systems, and the PIC Microcontroller Lecture 8: Interrupts The aims of this session are to explore the structure and application.
16F877A.
Why are Timer Functions Important?
Architectures of Digital Information Systems Part 1: Interrupts and DMA dr.ir. A.C. Verschueren Eindhoven University of Technology Section of Digital.
68HC11 Interrupts & Resets.
Timer and Interrupts.
UNIT – Microcontroller.
Computer Architecture
Interrupts, Counter and Timers
AVR Addressing Modes Subject: Microcontoller & Interfacing
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
ECE 3430 – Intro to Microcomputer Systems
An Introduction to Microprocessor Architecture using intel 8085 as a classic processor
Advisor: Prof. Gandhi Puvvada
EMT 348: Microcontroller Timer/counter
Interrupts, Tasks and Timers
EE Embedded Systems Fall 2015
EE Embedded Systems Fall 2015
Interrupts Interrupt is a process where an external device can get the attention of the microprocessor. The process starts from the I/O device The process.
Programmable Interval timer 8253 / 8254
Processor Fundamentals
Introducing the PIC Mid-Range Family and the 16F84A
PIC18 Timer Programming “Explain the assembly language programming for the timer and counter module”
Lecture 9: TI MSP430 Interrupts & Low Power Modes
Programmable Interval timer 8253 / 8254
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
COMP3221: Microprocessors and Embedded Systems
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Presentation transcript:

EE 22442 - Embedded Systems Fall 2015 Chapter Six: Working with time: interrupts, counters and timers EE 22442 - Embedded Systems Fall 2015 Belal H. Sababha, Ph.D. Assistant Professor of Electrical & Computer Engineering Computer Engineering Department King Abdullah II Faculty of Engineering Princess Sumaya University for Technology Amman, 11941, Jordan Phone: +962-6-535-9949 ext. 466 Email: b.sababha@psut.edu.jo Web: http://www.psut.edu.jo/sites/bsababha

Introduction In this chapter you will learn about: Why we need interrupts and counters/timers. The underlying interrupt hardware structure. The 16F84A interrupt structure. How to program with interrupts. The underlying microcontroller counter/timer hardware structure. The 16F84A Timer 0 structure. Simple applications of the counter/timer. The Sleep mode.

The main idea – interrupts A computer CPU follows the instructions of the program one by one in a precise and predictable fashion. An interrupt disturbs this order. Its function is to alert the CPU in no uncertain terms that some significant external event has happened, to stop it from what it is doing and force it to respond to what has happened. Originally interrupts were applied to allow emergency external events, such as power failure, the system overheating or major failure of a subsystem to get the attention of the CPU. As time went on, more subsystems gained the power to generate interrupts. This forced increasing complexity in interrupt structures and a need to recognize that not all interrupts were equal. To work successfully with interrupts, we need to understand both the hardware interrupt structure and the programming techniques needed to program successfully with them.

Interrupt structures

The 16F84A interrupt structure The 16F84A has four interrupt sources, all of which can be individually enabled or disabled: External interrupt. This is the only external interrupt input. It shares a pin with Port B, bit 0. It is edge triggered. Timer overflow. This is an interrupt caused by the Timer 0 module, which is the subject of the second half of this chapter. It occurs when the timer’s 8-bit counter overflows. Port B interrupt on change. This interrupts when a change is detected on any of the higher four bits of Port B. The mechanism was described in Section 3.4.1. EEPROM write complete. This interrupts when a write instruction to the EEPROM memory is completed.

The 16F84A interrupt sources

The INTCON register

The CPU response to an interrupt

Working with interrupts: Programming with a single interrupt Start the ISR at the interrupt vector, location 0004. Enable the interrupt that is to be used by setting the enable bit in the INTCON register. Set the Global Enable bit, GIE. Clear the interrupt flag within the ISR. End the ISR with a retfie instruction. Ensure that the interrupt source, for example Port B or Timer 0, is actually set up to generate interrupts!

Simple Assembly program using interrupts

Cont’d…

What happens if we use:

Lets write a program in C – Live demo

unsigned char j,k, l; void myDelay(void); interrupt(){ PORTD=0x00; PORTC=0xFF; for( l=0; l<10; l++) myDelay(); INTCON= INTCON & 0xFD; // or INTCON.INTF=0; clear the flag } void main() { unsigned char i; TRISD= 0x00; // PORTD is Output TRISC= 0x00; TRISB= 0x01; INTCON = INTCON | 0x90; // GIE and Local Enable the External Interrupt RB0 PORTC= 0x00; while(1){ for(i=0; i<200; i++){ PORTD = 0xFF; myDelay(); PORTD= 0x00; } } } This program will continuously blink the LEDs on PORTD, if an External Interrupt occurred on RB0, it will Turn On PORTC and Trun OFF PORTD for some amount of time void myDelay(void){ for(j=0; j<255; j++){ for(k=0; k<200; k++); }

Moving to multiple interrupts – identifying the source The 16F84A has four interrupt sources but only one interrupt vector. Therefore, if more than one interrupt is enabled, it is not obvious at the beginning of an ISR which interrupt has occurred. In this case the programmer must write the ISR so that at its beginning it tests the flags of all possible interrupts and determines from this which one has been called.

Handling Multiple Interrupts in Assembly

Handling Multiple Interrupts in C void interrupt(){ if(INTCON.RBIF){ //ISR For the PORTB change interrupt } if(INTCON.INTF){ //ISR for the external interrupt if(…){ //ISR3

Stopping interrupts from wrecking your program 1 – context saving An ISR that does not save the context of the CPU:

Solution to problem 1 An ISR that saves the context of the CPU: Q: How would you save the context in C?

Stopping interrupts from wrecking your program 2 – critical regions and masking We can resolve some of the problems of an interrupt occurring in a program section like the subroutine discussed above by appropriate context saving. Unfortunately, we can’t resolve them all, at least not just with context saving. What if an interrupt occurred in a software delay routine? The delay length would be increased by the duration of the ISR, which could be disastrous, and no amount of context saving would improve the situation.

Example of an Interrupt wrecking your program

Solution to Problem 2 We must accept the fact that in certain program areas we will not want to accept the intrusion of an interrupt under any circumstances, with or without context saving. We call these ‘critical regions’. We can disable, or ‘mask’, the interrupts for their duration by manipulating the enable bits in the INTCON register. Critical regions may include: times when the microcontroller is simply not readied to act on the interrupt (for example during initialization – hence, only enable interrupts after initialization is complete); time-sensitive activities, including timing loops and multi-instruction setting of outputs; any calculation made up of a series of instructions where the ISR makes use of the result. By properly applying the techniques of context saving and critical regions, we can make good use of interrupts without them displaying the more destructive side of their nature.

The main idea – counters and timers The digital counter reviewed

The counter as a timer

The 16F84A Timer 0 module

TMR0 Register Looking back to Figure 2.5, we can see that this appears as register TMR0 at memory location 01 in bank 0.

TMR0 Configuration Bits Timer 0 is configurable, controlled by a number of bits that appear in the OPTION register, as shown in Figure 6.9

TMR0 Overflow When the counter overflows, it sets the timer overflow flag, one of the PIC microcontroller’s four interrupt sources, which we met in Figure 6.2.

TMR0 Usage Hardware-generated delays Periodic Interrupt Generator Event counter Time Measurement between events

TMR0: Hardware-generated Delays and Periodic Interrupts TMR0 overflow interrupt with some logic may be used to generate delays Eg. Internal CLK (Fosc) = 800 KHz TMR CLK = Fosc/4 = 200KHz, T = 1/200KHz = 5uS (for every increment) TMR0 would overflow after 256 counts (if initialized to zero) = 256 * 5uS = 1275uS Using the prescaler of 4 (PS2,PS1,PS0  001) then TMR0 will overflow after: 4* 5uS * 256 = 5.120 ms if we use prescaler of 8 then TMR0 will increment every (1/200KHz)* 8 = 40uS  then if TMR0 counts 125 times it will overflow after 125 * 40uS = 5ms (hence initialize TMR0 to 256-125) to get the 125 counts before overflow. You may set the prescaler and number of counts to get an overflow interrupt. Afterwards you may use the interrupt with some extra logic to generate the required delay.

Live Demo A C program that generates interrupt based delays

TMR0: Event Counter Use RA4/T0CLK to increment TMR0 and count the number of times the counter got incremented

TMR0: Time measurement between events Eg. In the interrupt service routine of an external interrupt on a rising edge latch the value of TMR0 into edge1 Then in the ISR reconfigure the interrupt to happen on the next falling edge (Option register). Configure the Timer overflow interrupt and in this ISR count the times of overflow  cnt The next time a falling edge happens latch TMR0 into edge2. Then you may calculate the Pulse width:

The Watchdog Timer The WDT is another timer in the 16F84A that we need to take note of, even though it is not normally used in simple applications. A big danger in a computer-based system is that the software fails and the system locks up or becomes unresponsive. In a desktop computer such a lock-up can be annoying and one would normally have to reboot. In an embedded system it can be disastrous, as there may be no user to notice that there is something wrong and maybe no user interface anyway. The WDT offers a fairly brutal ‘solution’ to this problem.

The Watchdog Timer … cntd Its a counter, internal to the microcontroller, which is continually counting up. If it ever overflows, it forces the microcontroller into Reset. The WDT overflows every 18ms. This number can be multiplied by a prescaler set by the OPTION_REG. It is up to the programmer to ensure that within the program the WDT is repeatedly cleared. This is done with the instruction clrwdt. It is only when the program ceases to run correctly that these instructions are no longer executed and the overflow occurs.

The Watchdog Timer … cntd A WDT Reset is generally not good news for an embedded system. All current settings are of course destroyed and the program starts again. It is better than a program which is not running at all. Note that the WDT leaves one clue of its action behind, and that is through the TO bit in the Status register (Figure 2.3). It is possible to test this bit towards the beginning of a program and hence distinguish between a Power-on Reset and a WDT Reset. The 16F84A WDT is enabled by one of the configuration bits.

Sleep mode Going into the sleep mode is an important way of saving power. The microcontroller can be put into this mode by executing the instruction SLEEP. Once in Sleep mode, the microcontroller almost goes into suspended animation. The clock oscillator is switched off. The WDT is cleared Program execution is suspended All ports retain their current settings The PD and TO bits in the Status register are cleared and set respectively. If enabled, the WDT continues running. Under these conditions, power consumption falls to a negligible amount, atypical value of 1 uA, under specific ideal operating conditions.

Waking up from Sleep mode The 16F84A wakes from Sleep in the following situations: External reset through MCLR pin: The state of the PD bit in the Status register will indicate that the MCU was in sleep mode. WDT wake-up: The function of the WDT is a little different in Sleep. The WDT is blocked from causing a reset when in Sleep. Instead, on overflow it just causes a wake-up from Sleep. Occurrence of interrupt: Any individually enabled interrupts cause wake-up from Sleep, regardless of the state of the Global Interrupt Enable. Timer 0 cannot, however, generate an interrupt, as the internal clock is disabled. On wake-up, the oscillator circuit is restarted. It must complete its count before program execution can resume. The Sleep mode is extremely powerful concept, Many devices are not continuously active when powered. If put into Sleep when not in use, their power consumption can be dramatically reduced.

Interrupt latency Before executing the ISR, the CPU needs to: Finish executing the current instruction Push the PC to the Stack Reload the PC with 0x0004 Fetch the first instruction of the ISR

Summary Interrupts and counters/timers are important hardware features of almost all microcontrollers. They both carry a number of important hardware and software concepts, which must be understood. The basic techniques of using interrupts and counters/timers have been introduced in this chapter. There is considerably increased sophistication in their use in more advanced applications. Live demos on: 1. Periodic Interrupt 2. Event counter