本帖最后由 绿眼孤狼 于 2011-1-18 22:22 编辑
/***********************************************************************
* 定时公式: *
* T = (TIM_Period + 1) * (TIM_Prescaler + 1) / TIM2 Clock *
* *
* 附注:此结果已经示波器验证,正确! *
***********************************************************************/
#include "stm32f10x_lib.h"
#include "stm32f10x_conf.h"
/********** 函数声明 *********/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Configuration(void);
void Delay(u32 nCount);
/****** 主函数 ******/
int main(void)
{
#ifdef DEBUG
debug();
#endif
//----- 初始化 -------
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM_Configuration();
while(1);
}
/****** 系统时钟配置函数 ********/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit(); //RCC system reset(for debug purpose)
RCC_HSEConfig(RCC_HSE_ON); //Enable HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Wait till HSE is ready
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
FLASH_SetLatency(FLASH_Latency_2); //Set 2 Latency cycles
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB clock = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //APB2 clock = HCLK
RCC_PCLK1Config(RCC_HCLK_Div4); //APB1 clock = HCLK/4
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz
RCC_PLLCmd(ENABLE); //Enable PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLL is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock source
while(RCC_GetSYSCLKSource() != 0x08); //Wait till PLL is used as system clock source
//-----------------------------------------------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //GPIOD clock ENABLE
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //TIM2 clock ENABLE
}
}
/****** 中断配置函数 ********/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//--- 在内存中进行调试 -----
#ifdef VECT_TAB_RAM
NIVC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
//--- 在FLASH中进行调试 ----
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
//---- 配置TIM2中断,并使能 -----
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/********** 通用I/O配置函数 ***************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD8
}
/*****************************************************
函数名称:TIM_Configuration(void)
功能说明:通用定时器定时配置,配置定时溢出时间;
输入参数:无
返回值 :无
*****************************************************/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; //定时公式:(TIM_Period + 1) * (TIM_Prescaler + 1) / TIM2 Clock
TIM_TimeBaseStructure.TIM_Prescaler = 36000 - 1; //TIM2时钟为36MHz,经36000分频后为1KHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //初始化TIM2
//------------------------------
TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清中断标志,防止打开中断瞬间产生误操作
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //打开中断
TIM_Cmd(TIM2, ENABLE); //打开TIM2定时器
}
/*************************************************/
#ifdef DEBUG
void assert_failed(u8 *file, u32 line)
{
while(1);
}
/*********************************************/
/*********************************************/
#include "stm32f10x_it.h"
/*********** TIM2中断服务子程序 *************/
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清中断标志
//------------ 中断处理部分 -------------
GPIO_WriteBit(GPIOD, GPIO_Pin_8, (BitAction)(1 - \
GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_8)));
}
}
#endif |