我使用 GD32F150G8U6 器件,单板基本工作正常,包括 GPIO 点灯, USB FS, UART 等。在使用 Timer 时遇到连接在 APB2 上的定时器无法工作。
我初始化定时器时钟
RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_GPIOA | RCC_AHBPERIPH_GPIOB , ENABLE);
RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2 | RCC_APB1PERIPH_TIMER3 | RCC_APB1PERIPH_TIMER6 | RCC_APB1PERIPH_TIMER14, ENABLE);
RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_TIMER15 | RCC_APB2PERIPH_TIMER16 | RCC_APB2PERIPH_TIMER17, ENABLE);
设置 TIMER3
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE6, GPIO_AF_1);
TIMER_DeInit(TIMER3);
TIMER_TimeBaseStructure.TIMER_Prescaler = 719;
TIMER_TimeBaseStructure.TIMER_CounterMode= TIMER_COUNTER_UP;
TIMER_TimeBaseStructure.TIMER_Period = 50;
TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
TIMER_BaseInit(TIMER3, &TIMER_TimeBaseStructure);
TIMER_ICInitStructure.TIMER_CH = TIMER_CH_1;
TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH;
TIMER_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE;
TIMER_OCInitStructure.TIMER_Pulse = 25;
TIMER_OC1_Init(TIMER3, &TIMER_OCInitStructure);
TIMER_OC1_Preload(TIMER3, TIMER_OC_PRELOAD_DISABLE);
TIMER_CARLPreloadConfig(TIMER3, ENABLE);
TIMER_Enable(TIMER3, ENABLE);
用示波器在 PA6 上可观察到方波信号。但是对于 Timer17, 基本上是同样的设置
GPIO_InitStructure.GPIO_Pin = GPIO_PIN_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PINSOURCE7, GPIO_AF_5);
TIMER_DeInit(TIMER17);
TIMER_TimeBaseStructure.TIMER_Prescaler = 719;
TIMER_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
TIMER_TimeBaseStructure.TIMER_Period = 50;
TIMER_TimeBaseStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
TIMER_BaseInit(TIMER17, &TIMER_TimeBaseStructure);
TIMER_ICInitStructure.TIMER_CH = TIMER_CH_1;
TIMER_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1;
TIMER_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_HIGH;
TIMER_OCInitStructure.TIMER_OutputState= TIMER_OUTPUT_STATE_ENABLE;
TIMER_OCInitStructure.TIMER_Pulse = 25;
TIMER_OC1_Init(TIMER17, &TIMER_OCInitStructure);
TIMER_OC1_Preload(TIMER17, TIMER_OC_PRELOAD_DISABLE);
TIMER_CARLPreloadConfig(TIMER17, ENABLE);
TIMER_Enable(TIMER17, ENABLE);
在 PA7 上看不到任何信号。变换其它的I/O 引脚,试过 TIMER (15,16, 17)情况都相同,而这些定时器都是挂在 APB2上,它们需要进行特殊的设置吗?
|