0% found this document useful (0 votes)
8 views5 pages

STM32 Timer Base Docx

STM32_Timer_base_docx

Uploaded by

zOubaida fe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

STM32 Timer Base Docx

STM32_Timer_base_docx

Uploaded by

zOubaida fe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Timer’s handling through the applications

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.
/*******************************************************************************/

static void Delay_T3( unsigned long ms ) {


unsigned long m;
for( m = 0; m <= ms; m++ )

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 */
}
}

static void Timer3_Init(void) {

RCC_APB1ENR.TIM3EN=1; /* Enable the APB clock FOR TIM3 */

/* fCK_PSC / (PSC[15:0] + 1)
(8 MHz / (7+1)) = 1 MHz timer clock speed */
TIM3_PSC = 7;

TIM3_ARR = 999; /* (1 MHz / 1000) = 1KHz = 1ms */


TIM3_CR1.EN = 1; /* Finally enable TIM3 module */
}

static void Config_ GPIO (void) {


/* Enable the APB clock all GPIO port C */
RCC_APB2ENR.IOPCEN=1;
GPIOC_CRL=0x44444444;
GPIOC_CRH=0x44444444;
/* PC13 as output */
GPIOC_CRHbits.CNF13=0x0;
GPIOC_CRHbits.MODE13=0x3;
//GPIO_Digital_Output(&GPIOC_ODR,_GPIO_PINMASK_13);
}

int main(void) {
Timer3_Init ();
Config_ GPIO ();

while(1) { /* Endless loop */

GPIOC_BSRRbits.BS13=1; /* Turn ON the LED of PC13 */


//GPIOC_ODR.B13=1;
Delay_T3(1000);
/* Turn OFF the LED of PC13 */
GPIOC_BSRRbits.BR13=1;
//GPIOC_ODR.B13=0;
Delay_T3(1000);
}
}

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){
}
}

You might also like