cancel
Showing results for 
Search instead for 
Did you mean: 

how to initialize timers in stm32f103r8? does providing HSE is compulsary?

Skuma.18
Associate II

i want to create a delay function using timer TIM1 or TIM2 , after completely reading the datasheet i cannot initialize delay does it requires any external input?

i have attached my sample code

#include "stm32f10x.h"

void delay();

int main()

{

 /*Initialising Clock*/

  

 RCC->CR   = 0x00000103;            //HSI Enable

 RCC->CFGR  = 0x051C0000;            //HSI clock selected

 RCC->CR   = 0x01000103;            //PLL enabled

 RCC->AHBENR = 0x0001D057;            //Clock Enable

 RCC->APB2ENR = 0x00005E7D;            //Clock Enable

 RCC->APB1ENR = 0x3E7EC83F;            //Clock Enable

 RCC->CSR   = 0x0C000001;            //LSI Enabled

  

/* Initialising GPIO */

 GPIOA->CRL = 0x11111111;

 GPIOA->CRH = 0x11111111;

 GPIOB->CRL = 0x11111111;

 GPIOB->CRH = 0x11111111;

 GPIOC->CRL = 0x11111111;

 GPIOC->CRH = 0x11111111;

 GPIOD->CRL = 0x11111111;

 GPIOD->CRH = 0x11111111;

  

 while (1)

 {

 GPIOA->ODR = 0x0000FFFF;

 GPIOB->ODR = 0x0000FFFF;

 GPIOC->ODR = 0x0000FFFF;

 GPIOD->ODR = 0x0000FFFF;

 delay();

 GPIOA->ODR = 0x00000000;

 GPIOB->ODR = 0x00000000;

 GPIOC->ODR = 0x0000FFFF;

 GPIOD->ODR = 0x0000FFFF;

 delay();

 }

}

void delay()

{

 TIM2->CR1 = 0x0001;          //Counter Enabled

 TIM2->EGR = 0x0041;          //Trigger generation & update generation enabled

 TIM2->CNT = 0xFFFF;          //Counter value set

 TIM2->PSC = 0X0001;          //Prescalar value set

 TIM2->ARR = 0xFFFF;          //Auto - Reload register set

}

3 REPLIES 3

No device, and TIMs, can run from HSI. HSE is not required, but needed if you want to run at 72 MHz.

You really can't jam values into the PLL like that.

If you choose to program the chip at the register level, you are responsible for reading and understanding the reference manual, and for debugging your own code.

Perhaps review the HAL or SPL code to provide a working/function context to compare your own implementation against.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Skuma.18
Associate II

HI

Thanks for the reply

I have tried to execute my code in different ways finally i come through the NVIC interrupt handlers, NVIC acts as the global interrupt hence only by activating the NVIC we are able to activate the interrupt anyway i want to enable my timer without interrupt and the timer doesn't gets initialized, why is initialization of NVIC necessary

Interrupts and NVIC are not required to use the TIM in a free running mode.

Your delay routine might actually have to do something, and wait for the time to elapse.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..