/* 计算预分频因子 */
PrescalerValue = (uint16_t) (SystemCoreClock / 16000000) - 1;// 用RCC默认设置,48MHz
/* 时基设置 */
TIM_TimeBaseStructure.TIM_Period = 511;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* 输出比较模式设置 通道2*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val; // 这个值根本没有用?
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM3, DISABLE);
/* 使能TIM3计数器 */
TIM_Cmd(TIM3, ENABLE);
TIM_GenerateEvent(TIM3, TIM_EventSource_Update);
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOC输入时钟 */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
/* GPIOC 配置: TIM3 CH2 (PC7) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* 连接TIM3通道与AF */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_1);
我想用上面这段程序输出一个频率大约几百K的脉冲,但是实验的时候发现无法输出。频率大约到十几K之后,示波器上就什么都看不到了。用的是STM32F0-M51 discovery。 |