STM32 Timer Base Docx
STM32 Timer Base Docx
In this chapter, our goal is to explore several fundamental concepts essential for
fostering positive interaction, focusing on the principle of learning through practical
experience. With this approach, the student can accomplish and acquire the
following benefits:
1) The student become acquainted and be familiar with basic rudimentary
programming aimed at managing and manipulating the allocated time for each
execution task.
2) Enhance their fundamental understanding of configuring input and output ports
known as "GPIO" to gain a deeper insight into their origins.
3) In the user-friendly environment of "mikroC," the emphasis is on structured
programming that prioritizes internal registers over pre-defined functions,
offering students a more skill-enhancing learning experience.
Required Registers:
1
2
Application 1:
Our goal behind this example is to give an overview of how to deal with Timer3 in order to obtain a time
unit of 1 millisecond.
/*******************************************************************************/
3
{
TIM3_CNT = 0; /* Clear the count */
/* Wait UIF to be set */
while((TIM3_SR & TIM3_SR.UIF) == 0); /* This will generate 1ms delay */
TIM3_SR &= ~TIM3_SR.UIF; /* Reset UIF */
}
}
/* fCK_PSC / (PSC[15:0] + 1)
(8 MHz / (7+1)) = 1 MHz timer clock speed */
TIM3_PSC = 7;
int main(void) {
Timer3_Init ();
Config_ GPIO ();
Application 2 :
This app is designed to highlight time management by dealing with interruption caused by
overflow that occurred on Timer's counter.
4
void TIM2_IRQHandler() iv IVT_INT_TIM2 ics ICS_OFF
{
// Interrupt service routine code
GPIOD_ODR.B2 =~GPIOD_ODR.B2;
TIM2_SR &=~TIM2_SR.UIF;
TIM2_CNT &= 0x0 ;
}
static void Init_GPIO(void){
RCC_APB2ENR.IOPDEN=1;
GPIOD_CRL=0x44443244;
GPIOD_CRH=0x44444444;
}
static void Init_Timer2(void){
RCC_APB1ENR.TIM2EN=1;
TIM2_PSC=32000;
TIM2_ARR =500;
TIM2_DIER.UIE=1;
TIM2_CR1.CEN=1;
TIM2_CR1 |= 1<<0; //Counter enabled
TIM2_CR1 |= 1<<7; // Auto reload preload enable is buffered
TIM2_SR &= 0<<0; // clear flag register
TIM2_CNT &= 0x0 ; // init counter
NVIC_IntEnable(IVT_INT_TIM2);
EnableInterrupts();
}
void main() {
Init_GPIO();
Init_Timer2();
while(1){
}
}