//定時(shí)一秒LED亮#include "stm32f10x.h"
unsigned int TimingDelay;
void Delay(unsigned int x)
{
TimingDelay=x;
while(TimingDelay--);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) != RESET)//檢查中斷溢出標(biāo)志位
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清中斷溢出標(biāo)志位
GPIO_SetBits(GPIOC, GPIO_Pin_7);
}
}
int main(void)
{
SystemInit();
//配置IO口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);//IO口使能設(shè)置
GPIO_InitTypeDef GPIO_InitStructure; //定義結(jié)構(gòu)體
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//設(shè)置定時(shí)器2
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//打開(kāi)TIM2外設(shè)時(shí)鐘
TIM_TimeBaseStructure.TIM_Period = 10000;//1s
TIM_TimeBaseStructure.TIM_Prescaler = 7199;//7200分頻
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//設(shè)置時(shí)鐘分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//設(shè)置計(jì)數(shù)方式為向上計(jì)數(shù)
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//初始化定時(shí)器2
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//打開(kāi)定時(shí)器2中斷
TIM_Cmd(TIM2,ENABLE);//使能定時(shí)器
//使能TIM2中斷
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//選擇TIM2全局中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while(1);
}
//以下是報(bào)錯(cuò)函數(shù)
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif
評(píng)論