STM32F定时器的功能非常丰富,像我这样单纯的TIM2定时器溢出中断人恐怕不多了,所以找例子资料也找不到费了九牛二虎之力终于杜撰出来了,发帖庆祝!我这里用了ST新版的STM32F FWLIB2.0库,用到的函数都添加了中文注释。
IAR编译的项目传不上来,有要看的朋友留下EMAIL我发邮件给你。 /* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h"
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void TIM_Configuration(void); void delay(void);
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) {
#ifdef DEBUG debug();/*[初始化外围设备指针]*/ #endif GPIO_Configuration();//初始化io口 NVIC_Configuration();//初始化中断嵌套 RCC_Configuration(); //初始化时钟与复位 TIM_Configuration();//初始化定时器
while(1) { delay(); } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose)[复位RCC外围设备寄存器到默认复位值] */ RCC_DeInit();
/* Enable HSE [HSE振荡器开启]*/ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready [等待HSE启动]*/ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer [预取缓冲区允许]*/ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state[代码2个延时周期] */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK [AHB时钟等于SYSCLK]*/ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK [APB2时钟等于HCLK]*/ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 [低速APB1时钟等于HCLK/2]*/ RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz [配置PLL时钟源和乘法因子][PLL时钟输入等于HSE时钟][PLL乘法因子取值9]*/ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL [允许PLL]*/ RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready [等待PLL时钟就绪]*/ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
/* Select PLL as system clock source [选择PLL作为系统时钟]*/ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source[等待PLL被作为系统时钟] */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* TIM2 clock enable [TIM2定时器允许]*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/******************************************************************************* * Function Name : GPIO_Configuration * Description : LED输出配置 * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC clock [使能GPIOC时钟]*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/* Configure PC.04, PC.05, PC.06 and PC.07 as output push-pull[把PC4、PC5、PC6、PC7配置成输出模式] */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//GPIO最高速度50MHz GPIO_Init(GPIOC, &GPIO_InitStructure); }
/******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures the nested vectored interrupt controller.[配置中断向量表的起始位置] * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 [设置中断向量表的起始位置0x20000000]*/ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000[设置中断向量表的起始位置0x0x08000000] */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif
/* Configure the NVIC Preemption Priority Bits[配置优先级组] */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the TIM2 gloabal Interrupt [允许TIM2全局中断]*/ 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); }
/******************************************************************************* * Function Name : TIM2_Configuration * Description : * Input : None * Output : None * Return : None *******************************************************************************/ void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // TIM_OCInitTypeDef TIM_OCInitStructure ; TIM_DeInit( TIM2);//复位TIM2定时器
/* TIM2 configuration */ TIM_TimeBaseStructure.TIM_Period = 0xffff; //最大计数值0xffff TIM_TimeBaseStructure.TIM_Prescaler = 0x36;//分频0x36 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; // 时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */ TIM_ClearFlag(TIM2, TIM_FLAG_Update);
/* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter [允许tim2计数]*/ TIM_Cmd(TIM2, ENABLE); }
/******************************************************************************* * Function Name : delay * Description : 延时 * Input : None * Output : None * Return : None *******************************************************************************/ void delay(void) { u32 i,j; for (i=0; i<0x0fffff; i++) { j ++; } } #ifdef DEBUG /******************************************************************************* * Function Name : assert_failed[断言失败] * Description : Reports the name of the source file and the source line number * where the assert error has occurred. * Input : - file: pointer to the source file name * - line: assert error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ while (1) {
} } #endif /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
|