Sunteți pe pagina 1din 10

Microprocessor based embedded systems Lecture 19: Interrupts (Cont.

)
Dr Musharraf A Hanif

Overview of the lecture


Review of interrupt in general Interrupt structure on the STM32F100 Cortex-M3s Nested Vector Interrupt Controller (NVIC) NVICs registers Enabling a specific interrupt Sample code for a timer interrupt

by Dr. Musharraf A Hanif

Interrupt Review

Interrupts are an asynchronous signal generated by a peripheral to obtain the CPUs attention urgently. In response to an active / pending interrupt, the processor runs the corresponding interrupt service routine (ISR). The context has to be saved at the start of the ISR and restored at the end of the ISR. The address for the interrupt service routines are stored in a vector table. In the STM32F100, the vector table is in the startup file, while the context switching is managed by the hardware.
3 by Dr. Musharraf A Hanif

STM32F100s Interrupt Structure

The various peripherals on the STM32F100 can generate an interrupt signal. These interrupt signals are connected to the CortexM3s Nested Vector Interrupt Controller.
Timer 1 Timer 2 ADC USART 1 SPI 1 ...
4 by Dr. Musharraf A Hanif

Nested Vector Interrupt Interrupt lines Controller (NVIC)

Information exchange between the NVIC and CPU

CPU

STM32F100s Interrupt Structure (Cont.)

In order to use an interrupt from a peripheral:


Configure peripheral to generate an interrupt signal (peripheral registers). Enable the corresponding interrupt in the NVIC (NVIC registers). Ensure that the ISR function has the same name as the entry for that interrupt source in the vector table.

by Dr. Musharraf A Hanif

The NVIC structure and registers

Reference manual for Cortex M3 and ARMv7 NVIC sections:

Cortex M3 Technical Reference Manual (chapter 6, page 62) ARMv7 Architecture Reference Manual (section B3.4, page 750)

by Dr. Musharraf A Hanif

Configuring timer 2 to generate an interrupt

How to configure the timer to generate an interrupt at the timer peripheral? How to set the correct bit in the interrupt enable registers? What name to use for the ISR function?

by Dr. Musharraf A Hanif

The initialization function


void Tim_2_Init(const uint16_t PERIOD) { TIM2_CR1 = 0; TIM2_CNT = 0;

if(PERIOD > 1)
{ } TIM2_PSC = 7999; TIM2_ARR = PERIOD - 1; //prescaler for 1ms //interrupt after PERIOD ms

else
{ } TIM2_PSC = 799; TIM2_ARR = 9; //prescaler for 1e-4 s //interrupt after PERIOD ms

TIM2_DIER = 0x0001; //enable interrupt in timer peripheral


TIM2_CR1 = 0x0001; NVIC_ISER0 = 1<<28; }
8 by Dr. Musharraf A Hanif

//start timer 2 //enable the correct channel in the NVIC

The ISR function


void TIM2_IRQHandler(void) { static uint8_t Count; LED_Toggle(); TIM2_SR = 0; // Clear the interrupt flag }

by Dr. Musharraf A Hanif

Main function
int main(void) { RCC_APB2ENR |= 0x00000010; RCC_APB1ENR |= 0x00000001; Tim_2_Init(500); LED_Init(); while(1) { } return(1); } //enable clock to GPIO port C //enable TIM2

10

by Dr. Musharraf A Hanif

S-ar putea să vă placă și