//定时一秒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)//检查中断溢出标志位
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清中断溢出标志位
GPIO_SetBits(GPIOC, GPIO_Pin_7);
}
}
int main(void)
{
SystemInit();
//配置IO口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);//IO口使能设置
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
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);
//设置定时器2
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//打开TIM2外设时钟
TIM_TimeBaseStructure.TIM_Period = 10000;//1s
TIM_TimeBaseStructure.TIM_Prescaler = 7199;//7200分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//设置计数方式为向上计数
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//初始化定时器2
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//打开定时器2中断
TIM_Cmd(TIM2,ENABLE);//使能定时器
//使能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);
}
//以下是报错函数
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif