我就想用STM32F定时器最简单的定时溢出功能,我应该配置哪几个的寄存器?
我执行了如下初始化程序,TIM2_CNT竟然不走
void TIM2_config(void)
{
/* Time base configuration[初始化时间基单元] */
TIM_TimeBaseStructure.TIM_Period = 0x1000;//周期值
TIM_TimeBaseStructure.TIM_Prescaler = 0;//时钟的预分频数=0x0000
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//TIM_ClockDivision 配置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Prescaler configuration [配置TIM的预分频数]*/
TIM_PrescalerConfig(TIM2,3600, TIM_PSCReloadMode_Immediate);//TIM2,1分频,预分频数立即载入
TIM_UpdateDisableConfig(TIM2,ENABLE);
/* TIM IT enable [使能TIM2比较中断]*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
其他部分的初始化
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Enable USART1, GPIOA, GPIOA and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOA
| RCC_APB2Periph_AFIO, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure the NVIC Preemption Priority Bits配置优先级组 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
比较定时模式没有配置吧
比较定时模式没有配置吧
/* TIM Output Compare Init structure definition */
typedef struct
{
u16 TIM_OCMode; /* Timer Output Compare Mode */
u16 TIM_Channel; /* Timer Channel */
u16 TIM_Pulse; /* PWM or OC Channel pulse length */
u16 TIM_OCPolarity; /* PWM, OCM or OPM Channel polarity */
} TIM_OCInitTypeDef;
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM2 configuration */
TIM_TimeBaseStructure.TIM_Period = 0x4AF;//预置周期计数值
TIM_TimeBaseStructure.TIM_Prescaler = 0xEA5F; //分频系数
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //TIM_ClockDivision 配置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Timing Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
TIM_OCInitStructure.TIM_Pulse = 0x0;
TIM_OCInit(TIM2, &TIM_OCInitStructure);
/* Prescaler configuration [配置TIM的预分频数]*/
TIM_PrescalerConfig(TIM2,3600, TIM_PSCReloadMode_Immediate);//TIM2,1分频,预分频数立即载入
TIM_UpdateDisableConfig(TIM2,ENABLE);
/* TIM IT enable [使能TIM2比较中断]*/
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
如何判断“TIM2_CNT竟然不走”
TIM_PrescalerConfig(TIM2,3600, TIM_PSCReloadMode_Immediate); 预分频数是否太大?
没开时钟,童鞋。 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
这个网站做的真他妈的垃圾头顶了。
第一回看到个回帖还要标题的,就一个普通的论坛,还要实名论证。JP啊。