10 years, 3 months ago.  This question has been closed. Reason: Off Topic

TIM16 won't start (STM32F302 Nucleo)

I'm noob to mbed and Nucleo.

I want to make another periodical timer function to set higher priority than Ticker. My test code is as follow, using TIM16. Target board is STM32F302 Nucleo.

#include "mbed.h"

// STM32F302 Nucleo

// pin definitions
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

int main() {

    // provide clock to TIM16
    //RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM16, DISABLE);
    RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM16, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
    
    // Time base configuration
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 64000;
    TIM_TimeBaseStructure.TIM_Prescaler = 1000;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
    TIM_ITConfig(TIM16, TIM_IT_Update, ENABLE);

    // Enable the TIM16 gloabal Interrupt
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM16_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_Cmd(TIM16, ENABLE);

    myled = 1;
    while(1) {
        pc.printf("%d\r\n", TIM_GetCounter(TIM16));
        wait(0.5);
    }
}

void TIM1_UP_TIM16_IRQHandler(void) {

    if (TIM_GetITStatus(TIM16,TIM_IT_Update) != RESET) {
        TIM_ClearITPendingBit(TIM16,TIM_IT_Update);
        myled = !myled;
    }
}

TIM16 counter value remains 0, speculating TIM16 is not started. What's wrong in my code?

Thank you in advance.

It's now working. I'm sorry for upsetting you.

#include "mbed.h"

// STM32F302 Nucleo

// pin definitions
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);

int main() {
  
    // provide clock to TIM16
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
    pc.printf("PeriphClockCmd ENABLE done\r\n"); 
    
    // Time base configuration
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 64000;
    TIM_TimeBaseStructure.TIM_Prescaler = 1000;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
    TIM_ITConfig(TIM16, TIM_IT_Update, ENABLE);
    pc.printf("TIM16 init done\r\n");

    // Enable the TIM16 gloabal Interrupt
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM16_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    pc.printf("NVIC init done\r\n");
    
    TIM_Cmd(TIM16, ENABLE);
    pc.printf("TIM16 ENABLE done\r\n");

    myled = 1;
    while(1) {
        pc.printf("%d\r\n", TIM_GetCounter(TIM16));
        wait(0.5);
    }
}

extern "C" void TIM1_UP_TIM16_IRQHandler(void) {
    if (TIM_GetITStatus(TIM16,TIM_IT_Update) != RESET) {
        TIM_ClearITPendingBit(TIM16,TIM_IT_Update);
        myled = !myled;
    }
}
posted by _ peu605 19 Sep 2014