我正在学习使用基于 arm cortex 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
}