1. 程式人生 > >STM32F103C8開發TIM定時器(三)

STM32F103C8開發TIM定時器(三)

勾選韌體庫,選中:CMSIS>CORE;Device>Startup;>StdPeriph Drivers>EXTI;>Framework;>GPIO;>RCC;>TIM;等

#include "stm32f10x.h"

void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_FLAG_Update) != RESET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);
		GPIO_Write(GPIOA, ~GPIO_ReadInputData(GPIOA));
	}
}

int main()
{
	GPIO_InitTypeDef GPIO_InitS;
	NVIC_InitTypeDef NVIC_InitS;
	TIM_TimeBaseInitTypeDef TIM_TBIS;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	NVIC_InitS.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitS.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitS.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&NVIC_InitS);

	TIM_DeInit(TIM2);
	TIM_TBIS.TIM_Period = 10000;
	TIM_TBIS.TIM_Prescaler = 7200 - 1;
	TIM_TBIS.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM2, &TIM_TBIS);
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM2, ENABLE);


	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitS.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitS.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitS.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOA, &GPIO_InitS);

	GPIO_Write(GPIOA, 0xffff);
	while (1)
	{
	}
	return 0;
}