用tim2做个250us的定时
用来把led翻转 本来是2s的翻转
但现在实际是1s翻转
看了半天不知道为什么
各位大侠 帮分析下,程序如下:
谢谢大家!
//////////////////////////////////////////////////////////////////////
void TIM2_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能时钟
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);
//时基模块设置
/////////////////////////////////////////////////////////////////
//
//中断频率= apb1 clock/(perscaler+1) /period
//
/////////////////////////////////////////////////////////////////
//TIM2 使用内部时钟
//TIM_InternalClockConfig(TIM2);
//TIM2基本设置
//设置预分频器分频系数35,即APB1=72M/2, TIM1_CLK=36M/36=1MHz
//TIM_Period(TIM2_ARR)=250,计数器向上计数到250后产生更新事件,计数值归零
//向上计数模式
//TIM_RepetitionCounter(TIM2_RCR)=0,每次向上溢出都产生更新事件
// wave文件为4KHz mono pcm编码 ,所以取中断时间 为250us 4kHz=1M/250
TIM_DeInit(TIM2);
TIM_BaseInitStructure.TIM_Period = 250;
TIM_BaseInitStructure.TIM_Prescaler = 35;//0
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure);
//TIM_PrescalerConfig(TIM2, 35, TIM_PSCReloadMode_Immediate);//35+1预分频
//清中断,以免一启用中断后立即产生中断
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
//使能TIM1中断源
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//TIM1总开关:开启
TIM_Cmd(TIM2, ENABLE);
}
//////////////////////////////////////////////////////////////////////
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* 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)
{
}
}
}
/////////////////////////////////////////////////////////////////////////
void NVIC_Configuration(void)
{
// NVIC_InitTypeDef NVIC_InitStructure;
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQChannel;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
}
////////////////////////////////////////////////////////////////////////
中断程序
void TIM2_IRQHandler(void)
{
vu16 i;
if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
m++;
if(m>=8000)
{
m=0;
itcount++;
i= GPIO_ReadOutputData(GPIOF);
i=i^0x0200;
GPIO_Write(GPIOF,i);
}
}
} |