STM32 例程-2 Proteus下使LED闪烁(TIM3定时器)

1、stm32f10x_it.h和对应的.c中分别添加以下内容

extern u16 i;
void TIM3_IRQHandler(void);
void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
	{
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
		i++;
		if(i==1000)
		{
			GPIO_SetBits(GPIOC,GPIO_Pin_8);;
		}
		if(i==2000)
		{
			GPIO_ResetBits(GPIOC,GPIO_Pin_8);
			i=0;
		}
	}
}

  

2、Main.c修改如下

#include "stm32f10x.h"
__IO uint32_t TimingDelay;
u16 i=0;
void LED_GPIO_Config(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
    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(GPIOC,&GPIO_InitStructure);
}
void NVIC_Configuare(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x0F;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Configuare(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 10-1;                  
    TIM_TimeBaseStructure.TIM_Prescaler =(200-1);    
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;         
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 
    
    TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_Trigger,ENABLE); 
    TIM_Cmd(TIM3, ENABLE);  

}
void delay_nms(__IO uint32_t nTime)
{
    TimingDelay=nTime;
	while(TimingDelay!=0);
}
void Init_SysTick(void){

	if(SysTick_Config(SystemCoreClock/1000))
		while(1);
}
int main(void)
{
        SystemInit();
	RCC_ClockSecuritySystemCmd(ENABLE);
        LED_GPIO_Config();
	NVIC_Configuare();
	TIM3_Configuare();
	//Init_SysTick();
	while(1)
	{
			//GPIO_SetBits(GPIOC,GPIO_Pin_8);
			//delay_nms(1000);
			//GPIO_ResetBits(GPIOC,GPIO_Pin_8);
			//delay_nms(1000);
	}
} 

上面时钟频率按2M 计算

运行结果

 

posted @ 2021-02-13 22:11  zhaogaojian  阅读(574)  评论(0编辑  收藏  举报