就是想做个定时中断 用小灯的灭亮显示

2019-07-21 02:07发布

#include "stm32f10x_lib.h" void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY)==RESET); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//预取指缓存使能
FLASH_SetLatency(FLASH_Latency_2);  //设置代码延时值 延迟周期为2
RCC_HCLKConfig(RCC_SYSCLK_Div1);   //AHB时钟 = 系统时钟
RCC_PCLK2Config(RCC_HCLK_Div1);    //APB2时钟 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2);    //APB1时钟 = HCLK
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);  //PLL输入时钟 x 9
RCC_PLLCmd(ENABLE); while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);    //PLL为信号源
} void NVIC_Configuration(void)
{
 NVIC_InitTypeDef NVIC_InitStructure;
 #ifdef VECT_TAB_RAM
   NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
 #else
    NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
 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);
} void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //设置为复挽推出
GPIO_Init(GPIOC,&GPIO_InitStructure);
} void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef        TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); TIM_DeInit(TIM2); //将外设TIM2寄存器重设为缺省值
TIM_InternalClockConfig(TIM2); //设置TIMx内部时钟 TIM_TimeBaseStructure.TIM_Period = 2000;
TIM_TimeBaseStructure.TIM_Prescaler = 35999; //时钟分频36000
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;  //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
   
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Timing;//输出比较模式
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,ENABLE); TIM_PrescalerConfig(TIM2,35999,TIM_PSCReloadMode_Immediate); TIM_ClearFlag(TIM2, TIM_FLAG_Update); //清除中断标志
TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE ); //使能定时器中断
//TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);
}
 int main(void)
{
#ifdef DEBUG
 debug();
 #endif  RCC_Configuration();  //使能外设时钟
 NVIC_Configuration(); //中断设置
 GPIO_Configuration(); //管脚使能
 TIM_Configuration();   while(1)
  {
  ;
  }
       
}  


求帮助修改   我用的是STM32F107  不过只要改出来就好
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。