This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

keil mcbstm32f400 timer with interrupt

I am using keil mcbstm32f400 and I want a timer to generate interrupt every 2ms .I have written the code but i want some guidance whether i am approaching the problem in a right way.My doubts have been put in the comments in the code, some of them are basic doubts but i still asked them as i wanted to clear my concepts.I also wanted to know how to disable interrupts at beginning of isr and enabling interrupts before going out of isr.
Timer calculation= 2ms=16mhz/{(12499+1)*(63999+1)}

 #include "stm32f4xx_hal.h"              // Keil::Device:STM32Cube HAL:Common
#include "cmsis_os.h"                   // ARM::CMSIS:RTOS:Keil RTX
#include "Board_LED.h"                  // ::Board Support:LED
#include "Board_Buttons.h"              // ::Board Support:Buttons
#include "Board_ADC.h"                  // ::Board Support:A/D Converter
#include "RTE_Components.h"             // Component selection
//#include <EventRecorder.h>


    #include "GLCD_Config.h"
    #include "Board_GLCD.h"
    #include "Board_LED.h"
    #include "Board_ADC.h"


    #include <stm32f4xx_hal_tim.h>


//static TIM_HandleTypeDef htim2;

static TIM_HandleTypeDef s_TimerInstance = {
    .Instance = TIM2
};


//extern "C" void TIM2_IRQHandler()

        extern void TIM2_IRQHandler()
{
HAL_TIM_IRQHandler(&s_TimerInstance);
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)  // HERE *htim is correct or &HTIM2 WILL COME OR &S_TIMERINSTANCE
{
//IN THIS ISR HOW TO DISABLE INTERRUPT AT BEGINNING AND HOW TO ENABLE INTERRUPT BEFORE GOING OUT OF ISR
ADC_Initialize();
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
}

void InitializeTimer()//FOR 2ms TIMER WITH INTERRUPT PRESCALER AND PERIOD VALUES ARE CORRECT OR NOT ASSUMING 16MHZ CLOCK ,ALSO HOW TO CONFIRM THE CLOCK SPEED OF TIMER
{
    __TIM2_CLK_ENABLE();
    s_TimerInstance.Init.Prescaler = 63999;
    s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP;
    s_TimerInstance.Init.Period = 12499;
    s_TimerInstance.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    s_TimerInstance.Init.RepetitionCounter = 0;
    HAL_TIM_Base_Init(&s_TimerInstance);
    //HAL_TIM_Base_Start(&s_TimerInstance);
        HAL_TIM_Base_Start_IT(&s_TimerInstance);
}

void main(){
  osKernelInitialize();                     /* initialize CMSIS-RTOS          */
        HAL_Init();                               /* Initialize the HAL Library     */
  SystemClock_Config();                     /* Configure the System Clock     */
  LED_Initialize();                         /* Initialize LED                 */
  //Buttons_Initialize();                     /* Initialize Buttons             */
  ADC_Initialize();                         /* Initialize ADC                 */

        GLCD_Initialize();                    /* Graphical Display Init             */
            GLCD_SetBackgroundColor (GLCD_COLOR_WHITE);
            GLCD_ClearScreen ();
            GLCD_SetForegroundColor (GLCD_COLOR_WHITE);
            GLCD_SetBackgroundColor (GLCD_COLOR_BLUE);
            GLCD_SetFont            (&GLCD_Font_16x24);
            GLCD_DrawString (0, 9*24, "    constant temp   ");


InitializeTimer();
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
//remaining code








}