Question

I'm learning to use the MCU STM32f100RB, which is based on the arm cortex m3. To test the timer 6, I wrote a bit of codes as following.It's supposed to make the led blink. But it does not work.Anyone can give me a hand telling me what's problem? Is the timer initialized correctly? Thx

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"

void delay_millisec(register unsigned short n);

int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB1Periph_TIM6, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Speed  =     GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_Pin    =     GPIO_Pin_8;     //enable the pin 8 and pin 9
    GPIO_InitStructure.GPIO_Mode   =     GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    while(1)
    {
        GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET);
        delay_millisec(1000);

        GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET);
        delay_millisec(1000);
    }
    return 0;
}

void delay_millisec(register unsigned short n) 
{
   if (n > 1) n--;
   TIM6->PSC = 23999;   // Set prescaler to 24,000 (PSC + 1)
   TIM6->ARR = n;       // n = 1 gives 2msec delay rest are ok
   TIM6->CNT = 0;
   TIM6->EGR = TIM_EGR_UG;  // copy values into actual registers!
                            // Enable timer in one pulse mode
   TIM6->CR1 |= (TIM_CR1_OPM | TIM_CR1_CEN);
   while (TIM6->CR1 & TIM_CR1_CEN); // wait for it to switch off
}
Was it helpful?

Solution

You are not enabling the timer peripheral's clock, from what I can see.

Note that your code does this:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB1Periph_TIM6, ENABLE);
       ^                      ^                      ^
       |                      |                      |
      APB2                   APB2                   APB1?!!

but this can't be right; you're using constants for peripheral clock 1 and 2 in the same call, to clock 2. That won't fly.

You need to have:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

You really should be using the Standard Peripheral Library for the timer initialization too, no point in poking registers directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top