簡體   English   中英

如何在stm32f100RB中使用計時器6或計時器7?

[英]How to use the timer 6 or timer 7 in stm32f100RB?

我正在學習使用基於手臂皮質m3的MCU STM32f100RB。 為了測試計時器6,我編寫了以下代碼,應該使LED閃爍。 但這不起作用,任何人都可以幫我告訴我什么問題? 計時器是否正確初始化? 謝謝

#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
}

據我所知,您沒有啟用定時器外設的時鍾。

請注意,您的代碼會執行以下操作:

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

但這是不對的。 您在同一調用中為時鍾2使用外圍時鍾1和2的常量。

您需要具備:

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

您確實也應該使用標准外設庫來進行計時器初始化,不要直接戳寄存器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM