微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 定时器灯亮

定时器灯亮

时间:11-27 来源:互联网 点击:
//定时一秒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

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top