stm32使用定时器1实现闪烁led

我的stm32学习总结性作业

有些库参考原子

定时器1头文件

#ifndef _TIM1_H_
#define _TIM1_H_
#include "sys.h"
void TIM1_Init(void);
	 
 
#endif
 

定时器c文件

#include "TIM1.h"

void TIM1_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//使用高级定时器注意他在桥2上
	
	
TIM_TimeBaseStructure.TIM_Period = (10000-1);//ARR
TIM_TimeBaseStructure.TIM_Prescaler = (7200-1);//PSC
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, & TIM_TimeBaseStructure);

//使能定时器中断
	TIM_ITConfig(TIM1, TIM_IT_Update,ENABLE);
	
//配置定时器1的中断优先等级
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	//使能定时器1
	TIM_Cmd(TIM1,ENABLE);
}

led头文件

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"

#define LED PCout(13)	// PA13


void LED1_Init(void);//初始化

		 				    
#endif

ledc文件

#include "led.h"


	    
//LED IO初始化
void LED1_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	 //使能PC端口时钟
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;				 //LED0-->PA.13 端口配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
 GPIO_Init(GPIOA, &GPIO_InitStructure);					 //根据设定参数初始化GPIOA.13
 
 GPIO_Init(GPIOC, &GPIO_InitStructure);	  				
}
 

主函数

#include "sys.h"
#include "LED.h"
#include "TIM1.h"

int main(void)
{//配置中断优先级
	NVIC_Configuration();
 LED1_Init();
TIM1_Init();
while (1)
{


}

}

void TIM1_UP_IRQHandler(void)
{
	TIM_ClearFlag(TIM1,TIM_FLAG_Update);
LED = ~LED;


}

效果图



展开阅读全文

没有更多推荐了,返回首页