Multiple Timer Interrupt Example2
Multiple Timer Interrupt Example2
This chapter discusses a scenario where two tasks vie for execution
simultaneously. In a situation known as "preemption," allowing priority
interruption becomes essential to complete the first task (from Ready to Run
state) before enabling the second task to enter the "Run" mode. In this
experiment, two LEDs connected to GPIOC 6 and 7 pins flash to indicate the
update rates of the general-purpose timers used in this study.
1
2
The main block of the programmable timer is a 16-bit counter
with its related auto-reload register
The counter can count up, down or both up and down
The counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler
register can be written or read by software
This is true even when the counter is running
The time-base unit includes:
Counter register (TIMx_CNT)
Prescaler register (TIMx_PSC)
Auto-reload register (TIMx_ARR)
3
The buffer of the prescaler is reloaded with the preload value (content
of the TIMx_PSC register)
The auto-reload shadow register is updated with the preload value
(TIMx_ARR)
4
TIMx Slave Mode Control Register (TIMx_SMCR)
5
TIMx Auto-Reload Register (TIMx_ARR)
3) Application:
As mentioned above, we focus our attention on the simultaneous flashing of two
LEDs with different frequencies using Timer3 and Timer5 with interruption to
understand this behavior in the task priority management system.
void setup();
void GPIO_init();
void Timer3_init();
void Timer5_init();
6
void main(){
setup();
while(1)
{
};
}
void setup(){
GPIO_init();
Timer3_init();
Timer5_init();
}
void GPIO_init(){
RCC_APB2ENR.IOPCEN=1;
GPIOC_CRL=0x22444444;
GPIOC_CRH=0x44444444;
GPIOC_ODR.B6 = 1;
GPIOC_ODR.B7 = 1;
}
void Timer3_init(){
RCC_APB1ENR.TIM3EN = 1;
TIM3_CR1.CEN=0; //enable_TIM3_counter(false);
TIM3_PSC = 575;
TIM3_ARR = 62499;
DIR_TIM3_CR1_bit=1; // TIM3_CR1 |=(1<<4); Downcounting
TIM3_EGR.UG=1; // Re-initialize the counter and generate an update
NVIC_IntEnable(IVT_INT_TIM3);
EnableInterrupts();
NVIC_SetIntPriority (IVT_INT_TIM3, _NVIC_INT_PRIORITY_LVL1); // Set Priority
TIM3_DIER.UIE = 1; // up-date interrupt enable
TIM3_CR1.CEN=1; // enable_TIM3_counter
}
void Timer5_init(){
RCC_APB1ENR.TIM5EN = 1;
TIM5_CR1.CEN = 0;
TIM5_PSC = 575;
TIM5_ARR = 12499;
DIR_TIM5_CR1_bit=0; // UP-Counting
TIM5_EGR.UG=1; // Re-initialize the counter and generate an update
NVIC_IntEnable(IVT_INT_TIM5);
EnableInterrupts();
NVIC_SetIntPriority (IVT_INT_TIM5, _NVIC_INT_PRIORITY_LVL2); // Set Priority
TIM5_DIER.UIE = 1;
TIM5_CR1.CEN = 1;
}