Functions, Interrupts
Functions, Interrupts
Functions, Interrupts
Functions and Subroutines It is good practice to break programs into short functions or subroutines It makes programs easier to write and more reliable to test and maintain Functions are obviously useful for code that is called from more than one place Functions can readily be reused and incorporated into libraries
prg1
Prg2
Prg3
Complete stack frame for a subroutine with parameters passed, return address, saved copies of registers, and local variables
Interrupts
Interrupts are commonly used for a range of applications: Urgent tasks that must be executed promptly at higher priority than the main code. Infrequent tasks, such as handling slow input from humans. This saves the overhead of regular polling. Waking the CPU from sleep. This is particularly important in the MSP430, which typically spends much of its time in a low-power mode and can be awakened only by an interrupt. Calls to an operating system
ISR
The code to handle an interrupt is called an interrupt handler or interrupt service routine. Interrupts can be requested by most peripheral modules and some in the core of the MCU, such as the clock generator. Most interrupts are maskable, which means that they are effective only if the general interrupt enable (GIE) bit is set in the status register (SR).
ISR
The MSP430 uses vectored interrupts which means that the address of each ISR is stored in a vector table at a defined address in memory In most cases each vector is associated with a unique interrupt but some sources share a vector. Each interrupt vector has a distinct priority
INTERRUPT HANDLING
Interrupts must be handled in such a way that the code that was interrupted can be resumed without error. The hardware can take two extreme approaches to this 1. Copies of all the registers are saved on the stack automatically as part of the process for entering an interrupt 2. The opposite approach is for the hardware to save only the absolute minimum, which is the return address in the PC as in a subroutine.
What Happens when an Interrupt Is Requested? Hardware performs the following steps to launch the ISR 1.Any currently executing instruction is completed if the CPU was active when the interrupt was requested. 2. The PC, which points to the next instruction, is pushed onto the stack. 3. The SR is pushed onto the stack. 4. The interrupt with the highest priority is selected if multiple interrupts are waitingfor service.
The PC pops from the stack and execution resumes at the point where it was interrupted.
The device can be wakened only by an external signal. This is also called RAM retention mode.